BITKit/Src/Unity/Scripts/Entity/Components/Animator/EntityAnimator.cs

100 lines
3.5 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
2023-11-15 23:55:06 +08:00
using System.Net.Configuration;
2023-11-06 01:17:23 +08:00
using BITKit.Animations;
2023-06-05 19:57:17 +08:00
namespace BITKit.Entities
{
2023-11-30 00:25:43 +08:00
[ExecuteInEditMode]
2023-11-06 01:17:23 +08:00
public sealed class EntityAnimator : EntityBehavior
2023-06-05 19:57:17 +08:00
{
2023-11-30 00:25:43 +08:00
[SerializeField] private bool debug;
2023-11-06 01:17:23 +08:00
[SerializeField] private UnityAnimator[] animators;
2023-10-24 23:38:22 +08:00
[SerializeReference, SubclassSelector] private References[] animationKeyWords;
[SerializeReference, SubclassSelector] private References _rootVelocity;
[SerializeReference, SubclassSelector] private References[] boolParameters;
[SerializeReference, SubclassSelector] private References[] floatParameters;
private List<string> keyWords;
2023-11-15 23:55:06 +08:00
private ConstantHash[] _boolParameterHashes;
private ConstantHash[] _floatParameterHashes;
2023-06-05 19:57:17 +08:00
public override void OnAwake()
{
keyWords = animationKeyWords.Select(x => x.Get()).ToList();
2023-11-15 23:55:06 +08:00
_boolParameterHashes = boolParameters.Select(x =>new ConstantHash(x.Get())).ToArray();
_floatParameterHashes = floatParameters.Select(x =>new ConstantHash(x.Get())).ToArray();
2023-06-05 19:57:17 +08:00
}
public override void OnStart()
{
2023-11-06 01:17:23 +08:00
UnityEntity.AddListener<string>(Constant.Animation.Play, Play);
2023-06-05 19:57:17 +08:00
}
2023-08-23 01:59:26 +08:00
private void Play(string animationName)
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
if (enabled is false) return;
2023-11-30 00:25:43 +08:00
2023-08-23 01:59:26 +08:00
if (animationKeyWords.Length is 0 || keyWords.Contains(animationName))
2023-06-05 19:57:17 +08:00
{
2023-11-30 00:25:43 +08:00
if (debug)
BIT4Log.Log<EntityAnimator>($"{gameObject.name} playing {animationName}");
2023-06-05 19:57:17 +08:00
animators.ForEach(x =>
{
2023-08-23 01:59:26 +08:00
if (!x.isActiveAndEnabled) return;
2023-11-06 01:17:23 +08:00
x.Play(animationName);
2023-06-05 19:57:17 +08:00
});
2023-11-30 00:25:43 +08:00
return;
2023-06-05 19:57:17 +08:00
}
2023-11-30 00:25:43 +08:00
if (debug)
BIT4Log.Log<EntityAnimator>($"{gameObject.name} cancel play {animationName}");
2023-06-05 19:57:17 +08:00
}
2023-11-15 23:55:06 +08:00
2023-06-05 19:57:17 +08:00
public override void OnFixedUpdate(float deltaTime)
{
2023-11-15 23:55:06 +08:00
bool cacheBool;
float cacheFloat;
foreach (var boolPar in _boolParameterHashes)
2023-06-05 19:57:17 +08:00
{
2023-11-15 23:55:06 +08:00
foreach (var x in animators)
{
if (!x.isActiveAndEnabled) return;
UnityEntity.GetDirect<bool>(boolPar, out cacheBool);
x.animator.SetBool(boolPar.HashCode, cacheBool);
}
2023-06-05 19:57:17 +08:00
}
2023-11-15 23:55:06 +08:00
foreach (var floatPar in _floatParameterHashes)
2023-06-05 19:57:17 +08:00
{
2023-11-15 23:55:06 +08:00
foreach (var x in animators)
{
if (!x.isActiveAndEnabled) return;
UnityEntity.GetDirect<float>(floatPar, out cacheFloat);
x.animator.SetFloat(floatPar.HashCode, cacheFloat);
}
2023-06-05 19:57:17 +08:00
}
}
2023-08-23 01:59:26 +08:00
private void OnAnimatorMove()
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
if (enabled is false) return;
2023-11-30 00:25:43 +08:00
if (BITAppForUnity.IsPlaying is false)
{
foreach (var x in animators)
{
x.animator.ApplyBuiltinRootMotion();
}
return;
}
2023-11-06 01:17:23 +08:00
if (_rootVelocity is not null && UnityEntity is not null)
UnityEntity.Set(_rootVelocity, animators[0].animator.velocity);
2023-06-05 19:57:17 +08:00
}
2023-11-06 01:17:23 +08:00
2023-08-23 01:59:26 +08:00
// ReSharper disable once UnusedMember.Local
private void AnimationEvent(string eventName)
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
UnityEntity.Invoke(Constant.Animation.OnEvent, eventName);
2023-06-05 19:57:17 +08:00
}
}
}