更改文件架构
This commit is contained in:
9
Packages/Common~/Scripts/Animator/AnimationProperty.cs
Normal file
9
Packages/Common~/Scripts/Animator/AnimationProperty.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AnimationProperty : MonoBehaviour
|
||||
{
|
||||
public static implicit operator float(AnimationProperty self) => self.value;
|
||||
public float value;
|
||||
}
|
11
Packages/Common~/Scripts/Animator/AnimationProperty.cs.meta
Normal file
11
Packages/Common~/Scripts/Animator/AnimationProperty.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a970eb848545573428bd7acd46bdac3d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
14
Packages/Common~/Scripts/Animator/AnimatorHelper.cs
Normal file
14
Packages/Common~/Scripts/Animator/AnimatorHelper.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AnimatorHelper : MonoBehaviour
|
||||
{
|
||||
public Transform root;
|
||||
void OnAnimatorMove()
|
||||
{
|
||||
root?.SendMessage(nameof(OnAnimatorMove));
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Animator/AnimatorHelper.cs.meta
Normal file
11
Packages/Common~/Scripts/Animator/AnimatorHelper.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c77ffb9999903044809af0e80a9d4fc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
21
Packages/Common~/Scripts/Animator/AnimatorLookAt.cs
Normal file
21
Packages/Common~/Scripts/Animator/AnimatorLookAt.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AnimatorLookAt : MonoBehaviour
|
||||
{
|
||||
public Animator animator;
|
||||
public float weight;
|
||||
public float headWeight;
|
||||
public float bodyWeight;
|
||||
public float eyesWeight;
|
||||
public Transform target;
|
||||
public Vector3 offset;
|
||||
void OnAnimatorIK(int layerIndex)
|
||||
{
|
||||
animator.SetLookAtWeight(weight, headWeight, bodyWeight, eyesWeight);
|
||||
animator.SetLookAtPosition(target.rotation * offset + target.position);
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Animator/AnimatorLookAt.cs.meta
Normal file
11
Packages/Common~/Scripts/Animator/AnimatorLookAt.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 990a889ead6235645865a68b81fabf8c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
34
Packages/Common~/Scripts/Animator/AnimatorStateHelper.cs
Normal file
34
Packages/Common~/Scripts/Animator/AnimatorStateHelper.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
namespace BITKit
|
||||
{
|
||||
public class AnimatorStateHelper : StateMachineBehaviour
|
||||
{
|
||||
public string stateName;
|
||||
IGenericEvent<string> genericEvent;
|
||||
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
||||
{
|
||||
EnsureValid(animator);
|
||||
genericEvent?.Invoke<string>(Constant.Animation.OnPlay, stateName);
|
||||
}
|
||||
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
||||
{
|
||||
EnsureValid(animator);
|
||||
genericEvent?.Invoke<AnimatorStateInfo>(stateName, stateInfo);
|
||||
}
|
||||
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
||||
{
|
||||
EnsureValid(animator);
|
||||
genericEvent?.Invoke<string>(Constant.Animation.OnPlayEnd, stateName);
|
||||
genericEvent?.Invoke<AnimatorStateInfo>(stateName, new());
|
||||
}
|
||||
void EnsureValid(Animator animator)
|
||||
{
|
||||
if (genericEvent is null)
|
||||
{
|
||||
genericEvent = animator.GetComponent<IGenericEvent<string>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2947ebfac0dc0854397e325f85bca066
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
27
Packages/Common~/Scripts/Animator/SetAnimatorParameters.cs
Normal file
27
Packages/Common~/Scripts/Animator/SetAnimatorParameters.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading;
|
||||
namespace BITKit
|
||||
{
|
||||
public class SetAnimatorParameters : Provider<float>
|
||||
{
|
||||
public Animator animator;
|
||||
[SubclassSelector, SerializeReference] public References floatName;
|
||||
CancellationToken cancellationToken;
|
||||
void Start()
|
||||
{
|
||||
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
}
|
||||
public override float Get()
|
||||
{
|
||||
return animator.GetFloat(floatName);
|
||||
}
|
||||
public override async void Set(float t)
|
||||
{
|
||||
await UniTask.SwitchToMainThread(cancellationToken);
|
||||
animator?.SetFloat(floatName, t);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 186b0c933823eb4449e3c6d7cb6381ed
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
103
Packages/Common~/Scripts/Animator/UnityAnimator.cs
Normal file
103
Packages/Common~/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;
|
||||
}
|
||||
}
|
||||
}
|
11
Packages/Common~/Scripts/Animator/UnityAnimator.cs.meta
Normal file
11
Packages/Common~/Scripts/Animator/UnityAnimator.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a6ac57edc8fc02840a5887fc4ad996e1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
81
Packages/Common~/Scripts/Animator/UnityAnimatorStateInfo.cs
Normal file
81
Packages/Common~/Scripts/Animator/UnityAnimatorStateInfo.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.UIElements;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
#endif
|
||||
namespace BITKit.Animations
|
||||
{
|
||||
public class UnityAnimatorStateInfo : StateMachineBehaviour
|
||||
{
|
||||
[SerializeReference, SubclassSelector] public References stateName;
|
||||
public IAnimator animator;
|
||||
public int index;
|
||||
public float duration;
|
||||
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
||||
{
|
||||
EnsureCreation(animator);
|
||||
try
|
||||
{
|
||||
this.animator?.OnStateEnter(layerIndex, stateName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning(animator.name);
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
|
||||
{
|
||||
EnsureCreation(animator);
|
||||
this.animator?.OnStateExit(layerIndex, stateName);
|
||||
}
|
||||
public override void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
|
||||
{
|
||||
EnsureCreation(animator);
|
||||
this.animator?.OnStateEnter(index, stateName);
|
||||
}
|
||||
public override void OnStateMachineExit(Animator animator, int stateMachinePathHash)
|
||||
{
|
||||
EnsureCreation(animator);
|
||||
this.animator?.OnStateExit(index, stateName);
|
||||
}
|
||||
|
||||
public void Play()
|
||||
{
|
||||
if (duration is 0)
|
||||
{
|
||||
animator.Play(stateName);
|
||||
}
|
||||
else
|
||||
{
|
||||
animator.CrossFade(stateName, duration, index);
|
||||
}
|
||||
}
|
||||
void EnsureCreation(Animator animator)
|
||||
{
|
||||
if (this.animator is null)
|
||||
{
|
||||
this.animator = animator.GetComponent<IAnimator>();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* #if UNITY_EDITOR
|
||||
[CustomEditor(typeof(UnityAnimatorStateInfo))]
|
||||
public class UnityAnimatorStateInfoInspector : BITInspector<UnityAnimatorStateInfo>
|
||||
{
|
||||
public override VisualElement CreateInspectorGUI()
|
||||
{
|
||||
var stateName = root.Create<PropertyField>();
|
||||
return root;
|
||||
}
|
||||
}
|
||||
#endif */
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b8427dbe4b1369d4facb6106e0a37fe5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user