当前位置:网站首页>unity 代码为动画注册事件
unity 代码为动画注册事件
2022-06-10 21:54:00 【吴梓穆】
我们可以为动画添加事件,来实现自动播放下一段动画之类的效果,代码如下
private Animator animator;
private AnimationClip[] clips; //所有的动画
private void Awake()
{
animator = GetComponent<Animator>();
clips = animator.runtimeAnimatorController.animationClips;
AddAnimationEvent(clips, "EndEvent",1);
}
/// <summary>
/// 为动画片段批量注册事件 clips动画片段的数组 eventName 自己定义的事件实例方法名 timeRate在动画播到什么时候执行
/// </summary>
private void AddAnimationEvent(AnimationClip [] clips,string eventName,float timeRate)
{
for (int i = 0; i < clips.Length; i++)
{
AnimationEvent _event = new AnimationEvent();
_event.functionName = eventName;//设置绑定的事件名
_event.time = clips[i].length * timeRate;//设置事件的触发时机 timeRate =1 即动画播放完成时触发
clips[i].AddEvent(_event);
}
}
/// <summary>
/// 事件的实例方法
/// </summary>
private void EndEvent()
{
print("动画播放完成");
//播完动画,根据选择,自动播放或不自动播放
GameController.Instance.AutoPlay();
}
边栏推荐
- [XPath] use following sibling to obtain the following peer nodes
- dc_ Study and summary of labs--lab1
- Opencv_100问_第四章 (16-20)
- Redis数据结构
- 优化代码去除if-else
- Kubernetes binary installation (v1.20.15) (VI) deploying worknode nodes
- CCF CSP 202109-3 impulse neural network
- Keras deep learning practice (8) -- using data enhancement to improve neural network performance
- Web3 ecological decentralized financial platform sealem Finance
- CCF CSP 202109-4 collect cards
猜你喜欢
随机推荐
通达信股票开户安全吗?如何办理开户呢?
Whale conference empowers intelligent epidemic prevention
Sdn/nfv application in cloud data center
Custom view: graphics and image processing (I): using simple pictures
SMB anonyme
Mmdetection dataloader construction
Keras deep learning practice (8) -- using data enhancement to improve neural network performance
Face recognition software based on deepface model
ICML2022 | 從零開始重新審視端到端的語音到文本翻譯
Fallback operation in SVN
LeetCode - 5. Longest Palindromic Substring
【原创】医鹿APP九价HPV数据抓包分析
Sherri Monroe被任命为增材制造商绿色贸易协会的新任执行董事
Reflow and repaint
[original] analysis of nine price HPV data capture of Yilu app
Digital twin: third person mouse operation
Template_ Calculate number of combinations
【原创】医鹿APP九价HPV数据抓包分析
Déploiement et utilisation de base de la carte multi - réseau kubernets
Whale conference sharing: what should we do if the conference is difficult?









