动画状态机添加事件……
public class AnimalAnimatorController : MonoBehaviour {
public delegate void myAnimationEvent(Transform animalTsfm, string eventTag);
public static event myAnimationEvent onMyAnimationEventRecall;
public delegate void animStartEvent(Transform animalTsfm, string animationName);
public static event animStartEvent onAnimEventStartRecall;
public delegate void animEndEvent(Transform animalTsfm, string animationName);
public static event animEndEvent onAnimEventEndRecall;
private Animator animator;
public delegate void AnimalAnimatorCon(string diamondColliders);
public static event AnimalAnimatorCon animalColliders;
// Use this for initialization
void Start () {
animator = GetComponent<Animator>();
initTransforms();
}
private void initTransforms()
{
AnimatorOverrideController swapController = new AnimatorOverrideController();
swapController.runtimeAnimatorController = animator.runtimeAnimatorController;
AnimationClipPair[] clips = swapController.clips;
AnimationClip clip;
for (int i = 0; i != clips.Length; i++)
{
clip = clips[i].originalClip;
if (clip.name == "symbol")
{
clip.AddEvent(createEvent(0.02f * clip.length, SBUUDefine.EVENT_ANIMATION_HIGH_START_COMPLETE));
clip.AddEvent(createEvent(0.98f * clip.length, SBUUDefine.EVENT_ANIMATION_HIGH_END_COMPLETE));
}
if (clip.name == "eat")
{
clip.AddEvent(createEvent(0.5f * clip.length, SBUUDefine.EVENT_ANIMATION_SET_EATING_STATE_COMPLETE));
}
clip.AddEvent(createAnimEvent(0.2f * clip.length, clip.name, "start"));
clip.AddEvent(createAnimEvent(1 * clip.length, clip.name, "end"));
}
}
AnimationEvent createEvent(float firedoff_time, string eventTag)
{
AnimationEvent evt = new AnimationEvent();
evt.time = firedoff_time;
evt.stringParameter = eventTag;
evt.functionName = "animationEventRecall";
return evt;
}
AnimationEvent createAnimEvent(float firedoff_time, string animationName, string startOrEnd)
{
AnimationEvent evt = new AnimationEvent();
evt.time = firedoff_time;
evt.stringParameter = animationName;
if (startOrEnd == "start")
evt.functionName = "animEventStartRecall";
else if (startOrEnd == "end")
evt.functionName = "animEventEndRecall";
return evt;
}
private void animationEventRecall(string eventTag)
{
if (onMyAnimationEventRecall != null)
{
onMyAnimationEventRecall(transform, eventTag);
}
}
private void animEventStartRecall(string animationName)
{
if (onAnimEventStartRecall != null)
{
onAnimEventStartRecall(transform, animationName);
}
}
private void animEventEndRecall(string animationName)
{
if (onAnimEventEndRecall != null)
{
onAnimEventEndRecall(transform, animationName);
}
}
}