UITweener.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using AnimationOrTween;
  4. using UnityEngine;
  5. public abstract class UITweener : MonoBehaviour
  6. {
  7. public float amountPerDelta
  8. {
  9. get
  10. {
  11. if (this.mDuration != this.duration)
  12. {
  13. this.mDuration = this.duration;
  14. this.mAmountPerDelta = Mathf.Abs((this.duration <= 0f) ? 1000f : (1f / this.duration)) * Mathf.Sign(this.mAmountPerDelta);
  15. }
  16. return this.mAmountPerDelta;
  17. }
  18. }
  19. public float tweenFactor
  20. {
  21. get
  22. {
  23. return this.mFactor;
  24. }
  25. set
  26. {
  27. this.mFactor = Mathf.Clamp01(value);
  28. }
  29. }
  30. public Direction direction
  31. {
  32. get
  33. {
  34. return (this.amountPerDelta >= 0f) ? Direction.Forward : Direction.Reverse;
  35. }
  36. }
  37. private void Reset()
  38. {
  39. if (!this.mStarted)
  40. {
  41. this.SetStartToCurrentValue();
  42. this.SetEndToCurrentValue();
  43. }
  44. }
  45. protected virtual void Start()
  46. {
  47. this.Update();
  48. }
  49. private void Update()
  50. {
  51. float num = (!this.ignoreTimeScale) ? Time.deltaTime : RealTime.deltaTime;
  52. float num2 = (!this.ignoreTimeScale) ? Time.time : RealTime.time;
  53. if (!this.mStarted)
  54. {
  55. this.mStarted = true;
  56. this.mStartTime = num2 + this.delay;
  57. }
  58. if (num2 < this.mStartTime)
  59. {
  60. return;
  61. }
  62. this.mFactor += this.amountPerDelta * num;
  63. if (this.style == UITweener.Style.Loop)
  64. {
  65. if (this.mFactor > 1f)
  66. {
  67. this.mFactor -= Mathf.Floor(this.mFactor);
  68. }
  69. }
  70. else if (this.style == UITweener.Style.PingPong)
  71. {
  72. if (this.mFactor > 1f)
  73. {
  74. this.mFactor = 1f - (this.mFactor - Mathf.Floor(this.mFactor));
  75. this.mAmountPerDelta = -this.mAmountPerDelta;
  76. }
  77. else if (this.mFactor < 0f)
  78. {
  79. this.mFactor = -this.mFactor;
  80. this.mFactor -= Mathf.Floor(this.mFactor);
  81. this.mAmountPerDelta = -this.mAmountPerDelta;
  82. }
  83. }
  84. if (this.style == UITweener.Style.Once && (this.duration == 0f || this.mFactor > 1f || this.mFactor < 0f))
  85. {
  86. this.mFactor = Mathf.Clamp01(this.mFactor);
  87. this.Sample(this.mFactor, true);
  88. if (this.duration == 0f || (this.mFactor == 1f && this.mAmountPerDelta > 0f) || (this.mFactor == 0f && this.mAmountPerDelta < 0f))
  89. {
  90. base.enabled = false;
  91. }
  92. if (UITweener.current == null)
  93. {
  94. UITweener.current = this;
  95. if (this.onFinished != null)
  96. {
  97. this.mTemp = this.onFinished;
  98. this.onFinished = new List<EventDelegate>();
  99. EventDelegate.Execute(this.mTemp);
  100. for (int i = 0; i < this.mTemp.Count; i++)
  101. {
  102. EventDelegate eventDelegate = this.mTemp[i];
  103. if (eventDelegate != null && !eventDelegate.oneShot)
  104. {
  105. EventDelegate.Add(this.onFinished, eventDelegate, eventDelegate.oneShot);
  106. }
  107. }
  108. this.mTemp = null;
  109. }
  110. if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
  111. {
  112. this.eventReceiver.SendMessage(this.callWhenFinished, this, SendMessageOptions.DontRequireReceiver);
  113. }
  114. UITweener.current = null;
  115. }
  116. }
  117. else
  118. {
  119. this.Sample(this.mFactor, false);
  120. }
  121. }
  122. public void SetOnFinished(EventDelegate.Callback del)
  123. {
  124. EventDelegate.Set(this.onFinished, del);
  125. }
  126. public void SetOnFinished(EventDelegate del)
  127. {
  128. EventDelegate.Set(this.onFinished, del);
  129. }
  130. public void AddOnFinished(EventDelegate.Callback del)
  131. {
  132. EventDelegate.Add(this.onFinished, del);
  133. }
  134. public void AddOnFinished(EventDelegate del)
  135. {
  136. EventDelegate.Add(this.onFinished, del);
  137. }
  138. public void RemoveOnFinished(EventDelegate del)
  139. {
  140. if (this.onFinished != null)
  141. {
  142. this.onFinished.Remove(del);
  143. }
  144. if (this.mTemp != null)
  145. {
  146. this.mTemp.Remove(del);
  147. }
  148. }
  149. private void OnDisable()
  150. {
  151. this.mStarted = false;
  152. }
  153. public void Sample(float factor, bool isFinished)
  154. {
  155. float num = Mathf.Clamp01(factor);
  156. if (this.method == UITweener.Method.EaseIn)
  157. {
  158. num = 1f - Mathf.Sin(1.57079637f * (1f - num));
  159. if (this.steeperCurves)
  160. {
  161. num *= num;
  162. }
  163. }
  164. else if (this.method == UITweener.Method.EaseOut)
  165. {
  166. num = Mathf.Sin(1.57079637f * num);
  167. if (this.steeperCurves)
  168. {
  169. num = 1f - num;
  170. num = 1f - num * num;
  171. }
  172. }
  173. else if (this.method == UITweener.Method.EaseInOut)
  174. {
  175. num -= Mathf.Sin(num * 6.28318548f) / 6.28318548f;
  176. if (this.steeperCurves)
  177. {
  178. num = num * 2f - 1f;
  179. float num2 = Mathf.Sign(num);
  180. num = 1f - Mathf.Abs(num);
  181. num = 1f - num * num;
  182. num = num2 * num * 0.5f + 0.5f;
  183. }
  184. }
  185. else if (this.method == UITweener.Method.BounceIn)
  186. {
  187. num = this.BounceLogic(num);
  188. }
  189. else if (this.method == UITweener.Method.BounceOut)
  190. {
  191. num = 1f - this.BounceLogic(1f - num);
  192. }
  193. this.OnUpdate((this.animationCurve == null) ? num : this.animationCurve.Evaluate(num), isFinished);
  194. }
  195. private float BounceLogic(float val)
  196. {
  197. if (val < 0.363636f)
  198. {
  199. val = 7.5685f * val * val;
  200. }
  201. else if (val < 0.727272f)
  202. {
  203. val = 7.5625f * (val -= 0.545454f) * val + 0.75f;
  204. }
  205. else if (val < 0.90909f)
  206. {
  207. val = 7.5625f * (val -= 0.818181f) * val + 0.9375f;
  208. }
  209. else
  210. {
  211. val = 7.5625f * (val -= 0.9545454f) * val + 0.984375f;
  212. }
  213. return val;
  214. }
  215. [Obsolete("Use PlayForward() instead")]
  216. public void Play()
  217. {
  218. this.Play(true);
  219. }
  220. public void PlayForward()
  221. {
  222. this.Play(true);
  223. }
  224. public void PlayReverse()
  225. {
  226. this.Play(false);
  227. }
  228. public void Play(bool forward)
  229. {
  230. this.mAmountPerDelta = Mathf.Abs(this.amountPerDelta);
  231. if (!forward)
  232. {
  233. this.mAmountPerDelta = -this.mAmountPerDelta;
  234. }
  235. base.enabled = true;
  236. this.Update();
  237. }
  238. public void ResetToBeginning()
  239. {
  240. this.mStarted = false;
  241. this.mFactor = ((this.amountPerDelta >= 0f) ? 0f : 1f);
  242. this.Sample(this.mFactor, false);
  243. }
  244. public void Toggle()
  245. {
  246. if (this.mFactor > 0f)
  247. {
  248. this.mAmountPerDelta = -this.amountPerDelta;
  249. }
  250. else
  251. {
  252. this.mAmountPerDelta = Mathf.Abs(this.amountPerDelta);
  253. }
  254. base.enabled = true;
  255. }
  256. protected abstract void OnUpdate(float factor, bool isFinished);
  257. public static T Begin<T>(GameObject go, float duration) where T : UITweener
  258. {
  259. T t = go.GetComponent<T>();
  260. if (t != null && t.tweenGroup != 0)
  261. {
  262. t = (T)((object)null);
  263. T[] components = go.GetComponents<T>();
  264. int i = 0;
  265. int num = components.Length;
  266. while (i < num)
  267. {
  268. t = components[i];
  269. if (t != null && t.tweenGroup == 0)
  270. {
  271. break;
  272. }
  273. t = (T)((object)null);
  274. i++;
  275. }
  276. }
  277. if (t == null)
  278. {
  279. t = go.AddComponent<T>();
  280. if (t == null)
  281. {
  282. Debug.LogError(string.Concat(new object[]
  283. {
  284. "Unable to add ",
  285. typeof(T),
  286. " to ",
  287. NGUITools.GetHierarchy(go)
  288. }), go);
  289. return (T)((object)null);
  290. }
  291. }
  292. t.mStarted = false;
  293. t.duration = duration;
  294. t.mFactor = 0f;
  295. t.mAmountPerDelta = Mathf.Abs(t.amountPerDelta);
  296. t.style = UITweener.Style.Once;
  297. t.animationCurve = new AnimationCurve(new Keyframe[]
  298. {
  299. new Keyframe(0f, 0f, 0f, 1f),
  300. new Keyframe(1f, 1f, 1f, 0f)
  301. });
  302. t.eventReceiver = null;
  303. t.callWhenFinished = null;
  304. t.enabled = true;
  305. return t;
  306. }
  307. public virtual void SetStartToCurrentValue()
  308. {
  309. }
  310. public virtual void SetEndToCurrentValue()
  311. {
  312. }
  313. public static UITweener current;
  314. [HideInInspector]
  315. public UITweener.Method method;
  316. [HideInInspector]
  317. public UITweener.Style style;
  318. [HideInInspector]
  319. public AnimationCurve animationCurve = new AnimationCurve(new Keyframe[]
  320. {
  321. new Keyframe(0f, 0f, 0f, 1f),
  322. new Keyframe(1f, 1f, 1f, 0f)
  323. });
  324. [HideInInspector]
  325. public bool ignoreTimeScale = true;
  326. [HideInInspector]
  327. public float delay;
  328. [HideInInspector]
  329. public float duration = 1f;
  330. [HideInInspector]
  331. public bool steeperCurves;
  332. [HideInInspector]
  333. public int tweenGroup;
  334. [HideInInspector]
  335. public List<EventDelegate> onFinished = new List<EventDelegate>();
  336. [HideInInspector]
  337. public GameObject eventReceiver;
  338. [HideInInspector]
  339. public string callWhenFinished;
  340. private bool mStarted;
  341. private float mStartTime;
  342. private float mDuration;
  343. private float mAmountPerDelta = 1000f;
  344. private float mFactor;
  345. private List<EventDelegate> mTemp;
  346. public enum Method
  347. {
  348. Linear,
  349. EaseIn,
  350. EaseOut,
  351. EaseInOut,
  352. BounceIn,
  353. BounceOut
  354. }
  355. public enum Style
  356. {
  357. Once,
  358. Loop,
  359. PingPong
  360. }
  361. }