版本:Unity 6(6000.0)
语言英语
  • C#

精灵.texture

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有投稿,但我们确实会阅读来自用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您建议的更改无法提交。请在几分钟后重试。感谢您抽出时间来帮助我们提高 Unity 文档的质量。

关闭

取消

public Texture2D texture;

描述

获取对已用纹理的引用。如果已打包,这将指向图集,如果已打包,将指向源精灵。

这仅返回精灵当前使用的纹理。您无法使用此方法更改纹理。

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