c# - 在实例化对象上播放/停止动画/动画师
问题描述
我正在开发一个统一游戏,基本上,我有一个预制件,里面有一个精灵。我创建了一个附加到该精灵的动画。
FrogPrefab
|__ FrogSprite
我创建了一个带有公共字段“预制件”的脚本,我在其中传递了我的预制件。
我的问题是,我怎样才能从我的脚本中停止并播放这个动画。
我从我的 start 方法实例化了我的预制件......
public GameObject gameCharacterPrefab;
private GameObject frog;
void start() {
frog = (GameObject)Instantiate(gameCharacterPrefab, objectPoolPosition, Quaternion.identity);
}
我正在尝试做类似的事情......
frog.animation.stop();
感谢任何帮助
解决方案
首先,请注意该函数不应被Start
调用start
。也许这是问题中的错字,但值得一提。
用于GetComponent
获取Animator
orAnimation
组件。如果动画是预制件的子级,则使用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();
推荐阅读
- authentication - 中继 JWT 身份令牌是否安全?
- javascript - 如何使状态栏看起来像 HTML/CSS/JavaScript 中的某些在线订购状态栏?
- arrays - 根据matlab中的字典将一串字符转换为数字数组
- python - 如何在 Python3 中的函数中获取多个参数的用户输入
- javascript - 如何在一个 HTML 页面上的两个 Tabulator 表中显示一个 json 文件?
- azure - Azure 函数中的并发与 .Net 5(隔离)
- python-3.x - Python Selenium 按类名查找元素不起作用,并且在引发的错误中名称字符串的 0 位置有一个无条件点
- php - 比较来自 sql 数据库和 PHP 中 .xls 文件的两个西里尔字母字符串是否相等
- animation - Framer 运动复杂滚动动画
- java - 在 java 中使用多个线程安全地编辑数据库