AnimationEvent

来源:互联网 发布:知字是什么生肖 编辑:程序博客网 时间:2024/05/24 07:16
 

unity 代码添加AnimationEvent


经过测试只要Animator跟继承monoBehaviour的类A在同一个节点上,就可以注册类A中的public 方法,含0或1个参数(int,float,object,string),注意:只能是0或1个参数



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2.   
  3. class TAnimEvent:MonoBehaviour  
  4. {  
  5.     [SerializeField]  
  6.     private Animator mAnimator;  
  7.   
  8.     public bool abc;  
  9.   
  10.     void Start()  
  11.     {  
  12.         mAnimator = this.GetComponent<Animator>();  
  13.         RuntimeAnimatorController m_runtimeAnimatorController = this.GetComponent<Animator>().runtimeAnimatorController;  
  14.         AnimationEvent newEvent = new AnimationEvent();  
  15.         newEvent.functionName = "Print";  
  16.         newEvent.time = 0.0f;  
  17.         newEvent.intParameter = 9;  
  18.         //newEvent.floatParameter = 9.0f;  
  19.   
  20.   
  21.         AnimationClip[] clips = m_runtimeAnimatorController.animationClips;  
  22.         for (int i = 0; i < clips.Length; i++)  
  23.         {  
  24.             Debug.Log("Nafio -------!!");  
  25.             if (clips [i].name == "Fuck") {  
  26.                 Debug.Log("Nafio find TAnim!!");  
  27.                 m_runtimeAnimatorController.animationClips[i].AddEvent(newEvent);;  
  28.             }  
  29.         }  
  30.         mAnimator.Rebind();  
  31.     }  
  32.           
  33.     void Print(int a)  
  34.     {  
  35.         Debug.Log ("Nafio---PrintEvent!!!  a:"+a);  
  36.     }  
  37.   
  38.   
  39. }  



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. AnimatorStateInfo currentState = animator.GetCurrentAnimatorStateInfo(0);  
  2. if (currentState.nameHash == Animator.StringToHash("Base Layer.Idle"))  
  3. {  
  4.     if (!hasSet) {  
  5.         Action act = new Action();  
  6.         act.Att = this.transform;  
  7.         act.Def = this.transform;  
  8.           
  9.         AnimationEvent evt = new AnimationEvent();  
  10.         evt.time = 0;  
  11.         evt.functionName = "Test";  
  12.         evt.objectReferenceParameter = act as Object;  
  13.           
  14.         AnimationInfo[] info = animator.GetCurrentAnimationClipState(0);  
  15.   
  16.         foreach (AnimationInfo i in info) {  
  17.             i.clip.AddEvent(evt);  
  18.             hasSet = true;  
  19.         }  
  20.     }  
  21. }  


 

untiy AnimationEvent添加返回参数



[csharp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.      
  4. public class Try : MonoBehaviour {  
  5.      
  6.     public class Action : ScriptableObject  
  7.     {  
  8.         public Transform Att;  
  9.         public Transform Def;  
  10.     }  
  11.      
  12.     // Use this for initialization  
  13.     void Start () {  
  14.         Action act = new Action();  
  15.         act.Att = this.transform;  
  16.         act.Def = this.transform;  
  17.      
  18.         AnimationEvent evt = new AnimationEvent();  
  19.         evt.time = 0;  
  20.         evt.functionName = "Test";  
  21.         evt.objectReferenceParameter = act as Object;  
  22.         animation.GetClip("ani").AddEvent(evt);  
  23.      
  24.         Debug.Log(act.Att.gameObject.name);  
  25.     }     
  26.          
  27.     public void Test(Action obj) {  
  28.         Action act = obj as Action;  
  29.         Debug.Log(act.Att.gameObject.name);  
  30.     }  
  31. }  


貌似参数传多少个,类型为int float object都可以

http://blog.csdn.NET/damenhanter/article/details/52561673


0 0
原创粉丝点击