123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411 |
- using System.Collections.Generic;
- using UnityEngine;
- namespace Animancer
- {
-
-
-
-
-
-
-
-
-
- [AddComponentMenu(Strings.MenuPrefix + "Hybrid Animancer Component")]
- [HelpURL(Strings.DocsURLs.APIDocumentation + "/" + nameof(HybridAnimancerComponent))]
- public class HybridAnimancerComponent : NamedAnimancerComponent
- {
-
- #region Fields and Properties
-
- [SerializeField, Tooltip("The main Animator Controller that this object will play")]
- private ControllerTransition _Controller;
-
-
-
- public ref ControllerTransition Controller => ref _Controller;
-
- #endregion
-
- #region Initialisation
-
- #if UNITY_EDITOR
-
-
-
-
-
-
-
-
-
- protected override void Reset()
- {
- base.Reset();
- if (Animator != null)
- {
- Controller = Animator.runtimeAnimatorController;
- Animator.runtimeAnimatorController = null;
- }
- PlayAutomatically = false;
- }
- #endif
-
-
-
-
-
- protected override void OnEnable()
- {
- PlayController();
- base.OnEnable();
- #if UNITY_ASSERTIONS
- if (Animator != null && Animator.runtimeAnimatorController != null)
- OptionalWarning.NativeControllerHybrid.Log($"An Animator Controller is assigned to the" +
- $" {nameof(Animator)} component while also using a {nameof(HybridAnimancerComponent)}." +
- $" Most likely only one of them is being used so the other should be removed." +
- $" See the documentation for more information: {Strings.DocsURLs.AnimatorControllers}", this);
- #endif
- }
-
-
- public override void GatherAnimationClips(ICollection<AnimationClip> clips)
- {
- base.GatherAnimationClips(clips);
- clips.GatherFromSource(_Controller);
- }
-
- #endregion
-
- #region Animator Controller Wrappers
-
-
-
-
-
-
-
- public ControllerState PlayController()
- {
- if (!_Controller.IsValid())
- return null;
-
- Play(_Controller);
- return _Controller.State;
- }
-
- #region Cross Fade
-
-
-
- public void CrossFade(
- int stateNameHash,
- float fadeDuration = ControllerState.DefaultFadeDuration,
- int layer = -1,
- float normalizedTime = float.NegativeInfinity)
- {
- fadeDuration = ControllerState.GetFadeDuration(fadeDuration);
- var controllerState = PlayController();
- controllerState.Playable.CrossFade(stateNameHash, fadeDuration, layer, normalizedTime);
- }
-
-
-
- public AnimancerState CrossFade(
- string stateName,
- float fadeDuration = ControllerState.DefaultFadeDuration,
- int layer = -1,
- float normalizedTime = float.NegativeInfinity)
- {
- fadeDuration = ControllerState.GetFadeDuration(fadeDuration);
- if (States.TryGet(name, out var state))
- {
- Play(state, fadeDuration);
- if (layer >= 0)
- state.LayerIndex = layer;
- if (normalizedTime != float.NegativeInfinity)
- state.NormalizedTime = normalizedTime;
- return state;
- }
- else
- {
- var controllerState = PlayController();
- controllerState.Playable.CrossFade(stateName, fadeDuration, layer, normalizedTime);
- return controllerState;
- }
- }
-
-
-
- public void CrossFadeInFixedTime(
- int stateNameHash,
- float fadeDuration = ControllerState.DefaultFadeDuration,
- int layer = -1,
- float fixedTime = 0)
- {
- fadeDuration = ControllerState.GetFadeDuration(fadeDuration);
- var controllerState = PlayController();
- controllerState.Playable.CrossFadeInFixedTime(stateNameHash, fadeDuration, layer, fixedTime);
- }
-
-
-
- public AnimancerState CrossFadeInFixedTime(
- string stateName,
- float fadeDuration = ControllerState.DefaultFadeDuration,
- int layer = -1,
- float fixedTime = 0)
- {
- fadeDuration = ControllerState.GetFadeDuration(fadeDuration);
- if (States.TryGet(name, out var state))
- {
- Play(state, fadeDuration);
- if (layer >= 0)
- state.LayerIndex = layer;
- state.Time = fixedTime;
- return state;
- }
- else
- {
- var controllerState = PlayController();
- controllerState.Playable.CrossFadeInFixedTime(stateName, fadeDuration, layer, fixedTime);
- return controllerState;
- }
- }
-
- #endregion
-
- #region Play
-
-
- public void Play(
- int stateNameHash,
- int layer = -1,
- float normalizedTime = float.NegativeInfinity)
- {
- var controllerState = PlayController();
- controllerState.Playable.Play(stateNameHash, layer, normalizedTime);
- }
-
-
- public AnimancerState Play(
- string stateName,
- int layer = -1,
- float normalizedTime = float.NegativeInfinity)
- {
- if (States.TryGet(name, out var state))
- {
- Play(state);
- if (layer >= 0)
- state.LayerIndex = layer;
- if (normalizedTime != float.NegativeInfinity)
- state.NormalizedTime = normalizedTime;
- return state;
- }
- else
- {
- var controllerState = PlayController();
- controllerState.Playable.Play(stateName, layer, normalizedTime);
- return controllerState;
- }
- }
-
-
- public void PlayInFixedTime(
- int stateNameHash,
- int layer = -1,
- float fixedTime = 0)
- {
- var controllerState = PlayController();
- controllerState.Playable.PlayInFixedTime(stateNameHash, layer, fixedTime);
- }
-
-
- public AnimancerState PlayInFixedTime(
- string stateName,
- int layer = -1,
- float fixedTime = 0)
- {
- if (States.TryGet(name, out var 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
-
-
- public bool GetBool(int id) => _Controller.State.Playable.GetBool(id);
-
- public bool GetBool(string name) => _Controller.State.Playable.GetBool(name);
-
- public void SetBool(int id, bool value) => _Controller.State.Playable.SetBool(id, value);
-
- public void SetBool(string name, bool value) => _Controller.State.Playable.SetBool(name, value);
-
- public float GetFloat(int id) => _Controller.State.Playable.GetFloat(id);
-
- public float GetFloat(string name) => _Controller.State.Playable.GetFloat(name);
-
- public void SetFloat(int id, float value) => _Controller.State.Playable.SetFloat(id, value);
-
- public void SetFloat(string name, float value) => _Controller.State.Playable.SetFloat(name, value);
-
- public int GetInteger(int id) => _Controller.State.Playable.GetInteger(id);
-
- public int GetInteger(string name) => _Controller.State.Playable.GetInteger(name);
-
- public void SetInteger(int id, int value) => _Controller.State.Playable.SetInteger(id, value);
-
- public void SetInteger(string name, int value) => _Controller.State.Playable.SetInteger(name, value);
-
- public void SetTrigger(int id) => _Controller.State.Playable.SetTrigger(id);
-
- public void SetTrigger(string name) => _Controller.State.Playable.SetTrigger(name);
-
- public void ResetTrigger(int id) => _Controller.State.Playable.ResetTrigger(id);
-
- public void ResetTrigger(string name) => _Controller.State.Playable.ResetTrigger(name);
-
- public AnimatorControllerParameter GetParameter(int index) => _Controller.State.Playable.GetParameter(index);
-
- public int GetParameterCount() => _Controller.State.Playable.GetParameterCount();
-
- public bool IsParameterControlledByCurve(int id) => _Controller.State.Playable.IsParameterControlledByCurve(id);
-
- public bool IsParameterControlledByCurve(string name) => _Controller.State.Playable.IsParameterControlledByCurve(name);
-
- #endregion
-
- #region Misc
-
-
-
-
- public float GetLayerWeight(int layerIndex) => _Controller.State.Playable.GetLayerWeight(layerIndex);
-
- public void SetLayerWeight(int layerIndex, float weight) => _Controller.State.Playable.SetLayerWeight(layerIndex, weight);
-
- public int GetLayerCount() => _Controller.State.Playable.GetLayerCount();
-
- public int GetLayerIndex(string layerName) => _Controller.State.Playable.GetLayerIndex(layerName);
-
- public string GetLayerName(int layerIndex) => _Controller.State.Playable.GetLayerName(layerIndex);
-
-
-
-
- public AnimatorStateInfo GetCurrentAnimatorStateInfo(int layerIndex = 0) => _Controller.State.Playable.GetCurrentAnimatorStateInfo(layerIndex);
-
- public AnimatorStateInfo GetNextAnimatorStateInfo(int layerIndex = 0) => _Controller.State.Playable.GetNextAnimatorStateInfo(layerIndex);
-
- public bool HasState(int layerIndex, int stateID) => _Controller.State.Playable.HasState(layerIndex, stateID);
-
-
-
-
- public bool IsInTransition(int layerIndex = 0) => _Controller.State.Playable.IsInTransition(layerIndex);
-
- public AnimatorTransitionInfo GetAnimatorTransitionInfo(int layerIndex = 0) => _Controller.State.Playable.GetAnimatorTransitionInfo(layerIndex);
-
-
-
-
- public AnimatorClipInfo[] GetCurrentAnimatorClipInfo(int layerIndex = 0) => _Controller.State.Playable.GetCurrentAnimatorClipInfo(layerIndex);
-
- public void GetCurrentAnimatorClipInfo(int layerIndex, List<AnimatorClipInfo> clips) => _Controller.State.Playable.GetCurrentAnimatorClipInfo(layerIndex, clips);
-
- public int GetCurrentAnimatorClipInfoCount(int layerIndex = 0) => _Controller.State.Playable.GetCurrentAnimatorClipInfoCount(layerIndex);
-
- public AnimatorClipInfo[] GetNextAnimatorClipInfo(int layerIndex = 0) => _Controller.State.Playable.GetNextAnimatorClipInfo(layerIndex);
-
- public void GetNextAnimatorClipInfo(int layerIndex, List<AnimatorClipInfo> clips) => _Controller.State.Playable.GetNextAnimatorClipInfo(layerIndex, clips);
-
- public int GetNextAnimatorClipInfoCount(int layerIndex = 0) => _Controller.State.Playable.GetNextAnimatorClipInfoCount(layerIndex);
-
- #endregion
-
- #endregion
-
- }
- }
|