Sprite.texture

Unity · Версия 2020.3 LTS · Scripting · Sprite

Sprite.texture

public Texture2D texture;

Описание

Получить ссылку на используемую текстуру. Если упаковано, это будет указывать на атлас, если не упаковано, будет указывать на исходный спрайт.

Это возвращает только ту текстуру, которую в данный момент использует спрайт. Вы не можете изменить текстуру, используя это.

//Прикрепите этот скрипт к SpriteGameObject. Убедитесь, что у него есть компонент SpriteRenderer (должен быть по умолчанию).
//Затем присоедините второй Sprite в окне Inspector к вашему первому SpriteGameObject< /а>

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);
        }
    }
}

Обновлено 17 ноября 2026 г.