This commit is contained in:
CortexCore
2023-11-15 23:55:06 +08:00
parent 5446067f91
commit 70247f0242
82 changed files with 3271 additions and 579 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Cysharp.Threading.Tasks;
using System.Threading;
@@ -13,6 +14,7 @@ namespace BITKit.Animations
[Serializable]
public class AnimatorLayerInfo
{
public string LayerName;
public string stateName;
public string fullStateName;
internal string entryName;
@@ -42,7 +44,11 @@ namespace BITKit.Animations
{
for (int i = layerInfos.Count - 1; i < index; i++)
{
layerInfos.Add(new());
var newLayer = new AnimatorLayerInfo
{
LayerName = animator.GetLayerName(i+1)
};
layerInfos.Add(newLayer);
}
}
@@ -69,28 +75,18 @@ namespace BITKit.Animations
x.entryName = null;
}
}
public void CrossFade(string name, float duration, int index = 0, float normalizedTimeOffset = 0)
public void CrossFade(string animationName, float duration, int index = 0, float normalizedTimeOffset = 0)
{
name = name.Replace(".", "_");
animator.CrossFade(name, duration, index, normalizedTimeOffset);
animator.CrossFade(GetVariableName(animationName), duration, index, normalizedTimeOffset);
}
public void Play(string name, int index = -1, float normalizedTimeOffset = 0)
public void Play(string animationName, int index = -1, float normalizedTimeOffset = 0)
{
name = name.Replace(".", "_");
if (_registryStates.TryGetValue(name, out var stateInfo) && stateInfo.variables?.Length > 0)
if (debug)
{
name = stateInfo.variables.Random();
BIT4Log.Log<UnityAnimator>( $"{gameObject.name} Play:" + animationName);
}
animator.Play(name, index, normalizedTimeOffset);
}
public void Play(string name)
{
Play(name,-1,0);
animator.Play(GetVariableName(animationName), index, normalizedTimeOffset);
}
public void OnStateEnter(int index, string name)
@@ -129,7 +125,20 @@ namespace BITKit.Animations
{
foreach (var x in animator.GetBehaviours<UnityAnimatorStateInfo>())
{
_registryStates.TryAdd(x.stateName, x);
try
{
_registryStates.TryAdd(x.StateName, x);
foreach (var v in x.VariableNames)
{
_registryStates.TryAdd(v, x);
}
}
catch (Exception e)
{
BIT4Log.Warning<UnityAnimator>(gameObject.name);
throw;
}
}
}
@@ -140,5 +149,26 @@ namespace BITKit.Animations
this[i].currentState = animator.GetCurrentAnimatorStateInfo(i);
}
}
[ContextMenu(nameof(Diagnosis))]
private void Diagnosis()
{
}
private string GetVariableName(string animationName)
{
animationName = animationName.Replace(".", "_");
if (!_registryStates.TryGetValue(animationName, out var stateInfo)) return animationName;
animationName = stateInfo.StateName;
if (stateInfo.VariableAnimationNames.Length > 0)
{
animationName = stateInfo.VariableAnimationNames.Random();
}
return animationName;
}
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using BITKit;
using UnityEngine.Animations;
@@ -13,13 +14,33 @@ namespace BITKit.Animations
{
public class UnityAnimatorStateInfo : StateMachineBehaviour
{
[SerializeReference, SubclassSelector] public References stateName;
[SerializeReference, SubclassSelector] private References stateName;
[SerializeReference,SubclassSelector] private IReference[] variableNames;
public IAnimator animator;
public int index;
private int _index;
public float duration;
[SerializeReference, SubclassSelector] public References[] variables;
[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();
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
_index = layerIndex;
EnsureCreation(animator);
try
{
@@ -41,12 +62,12 @@ namespace BITKit.Animations
public override void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
{
EnsureCreation(animator);
this.animator?.OnStateEnter(index, stateName);
this.animator?.OnStateEnter(_index, stateName);
}
public override void OnStateMachineExit(Animator animator, int stateMachinePathHash)
{
EnsureCreation(animator);
this.animator?.OnStateExit(index, stateName);
this.animator?.OnStateExit(_index, stateName);
}
public void Play()
@@ -57,7 +78,7 @@ namespace BITKit.Animations
}
else
{
animator.CrossFade(stateName, duration, index);
animator.CrossFade(stateName, duration, _index);
}
}
void EnsureCreation(Animator animator)