我在YouTube上学习关于在代码中更改sprite动画的教程,我想知道是否可以将它更改为使用UI按钮更改sprite动画。有人知道怎么做吗。谢谢!
编辑:在您的帮助下,我重新放置了一些作品的脚本,它将雪碧图像从图像一更改为图像二,但我基本上要实现的是,每次我单击UI按钮时,精灵图像将从雪碧图像一(UI按钮单击)>雪碧图像二(UI按钮单击)>雪碧图像三(UI按钮单击)>然后重复这个过程,而不是自动改变自身的sprite图像。
发布于 2016-04-02 04:57:36
按钮有一个OnClick事件http://docs.unity3d.com/ScriptReference/UI.Button-onClick.html
您只需创建一个在单击按钮时调用的方法,在您的示例中,只创建一个更改的sprite代码。但是,当您使用计时器时,您将需要使用类似于bool的东西,因为onClick()
只在单击时被调用一次,而不是每个帧。
看,https://www.youtube.com/watch?v=J5ZNuM6K27E
bool b_RunSpriteAnim;
public void onClick(){
b_RunSpriteAnim = true;
}
void Update(){
if (b_RunSpriteAnim)
//your anim sprite stuff
}
然后,一旦雪碧anim完成,只需切换b_RunSpriteAnim
到false
并重置计时器。
编辑:,您不需要布尔值。我以为你想要它,是因为你使用了计时器(基于Youtube链接)。如果你只是想立即改变雪碧,那么你就不需要它了。至于Imagethree
不能工作,这是因为您从未将它包含在代码中。现在还不清楚你试图用Imagethree
实现什么,如果你把它也包括在onClick
中,它只会覆盖刚刚设置的图像2,所以我不知道你想要实现什么。
public void onClick(){
this.gameObject.GetComponent<SpriteRenderer>().sprite = Imagetwo;
}
第二版:
public Sprite[] Images;
//Index starts at one because we are setting the first sprite in Start() method
private int _Index = 1;
void Start(){
//Set the image to the first one
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[0];
}
public void onClick(){
//Reset back to 0 so it can loop again if the last sprite has been shown
if (_Index >= Images.Length)
_Index = 0;
//Set the image to array at element index, then increment
this.gameObject.GetComponent<SpriteRenderer>().sprite = Images[_Index++];
}
https://stackoverflow.com/questions/36372640
复制相似问题