// Animancer // Copyright 2020 Kybernetik // #pragma warning disable CS0618 // Type or member is obsolete (for ControllerStates in Animancer Lite). using System.Collections.Generic; using UnityEngine; using UnityEngine.Playables; namespace Animancer { /// [Pro-Only] /// A which plays a main with the /// ability to play other individual s separately. /// [AddComponentMenu(Strings.MenuPrefix + "Hybrid Animancer Component")] [HelpURL(Strings.APIDocumentationURL + "/HybridAnimancerComponent")] public class HybridAnimancerComponent : NamedAnimancerComponent { /************************************************************************************************************************/ #region Fields and Properties /************************************************************************************************************************/ [SerializeField, Tooltip("The main Animator Controller that this object will play")] private ControllerState.Transition _Controller; /// [] /// The main that this object plays. /// public ControllerState.Transition Controller { get { return _Controller; } set { _Controller = value; } } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Initialisation /************************************************************************************************************************/ #if UNITY_EDITOR /// [Editor-Only] /// Called by the Unity Editor when this component is first added (in Edit Mode) and whenever the Reset command /// is executed from its context menu. /// /// Sets = false by default so that will play the /// instead of the first animation in the /// array. /// protected override void Reset() { // The base.Animator property is not assigned yet and we need it before base.Reset clears the controller. var animator = Editor.AnimancerEditorUtilities.GetComponentInHierarchy(gameObject); if (animator != null && animator.runtimeAnimatorController != null) Controller = animator.runtimeAnimatorController; base.Reset(); PlayAutomatically = false; } #endif /************************************************************************************************************************/ /// /// Called by Unity when this component becomes enabled and active. /// /// Plays the if is false (otherwise it plays the /// first animation in the array). /// protected override void OnEnable() { PlayController(); base.OnEnable(); } /************************************************************************************************************************/ /// [] /// Gathers all the animations in the and the array. /// public override void GatherAnimationClips(ICollection clips) { base.GatherAnimationClips(clips); if (_Controller != null && _Controller.Controller != null) clips.Gather(_Controller.Controller.animationClips); } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Animator Controller Wrappers /************************************************************************************************************************/ /// /// Transitions to the over its specified /// . /// /// Returns the . /// public ControllerState PlayController() { if (_Controller == null || _Controller.Controller == null) return null; // Don't just return the result of Transition because it is an AnimancerState which we would need to cast. Play(_Controller); return _Controller.State; } /************************************************************************************************************************/ #region Cross Fade /************************************************************************************************************************/ /// /// Starts a transition from the current state to the specified state using normalized times. /// public void CrossFade(int stateNameHash, float transitionDuration = AnimancerPlayable.DefaultFadeDuration, int layer = -1, float normalizedTime = float.NegativeInfinity) { var controllerState = PlayController(); controllerState.Playable.CrossFade(stateNameHash, transitionDuration, layer, normalizedTime); } /************************************************************************************************************************/ /// /// Starts a transition from the current state to the specified state using normalized times. /// public AnimancerState CrossFade(string stateName, float transitionDuration = AnimancerPlayable.DefaultFadeDuration, int layer = -1, float normalizedTime = float.NegativeInfinity) { AnimancerState state; if (States.TryGet(name, out state)) { Play(state, transitionDuration); if (layer >= 0) state.LayerIndex = layer; state.NormalizedTime = normalizedTime; return state; } else { var controllerState = PlayController(); controllerState.Playable.CrossFade(stateName, transitionDuration, layer, normalizedTime); return controllerState; } } /************************************************************************************************************************/ /// /// Starts a transition from the current state to the specified state using times in seconds. /// public void CrossFadeInFixedTime(int stateNameHash, float transitionDuration = AnimancerPlayable.DefaultFadeDuration, int layer = -1, float fixedTime = 0) { var controllerState = PlayController(); controllerState.Playable.CrossFadeInFixedTime(stateNameHash, transitionDuration, layer, fixedTime); } /************************************************************************************************************************/ /// /// Starts a transition from the current state to the specified state using times in seconds. /// public AnimancerState CrossFadeInFixedTime(string stateName, float transitionDuration = AnimancerPlayable.DefaultFadeDuration, int layer = -1, float fixedTime = 0) { AnimancerState state; if (States.TryGet(name, out state)) { Play(state, transitionDuration); if (layer >= 0) state.LayerIndex = layer; state.Time = fixedTime; return state; } else { var controllerState = PlayController(); controllerState.Playable.CrossFadeInFixedTime(stateName, transitionDuration, layer, fixedTime); return controllerState; } } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Play /************************************************************************************************************************/ /// /// Plays the specified state immediately, starting from a particular normalized time. /// public void Play(int stateNameHash, int layer = -1, float normalizedTime = float.NegativeInfinity) { var controllerState = PlayController(); controllerState.Playable.Play(stateNameHash, layer, normalizedTime); } /************************************************************************************************************************/ /// /// Plays the specified state immediately, starting from a particular normalized time. /// public AnimancerState Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity) { AnimancerState state; if (States.TryGet(name, out state)) { Play(state); if (layer >= 0) state.LayerIndex = layer; state.NormalizedTime = normalizedTime; return state; } else { var controllerState = PlayController(); controllerState.Playable.Play(stateName, layer, normalizedTime); return controllerState; } } /************************************************************************************************************************/ /// /// Plays the specified state immediately, starting from a particular time (in seconds). /// public void PlayInFixedTime(int stateNameHash, int layer = -1, float fixedTime = 0) { var controllerState = PlayController(); controllerState.Playable.PlayInFixedTime(stateNameHash, layer, fixedTime); } /************************************************************************************************************************/ /// /// Plays the specified state immediately, starting from a particular time (in seconds). /// public AnimancerState PlayInFixedTime(string stateName, int layer = -1, float fixedTime = 0) { AnimancerState state; if (States.TryGet(name, out state)) { Play(state); if (layer >= 0) state.LayerIndex = layer; state.Time = fixedTime; return state; } else { var controllerState = PlayController(); controllerState.Playable.PlayInFixedTime(stateName, layer, fixedTime); return controllerState; } } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Parameters /************************************************************************************************************************/ /// Gets the value of the specified boolean parameter. public bool GetBool(int id) { return _Controller.State.Playable.GetBool(id); } /// Gets the value of the specified boolean parameter. public bool GetBool(string name) { return _Controller.State.Playable.GetBool(name); } /// Sets the value of the specified boolean parameter. public void SetBool(int id, bool value) { _Controller.State.Playable.SetBool(id, value); } /// Sets the value of the specified boolean parameter. public void SetBool(string name, bool value) { _Controller.State.Playable.SetBool(name, value); } /// Gets the value of the specified float parameter. public float GetFloat(int id) { return _Controller.State.Playable.GetFloat(id); } /// Gets the value of the specified float parameter. public float GetFloat(string name) { return _Controller.State.Playable.GetFloat(name); } /// Sets the value of the specified float parameter. public void SetFloat(int id, float value) { _Controller.State.Playable.SetFloat(id, value); } /// Sets the value of the specified float parameter. public void SetFloat(string name, float value) { _Controller.State.Playable.SetFloat(name, value); } /// Gets the value of the specified integer parameter. public int GetInteger(int id) { return _Controller.State.Playable.GetInteger(id); } /// Gets the value of the specified integer parameter. public int GetInteger(string name) { return _Controller.State.Playable.GetInteger(name); } /// Sets the value of the specified integer parameter. public void SetInteger(int id, int value) { _Controller.State.Playable.SetInteger(id, value); } /// Sets the value of the specified integer parameter. public void SetInteger(string name, int value) { _Controller.State.Playable.SetInteger(name, value); } /// Sets the specified trigger parameter to true. public void SetTrigger(int id) { _Controller.State.Playable.SetTrigger(id); } /// Sets the specified trigger parameter to true. public void SetTrigger(string name) { _Controller.State.Playable.SetTrigger(name); } /// Resets the specified trigger parameter to false. public void ResetTrigger(int id) { _Controller.State.Playable.ResetTrigger(id); } /// Resets the specified trigger parameter to false. public void ResetTrigger(string name) { _Controller.State.Playable.ResetTrigger(name); } /// Gets the details of one of the 's parameters. public AnimatorControllerParameter GetParameter(int index) { return _Controller.State.Playable.GetParameter(index); } /// Gets the number of parameters in the . public int GetParameterCount() { return _Controller.State.Playable.GetParameterCount(); } /// Indicates whether the specified parameter is controlled by an . public bool IsParameterControlledByCurve(int id) { return _Controller.State.Playable.IsParameterControlledByCurve(id); } /// Indicates whether the specified parameter is controlled by an . public bool IsParameterControlledByCurve(string name) { return _Controller.State.Playable.IsParameterControlledByCurve(name); } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #region Misc /************************************************************************************************************************/ // Layers. /************************************************************************************************************************/ /// Gets the weight of the layer at the specified index. public float GetLayerWeight(int layerIndex) { return _Controller.State.Playable.GetLayerWeight(layerIndex); } /// Sets the weight of the layer at the specified index. public void SetLayerWeight(int layerIndex, float weight) { _Controller.State.Playable.SetLayerWeight(layerIndex, weight); } /// Gets the number of layers in the . public int GetLayerCount() { return _Controller.State.Playable.GetLayerCount(); } /// Gets the index of the layer with the specified name. public int GetLayerIndex(string layerName) { return _Controller.State.Playable.GetLayerIndex(layerName); } /// Gets the name of the layer with the specified index. public string GetLayerName(int layerIndex) { return _Controller.State.Playable.GetLayerName(layerIndex); } /************************************************************************************************************************/ // States. /************************************************************************************************************************/ /// Returns information about the current state. public AnimatorStateInfo GetCurrentAnimatorStateInfo(int layerIndex = 0) { return _Controller.State.Playable.GetCurrentAnimatorStateInfo(layerIndex); } /// Returns information about the next state being transitioned towards. public AnimatorStateInfo GetNextAnimatorStateInfo(int layerIndex = 0) { return _Controller.State.Playable.GetNextAnimatorStateInfo(layerIndex); } /// Indicates whether the specified layer contains the specified state. public bool HasState(int layerIndex, int stateID) { return _Controller.State.Playable.HasState(layerIndex, stateID); } /************************************************************************************************************************/ // Transitions. /************************************************************************************************************************/ /// Indicates whether the specified layer is currently executing a transition. public bool IsInTransition(int layerIndex = 0) { return _Controller.State.Playable.IsInTransition(layerIndex); } /// Gets information about the current transition. public AnimatorTransitionInfo GetAnimatorTransitionInfo(int layerIndex = 0) { return _Controller.State.Playable.GetAnimatorTransitionInfo(layerIndex); } /************************************************************************************************************************/ // Clips. /************************************************************************************************************************/ /// Gets information about the s currently being played. public AnimatorClipInfo[] GetCurrentAnimatorClipInfo(int layerIndex = 0) { return _Controller.State.Playable.GetCurrentAnimatorClipInfo(layerIndex); } /// Gets information about the s currently being played. public void GetCurrentAnimatorClipInfo(int layerIndex, List clips) { _Controller.State.Playable.GetCurrentAnimatorClipInfo(layerIndex, clips); } /// Gets the number of s currently being played. public int GetCurrentAnimatorClipInfoCount(int layerIndex = 0) { return _Controller.State.Playable.GetCurrentAnimatorClipInfoCount(layerIndex); } /// Gets information about the s currently being transitioned towards. public AnimatorClipInfo[] GetNextAnimatorClipInfo(int layerIndex = 0) { return _Controller.State.Playable.GetNextAnimatorClipInfo(layerIndex); } /// Gets information about the s currently being transitioned towards. public void GetNextAnimatorClipInfo(int layerIndex, List clips) { _Controller.State.Playable.GetNextAnimatorClipInfo(layerIndex, clips); } /// Gets the number of s currently being transitioned towards. public int GetNextAnimatorClipInfoCount(int layerIndex = 0) { return _Controller.State.Playable.GetNextAnimatorClipInfoCount(layerIndex); } /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ #endregion /************************************************************************************************************************/ } }