c# - 在实例化对象上播放/停止动画/动画师

标签 c# unity-game-engine

我正在开发一个统一游戏,基本上,我有一个预制件,里面有一个 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获取AnimatorAnimation组件。如果动画是预制件的子级,则使用 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/

相关文章:

unity-game-engine - 如何为不同纵横比的屏幕设置 UI 位置动画?

javascript - 将回调从 Unity 传递到 JavaScript,然后返回到 Unity

c# - 函数插值 C#

c# - 异步编程控制流程

c# - HTTP 错误 401.0 - 未经授权

c# - 使用动态列表c#验证xpath最后一个元素的数据

c# - 谓词和 OrderBy , Func

iOS Developer Programs Specifications 加上 Unity3d export

c# - 如何根据动画师的位置条件启用 GameOver 菜单?

java - Unity 插件在使用通知 Compat Builder 时出现错误