示例#1
0
        /// <summary>
        /// Starts fading in the `state` over the course of the `fadeDuration` while fading out all others in this
        /// layer. Returns the `state`.
        /// <para></para>
        /// If the `state` was already playing and fading in with less time remaining than the `fadeDuration`, this
        /// method will allow it to complete the existing fade rather than starting a slower one.
        /// <para></para>
        /// If the layer currently has 0 <see cref="AnimancerNode.Weight"/>, this method will fade in the layer itself
        /// and simply <see cref="AnimancerState.Play"/> the `state`.
        /// <para></para>
        /// Animancer Lite only allows the default `fadeDuration` (0.25 seconds) in a runtime build.
        /// </summary>
        public AnimancerState Play(AnimancerState state, float fadeDuration, FadeMode mode = FadeMode.FixedSpeed)
        {
            Validate.Root(state, Root);

            if (fadeDuration <= 0 ||         // With no fade duration, Play immediately.
                (Index == 0 && Weight == 0)) // First animation on Layer 0 snap Weight to 1.
            {
                return(Play(state));
            }

            bool isFixedDuration;

            EvaluateFadeMode(mode, ref state, ref fadeDuration, out isFixedDuration);

            StartFade(1, fadeDuration);
            if (Weight == 0)
            {
                return(Play(state));
            }

            AddChild(state);

            CurrentState = state;

            // If the state is already playing or will finish fading in faster than this new fade,
            // continue the existing fade but still pretend it was restarted.
            if (state.IsPlaying && state.TargetWeight == 1 &&
                (state.Weight == 1 || state.FadeSpeed * fadeDuration > Math.Abs(1 - state.Weight)))
            {
                OnStartFade();
            }
            // Otherwise fade in the target state and fade out all others.
            else
            {
                state.IsPlaying = true;
                state.StartFade(1, fadeDuration);

                var count = States.Count;
                for (int i = 0; i < count; i++)
                {
                    var otherState = States[i];
                    if (otherState != state)
                    {
                        otherState.StartFade(0, fadeDuration);
                    }
                }
            }

            return(state);
        }
示例#2
0
        /************************************************************************************************************************/

        /// <summary>
        /// Starts fading in the 'state' over the course of the 'fadeDuration' while fading out all others in this
        /// layer. Returns the 'state'.
        /// <para></para>
        /// If the 'state' was already playing, it will continue doing so from the current time, unlike
        /// <see cref="CrossFadeFromStart"/>.
        /// <para></para>
        /// If the 'state' was already playing and fading in with less time remaining than the 'fadeDuration', this
        /// method will allow it to complete the existing fade rather than starting a slower one.
        /// <para></para>
        /// If this layer currently has 0 <see cref="Weight"/>, this method will instead start fading in the layer
        /// itself and simply <see cref="Play"/> the 'state'.
        /// <para></para>
        /// Animancer Lite only allows the default 'fadeDuration' (0.3 seconds) in a runtime build.
        /// </summary>
        public AnimancerState CrossFade(AnimancerState state, float fadeDuration = AnimancerPlayable.DefaultFadeDuration)
        {
            ValidateState(state);

            CurrentState = state;

            if (Weight == 0)
            {
                return(Play(state));
            }

            // If the state is already playing or will finish fading in faster than this new fade,
            // continue the existing fade but still pretend it was restarted.
            if (state.IsPlaying && state.TargetWeight == 1 &&
                (state.Weight == 1 || state.FadeSpeed * fadeDuration > Mathf.Abs(1 - state.Weight)))
            {
                OnStartFade();
            }
            // Otherwise fade in the target state and fade out all others.
            else
            {
                state.IsPlaying = true;
                state.StartFade(1, fadeDuration);

                var count = States.Count;
                for (int i = 0; i < count; i++)
                {
                    var otherState = States[i];
                    if (otherState != state)
                    {
                        otherState.StartFade(0, fadeDuration);
                    }
                }
            }

            return(state);
        }