脚本控制animation的事件

由于动作设计经常修改动作,所以每次改完都要再添加一次animation的事件,所以就直接写了个脚本,当然以后可以做成表格,然后用脚本从表格中读取,然后生成对应的animation事件。在Assets/Editor目录中放置代码,参考代码如下:

using UnityEngine;

using System.Collections;

using UnityEditor;

public class AddEventsToAnimations : MonoBehaviour {

 [MenuItem("Assets/Demo2/Add animations events")]  //添加到editor的menu

 static void AddEvents()

 {

         float t = 0.03333f;

         int i = 0;

         string attack01str = "Assets/My Project/Raw Data/Character/Hero/Anis/Attack01.anim";



         const int nLen = 4;

         AnimationEvent[] arr = new AnimationEvent[nLen];  



//设置了3个点添加事件

  int[] attack01time = new int[nLen-1]{10,25,40};



//装载动画

  AnimationClip attack01clip =  AssetDatabase.LoadMainAssetAtPath(attack01str) as AnimationClip;

  if (attack01clip == null)

  {

   Debug.Log("attack 01 clip is null");

   return;

  }



//添加事件

  for (i = 0; i < attack01time.Length; i++)

  {

   AnimationEvent attack01 = new AnimationEvent();

      attack01.functionName = "NotifyAttack1";

      attack01.time = t * attack01time[i];

   arr[i] = attack01;

  }



//这里是添加结束事件,看自己是不是需要了

  AnimationEvent attack011 = new AnimationEvent();

     attack011.functionName = "NotifyAttackEnd";

     attack011.time = t * 70;

  attack011.stringParameter = "Attack01";

  arr[i] = attack011;  



//用editor函数设置此动画的事件

  AnimationUtility.SetAnimationEvents(attack01clip, arr); 

}

http://jiaowodouge.blog.163.com/blog/static/187245168201171142327625/

你可能感兴趣的:(animation)