BITKit/Packages/Runtime~/Unity/Scripts/Animator/UnityAnimator.cs

122 lines
3.6 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using Unity.Mathematics;
namespace BITKit.Animations
{
public class UnityAnimator : MonoBehaviour, IAnimator
{
[System.Serializable]
public record AnimatorPlayEvent
{
public string stateName;
public int index;
}
[System.Serializable]
public class AnimatorLayerInfo
{
public string stateName;
public string fullStateName;
public AnimatorStateInfo currentState = new();
public event Action<string> onStateEnter;
public event Action<string> onStateExit;
public void OnStateEnter(string name)
{
onStateEnter?.Invoke(name);
}
public void OnStateExit(string name)
{
onStateExit?.Invoke(name);
}
}
[Header(Constant.Header.Components)]
public Animator animator;
[Header(Constant.Header.State)]
public bool isMatchingTarget;
public AnimatorLayerInfo this[int index]
{
get
{
if (layerInfos.Count <= index)
{
for (int i = layerInfos.Count - 1; i < index; i++)
{
layerInfos.Add(new());
}
}
2023-07-17 10:23:47 +08:00
if (index > layerInfos.Count - 1)
{
throw new Exception("Index out of range");
}
2023-06-05 19:57:17 +08:00
return layerInfos[index];
}
}
public List<AnimatorLayerInfo> layerInfos = new();
2023-06-29 14:57:11 +08:00
[Header(Constant.Header.InternalVariables)]
private readonly Dictionary<string,UnityAnimatorStateInfo> _registryStates=new ();
2023-06-05 19:57:17 +08:00
public void CrossFade(string name, float duration, int index = 0, float normalizedTimeOffset = 0)
{
animator.CrossFade(name, duration, index);
}
2023-06-29 14:57:11 +08:00
2023-06-05 19:57:17 +08:00
public void Play(string name, int index = -1, float normalizedTimeOffset = 0)
{
name = name.Replace(".", "_");
2023-06-29 14:57:11 +08:00
if (_registryStates.TryGetValue(name, out var stateInfo) && stateInfo.variables?.Length > 0)
2023-06-05 19:57:17 +08:00
{
2023-06-29 14:57:11 +08:00
name = stateInfo.variables.Random();
2023-06-05 19:57:17 +08:00
}
2023-06-29 14:57:11 +08:00
animator.Play(name, index, normalizedTimeOffset);
}
public void Play(string name)
{
Play(name,-1,0);
2023-06-05 19:57:17 +08:00
}
2023-06-29 14:57:11 +08:00
2023-06-05 19:57:17 +08:00
public void OnStateEnter(int index, string name)
{
this[index].fullStateName = name;
foreach (var item in name.Split("."))
{
name = item;
break;
}
this[index].stateName = name;
this[index].OnStateEnter(name);
}
public void OnStateExit(int index, string name)
{
this[index].OnStateExit(name);
}
public float3 GetRootVelocity()
{
return animator.velocity;
}
2023-06-29 14:57:11 +08:00
private void Start()
{
foreach (var x in animator.GetBehaviours<UnityAnimatorStateInfo>())
{
_registryStates.TryAdd(x.stateName, x);
}
}
private void Update()
2023-06-05 19:57:17 +08:00
{
2023-07-17 10:23:47 +08:00
for (var i = 0; i < animator.layerCount; i++)
2023-06-05 19:57:17 +08:00
{
2023-07-17 10:23:47 +08:00
this[i].currentState = animator.GetCurrentAnimatorStateInfo(i);
2023-06-05 19:57:17 +08:00
}
isMatchingTarget = animator.isMatchingTarget;
}
}
}