diff options
author | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-01-25 14:28:30 +0800 |
commit | 6eb915c129fc90c6f4c82ae097dd6ffad5239efc (patch) | |
tree | 7dd2be50edf41f36b60fac84696e731c13afe617 /Client/Assets/Scripts/XUtliPoolLib/XAnimator.cs |
+scripts
Diffstat (limited to 'Client/Assets/Scripts/XUtliPoolLib/XAnimator.cs')
-rw-r--r-- | Client/Assets/Scripts/XUtliPoolLib/XAnimator.cs | 643 |
1 files changed, 643 insertions, 0 deletions
diff --git a/Client/Assets/Scripts/XUtliPoolLib/XAnimator.cs b/Client/Assets/Scripts/XUtliPoolLib/XAnimator.cs new file mode 100644 index 00000000..507e9275 --- /dev/null +++ b/Client/Assets/Scripts/XUtliPoolLib/XAnimator.cs @@ -0,0 +1,643 @@ +using System;
+using System.Collections.Generic;
+using UnityEngine;
+
+namespace XUtliPoolLib
+{
+ public class XAnimator
+ {
+ public bool IsLoaded
+ {
+ get
+ {
+ return this.m_LoadStatus == 2;
+ }
+ }
+
+ private bool IsInLoadFinish
+ {
+ get
+ {
+ return this.m_LoadStatus == 1;
+ }
+ }
+
+ public float speed
+ {
+ get
+ {
+ return this.m_speed;
+ }
+ set
+ {
+ this.m_speed = value;
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ XAnimator.SyncSpeed(this);
+ }
+ else
+ {
+ this.SetCbFlag(XAnimator.ECallbackCmd.ESyncSpeed, true);
+ }
+ }
+ }
+
+ public bool enabled
+ {
+ get
+ {
+ return this.m_enable;
+ }
+ set
+ {
+ this.m_enable = value;
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ XAnimator.SyncEnable(this);
+ }
+ else
+ {
+ this.SetCbFlag(XAnimator.ECallbackCmd.ESyncEnable, true);
+ }
+ }
+ }
+
+ public string StateName
+ {
+ get
+ {
+ return this.m_stateName;
+ }
+ }
+
+ public AnimatorCullingMode cullingMode
+ {
+ set
+ {
+ this.m_cullingMode = value;
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ XAnimator.SyncAnimationCullingMode(this);
+ }
+ }
+ }
+
+ private short m_LoadStatus = 0;
+
+ public XGameObject xGameObject = null;
+
+ private Animator m_Ator = null;
+
+ private AnimatorOverrideController m_overrideController = null;
+
+ private AnimatorCullingMode m_cullingMode = (AnimatorCullingMode)1;
+
+ private bool m_delayPlay = false;
+
+ private bool m_crossFade = false;
+
+ private string m_stateName = "";
+
+ private int m_playLayer = 0;
+
+ private float m_normalizedTime = float.NegativeInfinity;
+
+ private float m_value = 0f;
+
+ private string m_triggerName = "";
+
+ private float m_speed = 1f;
+
+ private bool m_enable = true;
+
+ private Dictionary<string, float> m_FloatCache = null;
+
+ private int m_LoadFinishCbFlag = 0;
+
+ private static AnimLoadCallback SyncTriggerCmd = new AnimLoadCallback(XAnimator.SyncTrigger);
+
+ private static AnimLoadCallback SyncSetFloatCmd = new AnimLoadCallback(XAnimator.SyncSetFloat);
+
+ private static AnimLoadCallback SyncSpeedCmd = new AnimLoadCallback(XAnimator.SyncSpeed);
+
+ private static AnimLoadCallback SyncEnableCmd = new AnimLoadCallback(XAnimator.SyncEnable);
+
+ private static AnimLoadCallback[] loadCallbacks = null;
+
+ private Dictionary<string, XAnimator.AminInfo> m_Clips = new Dictionary<string, XAnimator.AminInfo>();
+
+ public static bool debug = false;
+
+ private enum ECallbackCmd
+ {
+ ESyncEnable = 1,
+ ESyncSpeed,
+ ESyncTrigger = 4,
+ ESyncSetFloat = 8
+ }
+
+ private struct AminInfo
+ {
+ public XAnimationClip xclip;
+
+ public string clipKey;
+
+ public string clipPath;
+ }
+
+ public XAnimator()
+ {
+ bool flag = XAnimator.loadCallbacks == null;
+ if (flag)
+ {
+ XAnimator.loadCallbacks = new AnimLoadCallback[]
+ {
+ XAnimator.SyncEnableCmd,
+ XAnimator.SyncSpeedCmd,
+ XAnimator.SyncTriggerCmd,
+ XAnimator.SyncSetFloatCmd
+ };
+ }
+ }
+
+ private void SetCbFlag(XAnimator.ECallbackCmd cmd, bool add)
+ {
+ int num = XFastEnumIntEqualityComparer<XAnimator.ECallbackCmd>.ToInt(cmd);
+ if (add)
+ {
+ this.m_LoadFinishCbFlag |= num;
+ }
+ else
+ {
+ this.m_LoadFinishCbFlag &= ~num;
+ }
+ }
+
+ private bool IsCbFlag(int index)
+ {
+ int num = 1 << index;
+ return (this.m_LoadFinishCbFlag & num) != 0;
+ }
+
+ private void CacheFloatValue(string name, float value)
+ {
+ bool flag = this.m_FloatCache == null;
+ if (flag)
+ {
+ this.m_FloatCache = DictionaryPool<string, float>.Get();
+ }
+ this.m_FloatCache[name] = value;
+ }
+
+ public void Init(GameObject gameObject)
+ {
+ this.m_LoadStatus = 1;
+ this.m_Ator = ((gameObject != null) ? gameObject.GetComponent<Animator>() : null);
+ bool flag = this.m_Ator != null;
+ if (flag)
+ {
+ this.m_Ator.enabled = true;
+ bool flag2 = this.m_Ator.runtimeAnimatorController is AnimatorOverrideController;
+ if (flag2)
+ {
+ this.m_overrideController = (this.m_Ator.runtimeAnimatorController as AnimatorOverrideController);
+ }
+ else
+ {
+ this.m_overrideController = new AnimatorOverrideController();
+ this.m_overrideController.runtimeAnimatorController = this.m_Ator.runtimeAnimatorController;
+ this.m_Ator.runtimeAnimatorController = this.m_overrideController;
+ }
+ foreach (KeyValuePair<string, XAnimator.AminInfo> keyValuePair in this.m_Clips)
+ {
+ XAnimator.AminInfo value = keyValuePair.Value;
+ bool flag3 = value.xclip != null;
+ if (flag3)
+ {
+ this.SyncOverrideAnim(value.clipKey, value.xclip);
+ }
+ }
+ this.m_Ator.cullingMode = 0;
+ this.m_Ator.Rebind();
+ for (int i = 0; i < XAnimator.loadCallbacks.Length; i++)
+ {
+ bool flag4 = this.IsCbFlag(i);
+ if (flag4)
+ {
+ AnimLoadCallback animLoadCallback = XAnimator.loadCallbacks[i];
+ animLoadCallback(this);
+ }
+ }
+ }
+ this.m_LoadFinishCbFlag = 0;
+ this.m_LoadStatus = 2;
+ }
+
+ public bool IsSame(Animator ator)
+ {
+ return this.m_Ator == ator;
+ }
+
+ public bool IsAnimStateValid()
+ {
+ bool flag = !this.IsLoaded;
+ bool result;
+ if (flag)
+ {
+ result = false;
+ }
+ else
+ {
+ bool flag2 = string.IsNullOrEmpty(this.m_stateName);
+ result = !flag2;
+ }
+ return result;
+ }
+
+ public void Reset()
+ {
+ bool flag = this.m_Ator != null;
+ if (flag)
+ {
+ this.m_Ator.cullingMode =(AnimatorCullingMode) 1;
+ this.m_Ator.enabled = false;
+ this.m_Ator = null;
+ }
+ bool flag2 = this.m_overrideController != null;
+ if (flag2)
+ {
+ foreach (AnimationClipPair animationClipPair in this.m_overrideController.clips)
+ {
+ bool flag3 = animationClipPair.overrideClip != null;
+ if (flag3)
+ {
+ this.m_overrideController[animationClipPair.originalClip.name] = null;
+ }
+ }
+ this.m_overrideController = null;
+ }
+ foreach (KeyValuePair<string, XAnimator.AminInfo> keyValuePair in this.m_Clips)
+ {
+ XAnimator.AminInfo value = keyValuePair.Value;
+ bool flag4 = value.xclip != null;
+ if (flag4)
+ {
+ XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource(value.clipPath, ".anim", value.xclip, false);
+ }
+ }
+ this.m_Clips.Clear();
+ this.m_cullingMode = (AnimatorCullingMode) 1;
+ this.m_delayPlay = false;
+ this.m_stateName = "";
+ this.m_value = -1f;
+ this.m_playLayer = -1;
+ this.m_normalizedTime = float.NegativeInfinity;
+ this.m_triggerName = "";
+ this.m_LoadStatus = 0;
+ this.m_LoadFinishCbFlag = 0;
+ this.xGameObject = null;
+ }
+
+ public void SetStateMachine(IAnimStateMachine stateMachine)
+ {
+ }
+
+ private void SyncOverrideAnim(string key, XAnimationClip xclip)
+ {
+ bool flag = this.m_Ator != null;
+ if (flag)
+ {
+ bool flag2 = XAnimator.debug;
+ if (flag2)
+ {
+ XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} OverrideAnim {1}", new object[]
+ {
+ this.xGameObject.Name,
+ key
+ });
+ }
+ this.m_overrideController[key] = ((xclip != null) ? xclip.clip : null);
+ }
+ }
+
+ public void OverrideAnim(string key, string clipPath, OverrideAnimCallback overrideAnim = null, bool forceOverride = false)
+ {
+ bool flag = clipPath == null;
+ if (flag)
+ {
+ clipPath = "";
+ }
+ bool flag2 = this.m_Ator == null && this.IsLoaded;
+ if (!flag2)
+ {
+ XAnimator.AminInfo aminInfo;
+ bool flag3 = this.m_Clips.TryGetValue(key, out aminInfo);
+ if (flag3)
+ {
+ bool flag4 = aminInfo.clipPath == clipPath;
+ if (flag4)
+ {
+ bool flag5 = forceOverride && this.IsLoaded;
+ if (flag5)
+ {
+ this.SyncOverrideAnim(key, aminInfo.xclip);
+ }
+ bool flag6 = overrideAnim != null;
+ if (flag6)
+ {
+ overrideAnim(aminInfo.xclip);
+ }
+ return;
+ }
+ bool flag7 = aminInfo.xclip != null;
+ if (flag7)
+ {
+ XSingleton<XResourceLoaderMgr>.singleton.UnSafeDestroyShareResource("", ".anim", aminInfo.xclip, false);
+ aminInfo.xclip = null;
+ }
+ }
+ else
+ {
+ aminInfo.clipKey = key;
+ }
+ aminInfo.clipPath = clipPath;
+ aminInfo.xclip = (string.IsNullOrEmpty(clipPath) ? null : XSingleton<XResourceLoaderMgr>.singleton.GetXAnimation(clipPath, true, false));
+ this.m_Clips[key] = aminInfo;
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ this.SyncOverrideAnim(key, aminInfo.xclip);
+ }
+ bool flag8 = overrideAnim != null;
+ if (flag8)
+ {
+ overrideAnim(aminInfo.xclip);
+ }
+ }
+ }
+
+ public void SetAnimLoadCallback(string key, OverrideAnimCallback overrideAnim)
+ {
+ bool flag = this.m_Ator == null && this.IsLoaded;
+ if (!flag)
+ {
+ bool flag2 = overrideAnim != null;
+ if (flag2)
+ {
+ XAnimator.AminInfo aminInfo;
+ bool flag3 = this.m_Clips.TryGetValue(key, out aminInfo);
+ if (flag3)
+ {
+ bool flag4 = aminInfo.xclip != null;
+ if (flag4)
+ {
+ overrideAnim(aminInfo.xclip);
+ }
+ }
+ }
+ }
+ }
+
+ public static void SyncAnimationCullingMode(XAnimator ator)
+ {
+ bool flag = ator != null && ator.m_Ator != null;
+ if (flag)
+ {
+ ator.m_Ator.cullingMode = ator.m_cullingMode;
+ }
+ }
+
+ private static void SyncTrigger(XAnimator ator)
+ {
+ bool flag = ator != null && ator.m_Ator != null && !string.IsNullOrEmpty(ator.m_triggerName);
+ if (flag)
+ {
+ bool flag2 = XAnimator.debug;
+ if (flag2)
+ {
+ XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} trigger {1}", new object[]
+ {
+ ator.xGameObject.Name,
+ ator.m_triggerName
+ });
+ }
+ ator.m_Ator.SetTrigger(ator.m_triggerName);
+ }
+ }
+
+ private static void SyncSetFloat(XAnimator ator)
+ {
+ bool flag = ator != null && ator.m_Ator != null;
+ if (flag)
+ {
+ bool flag2 = ator.m_FloatCache != null;
+ if (flag2)
+ {
+ Dictionary<string, float>.Enumerator enumerator = ator.m_FloatCache.GetEnumerator();
+ while (enumerator.MoveNext())
+ {
+ bool flag3 = XAnimator.debug;
+ KeyValuePair<string, float> keyValuePair;
+ if (flag3)
+ {
+ XDebug singleton = XSingleton<XDebug>.singleton;
+ string format = "GO:{0} SetFloat {1}";
+ object[] array = new object[2];
+ array[0] = ator.xGameObject.Name;
+ int num = 1;
+ keyValuePair = enumerator.Current;
+ array[num] = keyValuePair.Key;
+ singleton.AddWarningLog2(format, array);
+ }
+ Animator ator2 = ator.m_Ator;
+ keyValuePair = enumerator.Current;
+ string key = keyValuePair.Key;
+ keyValuePair = enumerator.Current;
+ ator2.SetFloat(key, keyValuePair.Value);
+ }
+ DictionaryPool<string, float>.Release(ator.m_FloatCache);
+ ator.m_FloatCache = null;
+ }
+ }
+ }
+
+ private static void SyncSpeed(XAnimator ator)
+ {
+ bool flag = ator != null && ator.m_Ator != null;
+ if (flag)
+ {
+ ator.m_Ator.speed = ator.m_speed;
+ }
+ }
+
+ private static void SyncEnable(XAnimator ator)
+ {
+ bool flag = ator != null && ator.m_Ator != null;
+ if (flag)
+ {
+ ator.m_Ator.enabled = ator.m_enable;
+ }
+ }
+
+ public static void Update(XAnimator ator)
+ {
+ XAnimator.SyncAnimationCullingMode(ator);
+ bool delayPlay = ator.m_delayPlay;
+ if (delayPlay)
+ {
+ ator.RealPlay();
+ }
+ }
+
+ public void RealPlay()
+ {
+ this.m_delayPlay = false;
+ bool flag = this.m_Ator != null;
+ if (flag)
+ {
+ bool crossFade = this.m_crossFade;
+ if (crossFade)
+ {
+ bool flag2 = XAnimator.debug;
+ if (flag2)
+ {
+ XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} crossFade {1} {2}", new object[]
+ {
+ this.xGameObject.Name,
+ this.m_stateName,
+ this.m_Ator.IsInTransition(0)
+ });
+ }
+ this.m_Ator.CrossFade(this.m_stateName, this.m_value, this.m_playLayer, this.m_normalizedTime);
+ }
+ else
+ {
+ bool flag3 = XAnimator.debug;
+ if (flag3)
+ {
+ XSingleton<XDebug>.singleton.AddWarningLog2("GO:{0} play {1}", new object[]
+ {
+ this.xGameObject.Name,
+ this.m_stateName
+ });
+ }
+ this.m_Ator.Play(this.m_stateName, this.m_playLayer, this.m_normalizedTime);
+ }
+ }
+ }
+
+ public void Play(string stateName, int layer, float normalizedTime)
+ {
+ this.m_stateName = stateName;
+ this.m_playLayer = layer;
+ this.m_normalizedTime = normalizedTime;
+ this.m_crossFade = false;
+ bool flag = this.IsAnimStateValid();
+ if (flag)
+ {
+ this.RealPlay();
+ }
+ else
+ {
+ this.m_delayPlay = true;
+ }
+ }
+
+ public void Play(string stateName, int layer)
+ {
+ this.m_stateName = stateName;
+ this.m_playLayer = layer;
+ this.m_normalizedTime = float.NegativeInfinity;
+ this.m_crossFade = false;
+ bool flag = this.IsAnimStateValid();
+ if (flag)
+ {
+ this.RealPlay();
+ }
+ else
+ {
+ this.m_delayPlay = true;
+ }
+ }
+
+ public void CrossFade(string stateName, float transitionDuration, int layer, float normalizedTime)
+ {
+ this.m_stateName = stateName;
+ this.m_value = transitionDuration;
+ this.m_playLayer = layer;
+ this.m_normalizedTime = normalizedTime;
+ this.m_crossFade = true;
+ bool flag = this.IsAnimStateValid();
+ if (flag)
+ {
+ this.RealPlay();
+ }
+ else
+ {
+ this.m_delayPlay = true;
+ }
+ }
+
+ public void SetTrigger(string name)
+ {
+ this.m_triggerName = name;
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ XAnimator.SyncTrigger(this);
+ }
+ else
+ {
+ this.SetCbFlag(XAnimator.ECallbackCmd.ESyncTrigger, true);
+ }
+ }
+
+ public void ResetTrigger()
+ {
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ XAnimator.SyncTrigger(this);
+ }
+ else
+ {
+ this.SetCbFlag(XAnimator.ECallbackCmd.ESyncTrigger, true);
+ }
+ }
+
+ public bool IsInTransition(int layerIndex)
+ {
+ return !(this.m_Ator == null) && this.m_Ator.IsInTransition(layerIndex);
+ }
+
+ public void SetFloat(string name, float value)
+ {
+ bool isLoaded = this.IsLoaded;
+ if (isLoaded)
+ {
+ bool flag = this.m_Ator != null;
+ if (flag)
+ {
+ this.m_Ator.SetFloat(name, value);
+ }
+ }
+ else
+ {
+ this.CacheFloatValue(name, value);
+ this.SetCbFlag(XAnimator.ECallbackCmd.ESyncSetFloat, true);
+ }
+ }
+
+ public void EnableRootMotion(bool enable)
+ {
+ bool flag = this.m_Ator != null;
+ if (flag)
+ {
+ this.m_Ator.applyRootMotion = enable;
+ }
+ }
+ }
+}
|