123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using UnityEngine;
- namespace ET.Client
- {
- [FriendOf(typeof(AnimatorComponent))]
- public static class AnimatorComponentSystem
- {
- [ObjectSystem]
- public class AnimatorComponentAwakeSystem : AwakeSystem<AnimatorComponent>
- {
- protected override void Awake(AnimatorComponent self)
- {
- self.Awake();
- }
- }
- [ObjectSystem]
- public class AnimatorComponentUpdateSystem : UpdateSystem<AnimatorComponent>
- {
- protected override void Update(AnimatorComponent self)
- {
- self.Update();
- }
- }
-
- [ObjectSystem]
- public class AnimatorComponentDestroySystem : DestroySystem<AnimatorComponent>
- {
- protected override void Destroy(AnimatorComponent self)
- {
- self.animationClips = null;
- self.Parameter = null;
- self.Animator = null;
- }
- }
-
- public static void Awake(this AnimatorComponent self)
- {
- Animator animator = self.GetParent<Unit>().GetComponent<GameObjectComponent>().GameObject.GetComponent<Animator>();
- if (animator == null)
- {
- return;
- }
- if (animator.runtimeAnimatorController == null)
- {
- return;
- }
- if (animator.runtimeAnimatorController.animationClips == null)
- {
- return;
- }
- self.Animator = animator;
- foreach (AnimationClip animationClip in animator.runtimeAnimatorController.animationClips)
- {
- self.animationClips[animationClip.name] = animationClip;
- }
- foreach (AnimatorControllerParameter animatorControllerParameter in animator.parameters)
- {
- self.Parameter.Add(animatorControllerParameter.name);
- }
- }
- public static void Update(this AnimatorComponent self)
- {
- if (self.isStop)
- {
- return;
- }
- if (self.MotionType == MotionType.None)
- {
- return;
- }
- try
- {
- self.Animator.SetFloat("MotionSpeed", self.MontionSpeed);
- self.Animator.SetTrigger(self.MotionType.ToString());
- self.MontionSpeed = 1;
- self.MotionType = MotionType.None;
- }
- catch (Exception ex)
- {
- throw new Exception($"动作播放失败: {self.MotionType}", ex);
- }
- }
- public static bool HasParameter(this AnimatorComponent self, string parameter)
- {
- return self.Parameter.Contains(parameter);
- }
- public static void PlayInTime(this AnimatorComponent self, MotionType motionType, float time)
- {
- AnimationClip animationClip;
- if (!self.animationClips.TryGetValue(motionType.ToString(), out animationClip))
- {
- throw new Exception($"找不到该动作: {motionType}");
- }
- float motionSpeed = animationClip.length / time;
- if (motionSpeed < 0.01f || motionSpeed > 1000f)
- {
- Log.Error($"motionSpeed数值异常, {motionSpeed}, 此动作跳过");
- return;
- }
- self.MotionType = motionType;
- self.MontionSpeed = motionSpeed;
- }
- public static void Play(this AnimatorComponent self, MotionType motionType, float motionSpeed = 1f)
- {
- if (!self.HasParameter(motionType.ToString()))
- {
- return;
- }
- self.MotionType = motionType;
- self.MontionSpeed = motionSpeed;
- }
- public static float AnimationTime(this AnimatorComponent self, MotionType motionType)
- {
- AnimationClip animationClip;
- if (!self.animationClips.TryGetValue(motionType.ToString(), out animationClip))
- {
- throw new Exception($"找不到该动作: {motionType}");
- }
- return animationClip.length;
- }
- public static void PauseAnimator(this AnimatorComponent self)
- {
- if (self.isStop)
- {
- return;
- }
- self.isStop = true;
- if (self.Animator == null)
- {
- return;
- }
- self.stopSpeed = self.Animator.speed;
- self.Animator.speed = 0;
- }
- public static void RunAnimator(this AnimatorComponent self)
- {
- if (!self.isStop)
- {
- return;
- }
- self.isStop = false;
- if (self.Animator == null)
- {
- return;
- }
- self.Animator.speed = self.stopSpeed;
- }
- public static void SetBoolValue(this AnimatorComponent self, string name, bool state)
- {
- if (!self.HasParameter(name))
- {
- return;
- }
- self.Animator.SetBool(name, state);
- }
- public static void SetFloatValue(this AnimatorComponent self, string name, float state)
- {
- if (!self.HasParameter(name))
- {
- return;
- }
- self.Animator.SetFloat(name, state);
- }
- public static void SetIntValue(this AnimatorComponent self, string name, int value)
- {
- if (!self.HasParameter(name))
- {
- return;
- }
- self.Animator.SetInteger(name, value);
- }
- public static void SetTrigger(this AnimatorComponent self, string name)
- {
- if (!self.HasParameter(name))
- {
- return;
- }
- self.Animator.SetTrigger(name);
- }
- public static void SetAnimatorSpeed(this AnimatorComponent self, float speed)
- {
- self.stopSpeed = self.Animator.speed;
- self.Animator.speed = speed;
- }
- public static void ResetAnimatorSpeed(this AnimatorComponent self)
- {
- self.Animator.speed = self.stopSpeed;
- }
- }
- }
|