BITFALL/Assets/BITKit/Unity/Scripts/Animator/UnityAnimatorStateInfo.cs

103 lines
3.1 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2023-11-15 23:54:54 +08:00
using System.Linq;
2023-06-08 14:09:50 +08:00
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
{
2023-11-15 23:54:54 +08:00
[SerializeReference, SubclassSelector] private References stateName;
[SerializeReference,SubclassSelector] private IReference[] variableNames;
2023-06-08 14:09:50 +08:00
public IAnimator animator;
2023-11-15 23:54:54 +08:00
private int _index;
2023-06-08 14:09:50 +08:00
public float duration;
2023-11-15 23:54:54 +08:00
[SerializeReference, SubclassSelector] private References[] variables;
public string StateName
{
get
{
try
{
return stateName;
}
catch (Exception e)
{
BIT4Log.Warning<UnityAnimatorStateInfo>(this);
throw;
}
}
}
public string[] VariableNames => variableNames.Select(x=>x.Get()).ToArray();
public string[] VariableAnimationNames => variables.Select(x=>x.Get()).ToArray();
2023-06-08 14:09:50 +08:00
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
2023-11-15 23:54:54 +08:00
_index = layerIndex;
2023-06-08 14:09:50 +08:00
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);
2023-11-15 23:54:54 +08:00
this.animator?.OnStateEnter(_index, stateName);
2023-06-08 14:09:50 +08:00
}
public override void OnStateMachineExit(Animator animator, int stateMachinePathHash)
{
EnsureCreation(animator);
2023-11-15 23:54:54 +08:00
this.animator?.OnStateExit(_index, stateName);
2023-06-08 14:09:50 +08:00
}
public void Play()
{
if (duration is 0)
{
animator.Play(stateName);
}
else
{
2023-11-15 23:54:54 +08:00
animator.CrossFade(stateName, duration, _index);
2023-06-08 14:09:50 +08:00
}
}
void EnsureCreation(Animator animator)
{
if (this.animator is null)
{
this.animator = animator.GetComponent<IAnimator>();
}
}
}
/* #if UNITY_EDITOR
[CustomEditor(typeof(UnityAnimatorStateInfo))]
public class UnityAnimatorStateInfoInspector : BITInspector<UnityAnimatorStateInfo>
{
public override VisualElement CreateInspectorGUI()
{
var stateName = root.Create<PropertyField>();
return root;
}
}
#endif */
}