LocationAnimations.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. #pragma warning disable 0649 // disable "never assigned" warnings
  9. namespace TheraBytes.BetterUi
  10. {
  11. [HelpURL("https://documentation.therabytes.de/better-ui/LocationAnimations.html")]
  12. [RequireComponent(typeof(RectTransform))]
  13. [AddComponentMenu("Better UI/Animation/Location Animations", 30)]
  14. public class LocationAnimations : MonoBehaviour
  15. {
  16. #region Nested types
  17. [Serializable]
  18. public class LocationAnimationEvent : UnityEvent
  19. {
  20. public LocationAnimationEvent() { }
  21. public LocationAnimationEvent(params UnityAction[] actions)
  22. {
  23. foreach (UnityAction act in actions)
  24. {
  25. this.AddListener(act);
  26. }
  27. }
  28. }
  29. [Serializable]
  30. public class LocationAnimationUpdateEvent : UnityEvent<float>
  31. {
  32. public LocationAnimationUpdateEvent() { }
  33. public LocationAnimationUpdateEvent(params UnityAction<float>[] actions)
  34. {
  35. foreach (UnityAction<float> act in actions)
  36. {
  37. this.AddListener(act);
  38. }
  39. }
  40. }
  41. [Serializable]
  42. public class RectTransformDataConfigCollection : SizeConfigCollection<RectTransformData> { }
  43. [Serializable]
  44. public class LocationData
  45. {
  46. [SerializeField]
  47. string name;
  48. [SerializeField]
  49. RectTransformData transformFallback = new RectTransformData();
  50. [SerializeField]
  51. RectTransformDataConfigCollection transformConfigs = new RectTransformDataConfigCollection();
  52. public string Name { get { return name; } internal set { name = value; } }
  53. public RectTransformData CurrentTransformData { get { return transformConfigs.GetCurrentItem(transformFallback); } }
  54. }
  55. [Serializable]
  56. public class Animation
  57. {
  58. [SerializeField]
  59. string name;
  60. [SerializeField]
  61. string from;
  62. [SerializeField]
  63. string to;
  64. [SerializeField]
  65. AnimationCurve curve;
  66. [SerializeField]
  67. LocationAnimationEvent actionBeforeStart = new LocationAnimationEvent();
  68. [SerializeField]
  69. LocationAnimationEvent actionAfterFinish = new LocationAnimationEvent();
  70. [SerializeField]
  71. LocationAnimationUpdateEvent actionOnUpdating = new LocationAnimationUpdateEvent();
  72. [SerializeField]
  73. bool animateWithEulerRotation = true;
  74. [SerializeField]
  75. float timeScale = 1;
  76. public string Name { get { return name; } internal set { name = value; } }
  77. public string From { get { return from; } set { from = value; } }
  78. public string To { get { return to; } set { to = value; } }
  79. // advanced
  80. public AnimationCurve Curve { get { return curve; } internal set { curve = value; } }
  81. public bool AnimateWithEulerRotation { get { return animateWithEulerRotation; } set { animateWithEulerRotation = value; } }
  82. public float TimeScale { get { return timeScale; } set { timeScale = value; } }
  83. public LocationAnimationEvent ActionBeforeStart { get { return actionBeforeStart; } }
  84. public LocationAnimationEvent ActionAfterFinish { get { return actionAfterFinish; } }
  85. public LocationAnimationUpdateEvent ActionOnUpdating { get { return actionOnUpdating; } }
  86. }
  87. [Serializable]
  88. public class AnimationState
  89. {
  90. public Animation Animation { get; internal set; }
  91. public RectTransformData From { get; internal set; }
  92. public RectTransformData To { get; internal set; }
  93. public float Time { get; set; }
  94. public float Duration { get; set; }
  95. public bool Loop { get; internal set; }
  96. public float TimeScale { get; set; }
  97. public LocationAnimationEvent ActionAfterFinish { get; internal set; }
  98. }
  99. #endregion
  100. public RectTransform RectTransform { get { return this.transform as RectTransform; } }
  101. public List<LocationData> Locations { get { return locations; } }
  102. public List<Animation> Animations { get { return animations; } }
  103. public string StartUpAnimation { get { return startUpAnimation; } set { startUpAnimation = value; } }
  104. public string StartLocation { get { return startLocation; } set { startLocation = value; } }
  105. public bool IsAnimating { get { return runningAnimation != null; } }
  106. public bool UseRelativeLocations
  107. {
  108. get { return useRelativeLocations; }
  109. #if UNITY_EDITOR
  110. set { useRelativeLocations = value; }
  111. #endif
  112. }
  113. [SerializeField]
  114. bool useRelativeLocations;
  115. [SerializeField]
  116. List<LocationData> locations = new List<LocationData>();
  117. [SerializeField]
  118. List<Animation> animations = new List<Animation>();
  119. [SerializeField]
  120. string startLocation;
  121. [SerializeField]
  122. string startUpAnimation;
  123. [SerializeField]
  124. LocationAnimationEvent actionOnInit;
  125. RectTransformData referenceLocation;
  126. AnimationState runningAnimation;
  127. public AnimationState RunningAnimation { get { return runningAnimation; } }
  128. public RectTransformData ReferenceLocation { get { EnsureReferenceLocation(true); return referenceLocation; } }
  129. private void Start()
  130. {
  131. ResetReferenceLocation();
  132. SetToLocation(startLocation);
  133. actionOnInit.Invoke();
  134. StartAnimation(startUpAnimation);
  135. }
  136. public void StopCurrentAnimation()
  137. {
  138. runningAnimation = null;
  139. }
  140. public void StartAnimation(string name) { StartAnimation(GetAnimation(name), null, null); }
  141. public void StartAnimation(string name, float timeScale) { StartAnimation(GetAnimation(name), timeScale, null); }
  142. public void StartAnimation(string name, LocationAnimationEvent onFinish) { StartAnimation(GetAnimation(name), null, onFinish); }
  143. public void StartAnimation(string name, float timeScale, LocationAnimationEvent onFinish) { StartAnimation(GetAnimation(name), timeScale, onFinish); }
  144. public void StartAnimation(Animation ani, float? timeScale, LocationAnimationEvent onFinish)
  145. {
  146. if (ani == null || ani.To == null || (runningAnimation != null && ani == runningAnimation.Animation))
  147. return;
  148. if (runningAnimation != null)
  149. {
  150. StopCurrentAnimation();
  151. }
  152. if (ani.Curve == null || ani.Curve.keys.Length <= 1)
  153. {
  154. SetToLocation(ani.To);
  155. return;
  156. }
  157. float speed = timeScale ?? ani.TimeScale;
  158. bool forever =
  159. (speed > 0
  160. && (ani.Curve.postWrapMode == WrapMode.Loop
  161. || ani.Curve.postWrapMode == WrapMode.PingPong))
  162. || (speed < 0
  163. && (ani.Curve.preWrapMode == WrapMode.Loop
  164. || ani.Curve.preWrapMode == WrapMode.PingPong));
  165. runningAnimation = new AnimationState()
  166. {
  167. Animation = ani,
  168. From = GetLocationTransformFallbackCurrent(ani.From),
  169. To = GetLocationTransformFallbackCurrent(ani.To),
  170. ActionAfterFinish = onFinish ?? ani.ActionAfterFinish,
  171. Duration = ani.Curve.keys[ani.Curve.keys.Length - 1].time,
  172. Loop = forever,
  173. TimeScale = speed,
  174. Time = 0,
  175. };
  176. ani.ActionBeforeStart.Invoke();
  177. }
  178. private void Update()
  179. {
  180. UpdateCurrentAnimation(Time.unscaledDeltaTime);
  181. }
  182. public void UpdateCurrentAnimation(float deltaTime)
  183. {
  184. if (runningAnimation == null || runningAnimation.Animation == null ||
  185. runningAnimation.Animation.Curve == null || runningAnimation.Animation.Curve.length == 0)
  186. return;
  187. bool animationTimeIsOver = (!runningAnimation.Loop && runningAnimation.Time >= runningAnimation.Duration);
  188. if (animationTimeIsOver)
  189. runningAnimation.Time = runningAnimation.Duration;
  190. float amount = runningAnimation.Animation.Curve.Evaluate(runningAnimation.Time);
  191. var rtd = RectTransformData.LerpUnclamped(runningAnimation.From, runningAnimation.To, amount, runningAnimation.Animation.AnimateWithEulerRotation);
  192. rtd.PushToTransform(RectTransform);
  193. runningAnimation.Animation.ActionOnUpdating.Invoke(amount);
  194. runningAnimation.Time += deltaTime * runningAnimation.TimeScale;
  195. if (animationTimeIsOver)
  196. {
  197. var cache = runningAnimation;
  198. runningAnimation = null;
  199. cache.ActionAfterFinish.Invoke();
  200. }
  201. }
  202. public void SetToLocation(string name)
  203. {
  204. LocationData loc = GetLocation(name);
  205. if (loc == null)
  206. return;
  207. PushTransformData(loc);
  208. }
  209. public LocationData GetLocation(string name)
  210. {
  211. return locations.FirstOrDefault(o => o.Name == name);
  212. }
  213. public void ResetReferenceLocation() { ResetReferenceLocation(this.RectTransform); }
  214. public void ResetReferenceLocation(RectTransform rectTransform) { ResetReferenceLocation(new RectTransformData(rectTransform)); }
  215. public void ResetReferenceLocation(RectTransformData reference) { referenceLocation = reference; }
  216. void PushTransformData(LocationData loc)
  217. {
  218. if (useRelativeLocations)
  219. {
  220. EnsureReferenceLocation();
  221. var cur = loc.CurrentTransformData;
  222. var transformData = RectTransformData.Combine(cur, referenceLocation);
  223. transformData.PushToTransform(RectTransform);
  224. }
  225. else
  226. {
  227. loc.CurrentTransformData.PushToTransform(RectTransform);
  228. }
  229. }
  230. private void EnsureReferenceLocation(bool force = false)
  231. {
  232. if (referenceLocation == null
  233. || ((force || useRelativeLocations) && referenceLocation == RectTransformData.Invalid))
  234. {
  235. ResetReferenceLocation();
  236. }
  237. }
  238. private RectTransformData GetLocationTransformFallbackCurrent(string name)
  239. {
  240. EnsureReferenceLocation();
  241. var loc = locations.FirstOrDefault(o => o.Name == name);
  242. RectTransformData cur = (loc == null)
  243. ? new RectTransformData(RectTransform)
  244. : loc.CurrentTransformData;
  245. RectTransformData result = (useRelativeLocations && loc != null)
  246. ? RectTransformData.Combine(cur, referenceLocation)
  247. : cur;
  248. result.SaveRotationAsEuler = true;
  249. return result;
  250. }
  251. public Animation GetAnimation(string name)
  252. {
  253. return animations.FirstOrDefault(o => o.Name == name);
  254. }
  255. #if UNITY_EDITOR
  256. public void OnValidate()
  257. {
  258. for (int i = 0; i < animations.Count; i++)
  259. {
  260. Animation ani = animations[i];
  261. if (ani.Curve == null || ani.Curve.keys.Length < 2)
  262. {
  263. ani.Curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
  264. }
  265. }
  266. for (int i = 0; i < locations.Count; i++)
  267. {
  268. LocationData loc = locations[i];
  269. var cur = loc.CurrentTransformData;
  270. if (cur == RectTransformData.Invalid)
  271. {
  272. cur.PullFromTransform(RectTransform);
  273. if (useRelativeLocations)
  274. {
  275. EnsureReferenceLocation();
  276. cur.PullFromData(RectTransformData.Separate(cur, referenceLocation));
  277. }
  278. }
  279. }
  280. }
  281. #endif
  282. }
  283. }
  284. #pragma warning restore 0649