lyh916

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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  艰苦奋斗中  阅读(515)  评论(0编辑  收藏  举报