Ejemplo n.º 1
0
        /// <summary>
        /// Add an event to the specified clip with the method callback.
        /// </summary>
        /// <param name="controller">Animator controller.</param>
        /// <param name="clipName">Animation clip name to attach the <paramref name="callbackMethodName"/> callback.</param>
        /// <param name="callbackMethodName">Method must be on <see cref="GameObject"/> with <see cref="Animator"/> component attached.</param>
        /// <param name="firePosition">Sets where an animation callback will be fired on the animation clip timeline.
        /// Choose <see cref="ClipEventFirePosition.Custom"/> to set custom fire time otherwise <paramref name="fireTime"/> will be ignored.</param>
        /// <param name="fireTime">Event fire time. Will be set to the clip max length if exceeded.</param>
        /// <exception cref="ArgumentOutOfRangeException">when <see cref="ClipEventFirePosition"/> value is out of enum range.</exception>
        public static void BindRuntimeAnimationEventCallback(this RuntimeAnimatorController controller, string clipName, string callbackMethodName, ClipEventFirePosition firePosition, float fireTime = 0f)
        {
            var clip = controller.GetAnimationClip(clipName);

            switch (firePosition)
            {
            case ClipEventFirePosition.Custom:
            {
                if (fireTime > clip.length)
                {
                    fireTime = clip.length;
                }
                break;
            }

            case ClipEventFirePosition.AtStart:
            {
                fireTime = 0f;
                break;
            }

            case ClipEventFirePosition.AtEnd:
            {
                fireTime = clip.length;
                break;
            }

            case ClipEventFirePosition.AtMiddle:
            {
                fireTime = clip.length / 2;
                break;
            }

            default:
                throw new ArgumentOutOfRangeException(nameof(firePosition), firePosition, null);
            }

            var animationEvent = new AnimationEvent
            {
                functionName = callbackMethodName,
                time         = fireTime
            };

            clip.AddEvent(animationEvent);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Divide clip length by 4 and add 2 animation events: at the beginning and near the end.
        /// </summary>
        /// <note>Example: If clip is length of 40 then events will be added to 10 nd 30 frames.</note>
        /// <param name="controller">Animator controller.</param>
        /// <param name="clipName">Animation clip name to attach the <paramref name="callbackMethodName"/> callback.</param>
        /// <param name="callbackMethodName">Method must be on <see cref="GameObject"/> with <see cref="Animator"/> component attached.</param>
        public static void BindRuntimeAnimationFootStepEventCallback(this RuntimeAnimatorController controller, string clipName, string callbackMethodName)
        {
            var clip = controller.GetAnimationClip(clipName);

            var forthPart = clip.length / 4f;

            var firstStep = new AnimationEvent
            {
                functionName = callbackMethodName,
                time         = forthPart
            };

            var secondStep = new AnimationEvent
            {
                functionName = callbackMethodName,
                time         = forthPart + forthPart + forthPart
            };

            clip.AddEvent(firstStep);
            clip.AddEvent(secondStep);
        }