public Texture2D texture ;

Descripción

Obtiene la referencia a la textura utilizada. Si está llena esto apuntará al atlas, si no está empaquetada señalará al sprite de la fuente.

This only returns the Texture the Sprite is currently using. You cannot change the Texture using this.

//Attach this script to a Sprite GameObject. Make sure it has a SpriteRenderer component (should have by default)
//Next, attach a second Sprite in the Inspector window of your first Sprite GameObject

using UnityEngine;

public class Example : MonoBehaviour { SpriteRenderer m_SpriteRenderer; public Sprite m_Sprite;

void Start() { //Fetch the SpriteRenderer of the Sprite m_SpriteRenderer = GetComponent<SpriteRenderer>(); //Output the current Texture of the Sprite (this returns the source Sprite if the Texture isn't packed) Debug.Log("Texture 1 : " + m_SpriteRenderer.sprite.texture); }

void Update() { //Press Space key to change the Sprite to the Sprite you attach in the Inspector if (Input.GetKeyDown(KeyCode.Space)) { //Change the Sprite m_SpriteRenderer.sprite = m_Sprite; //Output the Texture of the new Sprite (this returns the source Sprite if the Texture isn't packed) Debug.Log("Texture 2 : " + m_SpriteRenderer.sprite.texture); } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961