2023-06-08 14:09:50 +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
|
|
|
|
{
|
2023-10-24 23:37:59 +08:00
|
|
|
[Serializable]
|
2023-06-08 14:09:50 +08:00
|
|
|
public class AnimatorLayerInfo
|
|
|
|
{
|
|
|
|
public string stateName;
|
|
|
|
public string fullStateName;
|
2023-10-24 23:37:59 +08:00
|
|
|
internal string entryName;
|
2023-06-08 14:09:50 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 23:37:59 +08:00
|
|
|
|
|
|
|
[Header(Constant.Header.Settings)]
|
|
|
|
[SerializeField] private bool debug;
|
|
|
|
|
2023-06-08 14:09:50 +08:00
|
|
|
[Header(Constant.Header.Components)]
|
|
|
|
public Animator animator;
|
|
|
|
public AnimatorLayerInfo this[int index]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (layerInfos.Count <= index)
|
|
|
|
{
|
|
|
|
for (int i = layerInfos.Count - 1; i < index; i++)
|
|
|
|
{
|
|
|
|
layerInfos.Add(new());
|
|
|
|
}
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
|
|
|
if (index > layerInfos.Count - 1)
|
|
|
|
{
|
|
|
|
throw new Exception("Index out of range");
|
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
return layerInfos[index];
|
|
|
|
}
|
|
|
|
}
|
2023-10-24 23:37:59 +08:00
|
|
|
[SerializeField] private List<AnimatorLayerInfo> layerInfos = new();
|
2023-08-12 01:43:24 +08:00
|
|
|
[Header(Constant.Header.InternalVariables)]
|
|
|
|
private readonly Dictionary<string,UnityAnimatorStateInfo> _registryStates=new ();
|
2023-10-24 23:37:59 +08:00
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
{
|
|
|
|
animator.enabled = true;
|
|
|
|
}
|
|
|
|
private void OnDisable()
|
|
|
|
{
|
|
|
|
animator.enabled = false;
|
|
|
|
foreach (var x in layerInfos)
|
|
|
|
{
|
|
|
|
x.entryName = null;
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
public void CrossFade(string name, float duration, int index = 0, float normalizedTimeOffset = 0)
|
|
|
|
{
|
2023-10-20 19:31:12 +08:00
|
|
|
name = name.Replace(".", "_");
|
|
|
|
|
|
|
|
animator.CrossFade(name, duration, index, normalizedTimeOffset);
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
2023-06-08 14:09:50 +08:00
|
|
|
public void Play(string name, int index = -1, float normalizedTimeOffset = 0)
|
|
|
|
{
|
|
|
|
name = name.Replace(".", "_");
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
if (_registryStates.TryGetValue(name, out var stateInfo) && stateInfo.variables?.Length > 0)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-08-12 01:43:24 +08:00
|
|
|
name = stateInfo.variables.Random();
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
|
|
|
|
2023-08-12 01:43:24 +08:00
|
|
|
animator.Play(name, index, normalizedTimeOffset);
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
|
|
|
public void Play(string name)
|
|
|
|
{
|
|
|
|
Play(name,-1,0);
|
|
|
|
}
|
|
|
|
|
2023-06-08 14:09:50 +08:00
|
|
|
public void OnStateEnter(int index, string name)
|
|
|
|
{
|
2023-10-24 23:37:59 +08:00
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
BIT4Log.Log<UnityAnimator>($"OnEntry:{name}");
|
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
this[index].fullStateName = name;
|
|
|
|
foreach (var item in name.Split("."))
|
|
|
|
{
|
|
|
|
name = item;
|
|
|
|
break;
|
|
|
|
}
|
2023-10-24 23:37:59 +08:00
|
|
|
this[index].stateName = this[index].entryName = name;
|
2023-06-08 14:09:50 +08:00
|
|
|
this[index].OnStateEnter(name);
|
|
|
|
}
|
|
|
|
public void OnStateExit(int index, string name)
|
|
|
|
{
|
2023-10-24 23:37:59 +08:00
|
|
|
if (string.IsNullOrEmpty(this[index].entryName))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (debug)
|
|
|
|
{
|
|
|
|
BIT4Log.Log<UnityAnimator>($"OnExit:{name}");
|
|
|
|
}
|
2023-06-08 14:09:50 +08:00
|
|
|
this[index].OnStateExit(name);
|
|
|
|
}
|
|
|
|
public float3 GetRootVelocity()
|
|
|
|
{
|
|
|
|
return animator.velocity;
|
|
|
|
}
|
2023-08-12 01:43:24 +08:00
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
foreach (var x in animator.GetBehaviours<UnityAnimatorStateInfo>())
|
|
|
|
{
|
|
|
|
_registryStates.TryAdd(x.stateName, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-08-12 01:43:24 +08:00
|
|
|
for (var i = 0; i < animator.layerCount; i++)
|
2023-06-08 14:09:50 +08:00
|
|
|
{
|
2023-08-12 01:43:24 +08:00
|
|
|
this[i].currentState = animator.GetCurrentAnimatorStateInfo(i);
|
2023-06-08 14:09:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|