首页 > 解决方案 > 在实例化对象上播放/停止动画/动画师

问题描述

我正在开发一个统一游戏,基本上,我有一个预制件,里面有一个精灵。我创建了一个附加到该精灵的动画。

FrogPrefab
    |__ FrogSprite

我创建了一个带有公共字段“预制件”的脚本,我在其中传递了我的预制件。

我的问题是,我怎样才能从我的脚本中停止并播放这个动画。

我从我的 start 方法实例化了我的预制件......

public GameObject gameCharacterPrefab;

private GameObject frog;

void start() {
    frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
}

我正在尝试做类似的事情......

frog.animation.stop();

感谢任何帮助

标签: c#unity3d

解决方案


首先,请注意该函数不应被Start调用start。也许这是问题中的错字,但值得一提。

用于GetComponent获取AnimatororAnimation组件。如果动画是预制件的子级,则使用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);

获取Animation组件

anim = frog.GetComponent<Animation>();

播放动画名称

anim.Play("AnimName");

停止动画

anim.Stop();

推荐阅读