using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using BITKit; using UnityEngine.Animations; using UnityEngine.UIElements; #if UNITY_EDITOR using UnityEditor; using UnityEditor.UIElements; #endif namespace BITKit.Animations { public class UnityAnimatorStateInfo : StateMachineBehaviour { [SerializeReference, SubclassSelector] private References stateName; [SerializeReference,SubclassSelector] private IReference[] variableNames; public IAnimator animator; private int _index; public float duration; [SerializeReference, SubclassSelector] private References[] variables; public string StateName { get { try { return stateName; } catch (Exception e) { BIT4Log.Warning(this); throw; } } } public string[] VariableNames => variableNames.Select(x=>x.Get()).ToArray(); public string[] VariableAnimationNames => variables.Select(x=>x.Get()).ToArray(); public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { _index = layerIndex; EnsureCreation(animator); try { this.animator?.OnStateEnter(layerIndex, stateName); } catch (Exception e) { Debug.LogWarning(animator.name); Console.WriteLine(e); throw; } } public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { EnsureCreation(animator); this.animator?.OnStateExit(layerIndex, stateName); } public override void OnStateMachineEnter(Animator animator, int stateMachinePathHash) { EnsureCreation(animator); this.animator?.OnStateEnter(_index, stateName); } public override void OnStateMachineExit(Animator animator, int stateMachinePathHash) { EnsureCreation(animator); this.animator?.OnStateExit(_index, stateName); } public void Play() { if (duration is 0) { animator.Play(stateName); } else { animator.CrossFade(stateName, duration, _index); } } void EnsureCreation(Animator animator) { if (this.animator is null) { this.animator = animator.GetComponent(); } } } /* #if UNITY_EDITOR [CustomEditor(typeof(UnityAnimatorStateInfo))] public class UnityAnimatorStateInfoInspector : BITInspector { public override VisualElement CreateInspectorGUI() { var stateName = root.Create(); return root; } } #endif */ }