我正在开发一个统一游戏,基本上,我有一个预制件,里面有一个 Sprite 。我创建了一个附加到该 Sprite 的动画。
FrogPrefab
|__ FrogSprite
我创建了一个带有公共(public)字段“预制件”的脚本,我在其中传递了我的预制件。
我的问题是,如何从脚本中停止并播放该动画。
我从我的启动方法实例化了我的预制件...
public GameObject gameCharacterPrefab;
private GameObject frog;
void start() {
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
}
我正在尝试做类似的事情......
frog.animation.stop();
感谢任何帮助
最佳答案
首先,请注意该函数应该被称为 Start
而不是 start
。也许这是问题中的拼写错误,但值得一提。
使用GetComponent
获取Animator
或Animation
组件。如果动画是预制件的子级,则使用 GetComponentInChildren
。
如果使用Animator
组件:
public GameObject gameCharacterPrefab;
private GameObject frog;
Vector3 objectPoolPosition = Vector3.zero;
Animator anim;
实例化预制件
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
获取Animator
组件
anim = frog.GetComponent<Animator>();
播放动画状态
anim.Play("AnimStateName");
停止动画
anim.StopPlayback();
如果使用Animation
组件:
public GameObject gameCharacterPrefab;
private GameObject frog;
Vector3 objectPoolPosition = Vector3.zero;
Animation anim;
实例化预制件
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
获取动画
组件
anim = frog.GetComponent<Animation>();
播放动画名称
anim.Play("AnimName");
停止动画
anim.Stop();
关于c# - 在实例化对象上播放/停止动画/动画师,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50770529/