MaidIdleMotionAdditiveHook.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [RequireComponent(typeof(Animation))]
  6. [RequireComponent(typeof(Animator))]
  7. public class MaidIdleMotionAdditiveHook : MonoBehaviour
  8. {
  9. public bool isAdditive
  10. {
  11. get
  12. {
  13. return this.isAdditive_;
  14. }
  15. set
  16. {
  17. this.isAdditive_ = value;
  18. if (this.isAdditive_)
  19. {
  20. if (this.StartAdditive(this.targetClip))
  21. {
  22. this.maidAnimation.enabled = false;
  23. this.animator.enabled = true;
  24. }
  25. else
  26. {
  27. this.isAdditive_ = false;
  28. }
  29. }
  30. else
  31. {
  32. this.animator.enabled = false;
  33. if (!this.maidAnimation.enabled)
  34. {
  35. this.maidAnimation.enabled = true;
  36. bool flag = false;
  37. IEnumerator enumerator = this.maidAnimation.GetEnumerator();
  38. try
  39. {
  40. while (enumerator.MoveNext())
  41. {
  42. object obj = enumerator.Current;
  43. AnimationState animationState = (AnimationState)obj;
  44. if (this.targetClip != null)
  45. {
  46. if (animationState != null && animationState.clip != this.targetClip)
  47. {
  48. animationState.time = 0f;
  49. flag = true;
  50. break;
  51. }
  52. }
  53. else if (animationState != null && animationState.clip != null && this.maidAnimation.IsPlaying(animationState.clip.name))
  54. {
  55. animationState.time = 0f;
  56. flag = true;
  57. break;
  58. }
  59. }
  60. }
  61. finally
  62. {
  63. IDisposable disposable;
  64. if ((disposable = (enumerator as IDisposable)) != null)
  65. {
  66. disposable.Dispose();
  67. }
  68. }
  69. if (flag)
  70. {
  71. this.maidAnimation.Sample();
  72. }
  73. }
  74. }
  75. }
  76. }
  77. public float additiveWeight
  78. {
  79. get
  80. {
  81. return this.animator.GetLayerWeight(1);
  82. }
  83. set
  84. {
  85. this.animator.SetLayerWeight(1, value);
  86. }
  87. }
  88. private void Awake()
  89. {
  90. this.maidAnimation = base.GetComponent<Animation>();
  91. this.animator = base.GetComponent<Animator>();
  92. if (this.animator == null)
  93. {
  94. this.animator = base.gameObject.AddComponent<Animator>();
  95. }
  96. this.animator.enabled = false;
  97. this.animator.runtimeAnimatorController = UnityEngine.Object.Instantiate<RuntimeAnimatorController>(Resources.Load<RuntimeAnimatorController>("ScenePhotoMode/Animation/MaidIdleAdditive"));
  98. this.overrideController = new AnimatorOverrideController(this.animator.runtimeAnimatorController);
  99. List<KeyValuePair<AnimationClip, AnimationClip>> list = new List<KeyValuePair<AnimationClip, AnimationClip>>(this.overrideController.overridesCount);
  100. this.overrideController.GetOverrides(list);
  101. list[0] = new KeyValuePair<AnimationClip, AnimationClip>(list[0].Key, null);
  102. this.overrideController.ApplyOverrides(list);
  103. this.animator.runtimeAnimatorController = this.overrideController;
  104. }
  105. private bool StartAdditive(AnimationClip clip = null)
  106. {
  107. if (clip == null)
  108. {
  109. IEnumerator enumerator = this.maidAnimation.GetEnumerator();
  110. try
  111. {
  112. while (enumerator.MoveNext())
  113. {
  114. object obj = enumerator.Current;
  115. AnimationState animationState = (AnimationState)obj;
  116. if (!(animationState == null) && !(animationState.clip == null))
  117. {
  118. if (this.maidAnimation.IsPlaying(animationState.clip.name))
  119. {
  120. clip = animationState.clip;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. finally
  127. {
  128. IDisposable disposable;
  129. if ((disposable = (enumerator as IDisposable)) != null)
  130. {
  131. disposable.Dispose();
  132. }
  133. }
  134. if (clip == null)
  135. {
  136. return false;
  137. }
  138. }
  139. AnimationClip animationClip = this.instanceClip;
  140. AnimationClip animationClip2 = UnityEngine.Object.Instantiate<AnimationClip>(clip);
  141. animationClip2.legacy = false;
  142. this.instanceClip = animationClip2;
  143. List<KeyValuePair<AnimationClip, AnimationClip>> list = new List<KeyValuePair<AnimationClip, AnimationClip>>(this.overrideController.overridesCount);
  144. this.overrideController.GetOverrides(list);
  145. list[0] = new KeyValuePair<AnimationClip, AnimationClip>(list[0].Key, animationClip2);
  146. this.overrideController.ApplyOverrides(list);
  147. this.animator.CrossFade(this.animator.GetCurrentAnimatorStateInfo(0).fullPathHash, 0f);
  148. if (animationClip != null)
  149. {
  150. UnityEngine.Object.DestroyImmediate(animationClip);
  151. }
  152. return true;
  153. }
  154. private void OnDestroy()
  155. {
  156. UnityEngine.Object.Destroy(this);
  157. if (!this.notAnimatorDestroy && this.animator != null)
  158. {
  159. this.animator.enabled = false;
  160. UnityEngine.Object.Destroy(this.animator);
  161. this.animator = null;
  162. }
  163. if (this.instanceClip != null)
  164. {
  165. UnityEngine.Object.Destroy(this.instanceClip);
  166. this.instanceClip = null;
  167. }
  168. }
  169. private const string IdleAdditiveAnimatorControllerPath = "ScenePhotoMode/Animation/MaidIdleAdditive";
  170. public AnimationClip targetClip;
  171. public bool notAnimatorDestroy;
  172. private bool isAdditive_;
  173. private Animator animator;
  174. private Animation maidAnimation;
  175. private AnimatorOverrideController overrideController;
  176. private AnimationClip instanceClip;
  177. }