动画状态机--动画片段添加事件

动画状态机添加事件……

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);
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值