} // Uninitialize #endregion #region Play /// <summary> /// Plays animation without any blending. /// </summary> /// <remarks> /// If the animation is already playing, other animations will be stopped but the animation will not rewind to the beginning. /// If the animation is not set to be looping it will be stopped and rewinded after playing. /// </remarks> /// <param name="name">Animation name</param> public void Play(string name) { if (!animationStates.ContainsKey(name)) { throw new ArgumentException("Model Animation Component: the animation name does not exist."); } AnimationState animationState = animationStates[name]; AnimationPlayed modelAnimationPlayer = null; // Stop animations on this layer foreach (AnimationPlayed activeAnimation in activeAnimations) { if (activeAnimation.AnimationState.ModelAnimation.Name != name) { activeAnimation.ModelAnimationPlayer.Stop(); } else { modelAnimationPlayer = activeAnimation; } } activeAnimations.Clear(); // If the animation was not being played. if (modelAnimationPlayer == null) { // Create the new animation modelAnimationPlayer = new AnimationPlayed(animationState, AnimationManager.FetchModelAnimationPlayer()); modelAnimationPlayer.ModelAnimationPlayer.Play(animationState); } activeAnimations.Add(modelAnimationPlayer); } // Play
public void PlayAnimation(string animation) { AnimationPlayed?.Invoke(animation); this.lastPlayedAnimation = animation; Model.PlayAnimation(animation); // Debug.Log($"Playing animation \"{animation}\" on {name}'s actor."); }
} // Play #endregion #region Cross Fade /// <summary> /// Fades the animation with name animation in over a period of time seconds and fades other animations out. /// </summary> /// <remarks> /// If the animation is not set to be looping it will be stopped and rewinded after playing. /// </remarks> /// <param name="name">Animation name.</param> /// <param name="fadeLength">Fade length.</param> public void CrossFade(string name, float fadeLength = 0.3f) { if (!animationStates.ContainsKey(name)) { throw new ArgumentException("Model Animation Component: the animation name does not exist."); } AnimationPlayed animationPlayed = null; AnimationState animationState = animationStates[name]; // Stop animations on this layer. foreach (AnimationPlayed activeAnimation in activeAnimations) { if (activeAnimation.AnimationState.ModelAnimation.Name == name) { animationPlayed = activeAnimation; } } // If the animation was not being played. if (animationPlayed == null) { // Create the new animation. animationPlayed = new AnimationPlayed(animationState, AnimationManager.FetchModelAnimationPlayer()); animationPlayed.ModelAnimationPlayer.Play(animationState); } else { // I want to place the animation in the last place. activeAnimations.Remove(animationPlayed); } animationPlayed.FadeLenght = fadeLength; if (activeAnimations.Count > 0 && animationPlayed.ElapsedTime != 0) { animationPlayed.ModelAnimationPlayer.AnimationState.NormalizedTime = activeAnimations[activeAnimations.Count - 1].ModelAnimationPlayer.AnimationState.NormalizedTime; } activeAnimations.Add(animationPlayed); } // CrossFade