1
This commit is contained in:
103
Unity/Scripts/Animator/UnityAnimator.cs
Normal file
103
Unity/Scripts/Animator/UnityAnimator.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
return layerInfos[index];
|
||||
}
|
||||
}
|
||||
public List<AnimatorLayerInfo> layerInfos = new();
|
||||
public void CrossFade(string name, float duration, int index = 0, float normalizedTimeOffset = 0)
|
||||
{
|
||||
animator.CrossFade(name, duration, index);
|
||||
}
|
||||
public void Play(string name, int index = -1, float normalizedTimeOffset = 0)
|
||||
{
|
||||
// Debug.Log(name);
|
||||
name = name.Replace(".", "_");
|
||||
|
||||
if (index is -1)
|
||||
{
|
||||
animator.Play(name, -1, normalizedTimeOffset);
|
||||
}
|
||||
else
|
||||
{
|
||||
animator.Play(name, index, normalizedTimeOffset);
|
||||
}
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
int index = 0;
|
||||
foreach (var x in layerInfos)
|
||||
{
|
||||
x.currentState = animator.GetCurrentAnimatorStateInfo(index++);
|
||||
}
|
||||
isMatchingTarget = animator.isMatchingTarget;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user