[Unity动画]03.动画事件

1.找到动画,添加动画事件

 

2.在脚本中添加回调方法

TestAnimator.cs

 1 using UnityEngine;
 2 
 3 public class TestAnimator : MonoBehaviour {
 4 
 5     //------------------------------------------外部
 6     public float Move = 0;
 7     public bool IsDying = false;
 8 
 9     //------------------------------------------内部
10     private Animator animator;
11 
12     //常数
13     private int baseLayerIndex;
14     private int idleStateHash;
15     private int runStateHash;
16     private int dyingStateHash;
17     private string movePara;
18     private string isDyingPara;
19 
20     void Start ()
21     {
22         animator = GetComponent<Animator>();
23 
24         baseLayerIndex = animator.GetLayerIndex("Base Layer");
25         idleStateHash = Animator.StringToHash("Base Layer.Idle");
26         runStateHash = Animator.StringToHash("Base Layer.Run");
27         dyingStateHash = Animator.StringToHash("Base Layer.Dying");
28         movePara = "move";
29         isDyingPara = "isDying";
30     }
31     
32     void Update ()
33     {
34         //------------------------------------------播放动作
35         animator.SetFloat(movePara, Move);
36         if (IsDying)
37         {
38             animator.SetBool(isDyingPara, true);
39             IsDying = false;
40         }
41 
42         //------------------------------------------动作恢复
43         AnimatorStateInfo stateInfo;
44         int fullPathHash;
45 
46         //BaseLayer
47         stateInfo = animator.GetCurrentAnimatorStateInfo(baseLayerIndex);
48         if (!animator.IsInTransition(baseLayerIndex) && stateInfo.normalizedTime >= 1)
49         {
50             fullPathHash = stateInfo.fullPathHash;
51             if (fullPathHash == dyingStateHash)
52             {
53                 animator.SetBool(isDyingPara, false);
54             }
55         }
56     }
57 
58     public void SetDying(bool state)
59     {
60         if (animator)
61         {
62             animator.SetBool(isDyingPara, state);
63         }
64     }
65 
66     public void ActionCallBack(string s)
67     {
68         if (s == "dyingStart")
69         {
70             Debug.Log("111");
71         }
72         else if (s == "dying")
73         {
74             Debug.LogWarning("222");
75         }
76         else if (s == "dyingEnd")
77         {
78             Debug.LogError("333");
79         }
80     }
81 }

 

3.输出如下

posted on 2019-02-13 21:37  艰苦奋斗中 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lyh916/p/10371947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值