1
This commit is contained in:
81
Unity/Scripts/Animator/UnityAnimatorStateInfo.cs
Normal file
81
Unity/Scripts/Animator/UnityAnimatorStateInfo.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
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] public References stateName;
|
||||
public IAnimator animator;
|
||||
public int index;
|
||||
public float duration;
|
||||
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int 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<IAnimator>();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* #if UNITY_EDITOR
|
||||
[CustomEditor(typeof(UnityAnimatorStateInfo))]
|
||||
public class UnityAnimatorStateInfoInspector : BITInspector<UnityAnimatorStateInfo>
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
var stateName = root.Create<PropertyField>();
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif */
|
||||
}
|
Reference in New Issue
Block a user