Exemplo n.º 1
0
    //获取动画(根据是否是重载的动画)
    public AnimationClip GetClip(UnitAnim anim, bool overrided)
    {
        for (int i = 0; i < clips.Count; i++)
        {
            if (clips[i].Key.name == anim.ToString())
            {
                //如果是返回重载动画,且有重载的动画
                if (overrided && clips[i].Value != null)
                {
                    return(clips[i].Value);
                }
                else
                {
                    return(clips[i].Key);
                }
            }
        }

        Debug.LogError("未能找到动画:" + anim.ToString());

        return(null);
    }
Exemplo n.º 2
0
    IEnumerator PlayAnimCor(UnitAnim anim, float preswingTime, float swingTime, float backswingTime,
                            Action onSwingStart, Action onSwingEnd, Action onPlayComplete)
    {
        AnimationClip clip = GetClip(anim, true);

        animator.Play(anim.ToString(), -1, 0f);

        //有事件帧分割动画
        if (clip.events.Length > 0)
        {
            //前摇
            if (preswingTime > 0)
            {
                float preswingSpeed = clip.events[0].time / preswingTime;

                animator.speed = preswingSpeed;

                yield return(new WaitForSeconds(preswingTime));
            }
            else
            {
                yield return(new WaitForSeconds(clip.events[0].time));
            }

            onSwingStart?.Invoke();

            //摇
            if (swingTime > 0)
            {
                float swingSpeed = (clip.events[1].time - clip.events[0].time) / swingTime;

                animator.speed = swingSpeed;

                yield return(new WaitForSeconds(swingTime));
            }
            else
            {
                yield return(new WaitForSeconds(clip.events[1].time - clip.events[0].time));
            }

            onSwingEnd?.Invoke();

            //后摇
            if (backswingTime > 0)
            {
                float backswingSpeed = (clip.length - clip.events[1].time) / backswingTime;

                animator.speed = backswingSpeed;

                yield return(new WaitForSeconds(backswingTime));
            }
            else
            {
                yield return(new WaitForSeconds(clip.length - clip.events[1].time));
            }
        }
        //没有事件帧,且设置了释放时间,则调整整个动画时间
        else if (swingTime > 0)
        {
            float swingSpeed = clip.length / swingTime;

            animator.speed = swingSpeed;

            yield return(new WaitForSeconds(swingTime));
        }

        //完成
        onPlayComplete?.Invoke();

        animator.speed = 1;
    }