Answers:
该代码已为您注释。请享用。
public Sprite sprite1; // Drag your first sprite here
public Sprite sprite2; // Drag your second sprite here
private SpriteRenderer spriteRenderer;
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // we are accessing the SpriteRenderer that is attached to the Gameobject
if (spriteRenderer.sprite == null) // if the sprite on spriteRenderer is null then
spriteRenderer.sprite = sprite1; // set the sprite to sprite1
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space)) // If the space bar is pushed down
{
ChangeTheDamnSprite (); // call method to change sprite
}
}
void ChangeTheDamnSprite ()
{
if (spriteRenderer.sprite == sprite1) // if the spriteRenderer sprite = sprite1 then change to sprite2
{
spriteRenderer.sprite = sprite2;
}
else
{
spriteRenderer.sprite = sprite1; // otherwise change it back to sprite1
}
}
您需要在游戏对象上附加一个精灵渲染器。创建一个新的C#脚本,并将一个GameObject附加到它。将代码粘贴在括号之间...我确定您可以从那里弄清楚它:)