DOTweenModuleUnityVersion.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2018/07/13
  3. using System;
  4. using UnityEngine;
  5. using DG.Tweening.Core;
  6. using DG.Tweening.Plugins.Options;
  7. #pragma warning disable 1591
  8. namespace DG.Tweening
  9. {
  10. /// <summary>
  11. /// Shortcuts/functions that are not strictly related to specific Modules
  12. /// but are available only on some Unity versions
  13. /// </summary>
  14. public static class DOTweenModuleUnityVersion
  15. {
  16. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1_OR_NEWER
  17. #region Unity 4.3 or Newer
  18. #region Material
  19. /// <summary>Tweens a Material's color using the given gradient
  20. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  21. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  22. /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param>
  23. public static Sequence DOGradientColor(this Material target, Gradient gradient, float duration)
  24. {
  25. Sequence s = DOTween.Sequence();
  26. GradientColorKey[] colors = gradient.colorKeys;
  27. int len = colors.Length;
  28. for (int i = 0; i < len; ++i) {
  29. GradientColorKey c = colors[i];
  30. if (i == 0 && c.time <= 0) {
  31. target.color = c.color;
  32. continue;
  33. }
  34. float colorDuration = i == len - 1
  35. ? duration - s.Duration(false) // Verifies that total duration is correct
  36. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  37. s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear));
  38. }
  39. return s;
  40. }
  41. /// <summary>Tweens a Material's named color property using the given gradient
  42. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  43. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  44. /// <param name="gradient">The gradient to use</param>
  45. /// <param name="property">The name of the material property to tween (like _Tint or _SpecColor)</param>
  46. /// <param name="duration">The duration of the tween</param>
  47. public static Sequence DOGradientColor(this Material target, Gradient gradient, string property, float duration)
  48. {
  49. Sequence s = DOTween.Sequence();
  50. GradientColorKey[] colors = gradient.colorKeys;
  51. int len = colors.Length;
  52. for (int i = 0; i < len; ++i) {
  53. GradientColorKey c = colors[i];
  54. if (i == 0 && c.time <= 0) {
  55. target.SetColor(property, c.color);
  56. continue;
  57. }
  58. float colorDuration = i == len - 1
  59. ? duration - s.Duration(false) // Verifies that total duration is correct
  60. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  61. s.Append(target.DOColor(c.color, property, colorDuration).SetEase(Ease.Linear));
  62. }
  63. return s;
  64. }
  65. #endregion
  66. #endregion
  67. #endif
  68. #if UNITY_5_3_OR_NEWER || UNITY_2017_1_OR_NEWER
  69. #region Unity 5.3 or Newer
  70. #region CustomYieldInstructions
  71. /// <summary>
  72. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or complete.
  73. /// It can be used inside a coroutine as a yield.
  74. /// <para>Example usage:</para><code>yield return myTween.WaitForCompletion(true);</code>
  75. /// </summary>
  76. public static CustomYieldInstruction WaitForCompletion(this Tween t, bool returnCustomYieldInstruction)
  77. {
  78. if (!t.active) {
  79. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  80. return null;
  81. }
  82. return new DOTweenCYInstruction.WaitForCompletion(t);
  83. }
  84. /// <summary>
  85. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or rewinded.
  86. /// It can be used inside a coroutine as a yield.
  87. /// <para>Example usage:</para><code>yield return myTween.WaitForRewind();</code>
  88. /// </summary>
  89. public static CustomYieldInstruction WaitForRewind(this Tween t, bool returnCustomYieldInstruction)
  90. {
  91. if (!t.active) {
  92. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  93. return null;
  94. }
  95. return new DOTweenCYInstruction.WaitForRewind(t);
  96. }
  97. /// <summary>
  98. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed.
  99. /// It can be used inside a coroutine as a yield.
  100. /// <para>Example usage:</para><code>yield return myTween.WaitForKill();</code>
  101. /// </summary>
  102. public static CustomYieldInstruction WaitForKill(this Tween t, bool returnCustomYieldInstruction)
  103. {
  104. if (!t.active) {
  105. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  106. return null;
  107. }
  108. return new DOTweenCYInstruction.WaitForKill(t);
  109. }
  110. /// <summary>
  111. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or has gone through the given amount of loops.
  112. /// It can be used inside a coroutine as a yield.
  113. /// <para>Example usage:</para><code>yield return myTween.WaitForElapsedLoops(2);</code>
  114. /// </summary>
  115. /// <param name="elapsedLoops">Elapsed loops to wait for</param>
  116. public static CustomYieldInstruction WaitForElapsedLoops(this Tween t, int elapsedLoops, bool returnCustomYieldInstruction)
  117. {
  118. if (!t.active) {
  119. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  120. return null;
  121. }
  122. return new DOTweenCYInstruction.WaitForElapsedLoops(t, elapsedLoops);
  123. }
  124. /// <summary>
  125. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or has reached the given position (loops included, delays excluded).
  126. /// It can be used inside a coroutine as a yield.
  127. /// <para>Example usage:</para><code>yield return myTween.WaitForPosition(2.5f);</code>
  128. /// </summary>
  129. /// <param name="position">Position (loops included, delays excluded) to wait for</param>
  130. public static CustomYieldInstruction WaitForPosition(this Tween t, float position, bool returnCustomYieldInstruction)
  131. {
  132. if (!t.active) {
  133. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  134. return null;
  135. }
  136. return new DOTweenCYInstruction.WaitForPosition(t, position);
  137. }
  138. /// <summary>
  139. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or started
  140. /// (meaning when the tween is set in a playing state the first time, after any eventual delay).
  141. /// It can be used inside a coroutine as a yield.
  142. /// <para>Example usage:</para><code>yield return myTween.WaitForStart();</code>
  143. /// </summary>
  144. public static CustomYieldInstruction WaitForStart(this Tween t, bool returnCustomYieldInstruction)
  145. {
  146. if (!t.active) {
  147. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  148. return null;
  149. }
  150. return new DOTweenCYInstruction.WaitForStart(t);
  151. }
  152. #endregion
  153. #endregion
  154. #endif
  155. #if UNITY_2018_1_OR_NEWER
  156. #region Unity 2018.1 or Newer
  157. #region Material
  158. /// <summary>Tweens a Material's named texture offset property with the given ID to the given value.
  159. /// Also stores the material as the tween's target so it can be used for filtered operations</summary>
  160. /// <param name="endValue">The end value to reach</param>
  161. /// <param name="propertyID">The ID of the material property to tween (also called nameID in Unity's manual)</param>
  162. /// <param name="duration">The duration of the tween</param>
  163. public static TweenerCore<Vector2, Vector2, VectorOptions> DOOffset(this Material target, Vector2 endValue, int propertyID, float duration)
  164. {
  165. if (!target.HasProperty(propertyID)) {
  166. if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(propertyID);
  167. return null;
  168. }
  169. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.GetTextureOffset(propertyID), x => target.SetTextureOffset(propertyID, x), endValue, duration);
  170. t.SetTarget(target);
  171. return t;
  172. }
  173. /// <summary>Tweens a Material's named texture scale property with the given ID to the given value.
  174. /// Also stores the material as the tween's target so it can be used for filtered operations</summary>
  175. /// <param name="endValue">The end value to reach</param>
  176. /// <param name="propertyID">The ID of the material property to tween (also called nameID in Unity's manual)</param>
  177. /// <param name="duration">The duration of the tween</param>
  178. public static TweenerCore<Vector2, Vector2, VectorOptions> DOTiling(this Material target, Vector2 endValue, int propertyID, float duration)
  179. {
  180. if (!target.HasProperty(propertyID)) {
  181. if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(propertyID);
  182. return null;
  183. }
  184. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.GetTextureScale(propertyID), x => target.SetTextureScale(propertyID, x), endValue, duration);
  185. t.SetTarget(target);
  186. return t;
  187. }
  188. #endregion
  189. #endregion
  190. #endif
  191. }
  192. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  193. // ███ CLASSES █████████████████████████████████████████████████████████████████████████████████████████████████████████
  194. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  195. #if UNITY_5_3_OR_NEWER || UNITY_2017_1_OR_NEWER
  196. public static class DOTweenCYInstruction
  197. {
  198. public class WaitForCompletion : CustomYieldInstruction
  199. {
  200. public override bool keepWaiting { get {
  201. return t.active && !t.IsComplete();
  202. }}
  203. readonly Tween t;
  204. public WaitForCompletion(Tween tween)
  205. {
  206. t = tween;
  207. }
  208. }
  209. public class WaitForRewind : CustomYieldInstruction
  210. {
  211. public override bool keepWaiting { get {
  212. return t.active && (!t.playedOnce || t.position * (t.CompletedLoops() + 1) > 0);
  213. }}
  214. readonly Tween t;
  215. public WaitForRewind(Tween tween)
  216. {
  217. t = tween;
  218. }
  219. }
  220. public class WaitForKill : CustomYieldInstruction
  221. {
  222. public override bool keepWaiting { get {
  223. return t.active;
  224. }}
  225. readonly Tween t;
  226. public WaitForKill(Tween tween)
  227. {
  228. t = tween;
  229. }
  230. }
  231. public class WaitForElapsedLoops : CustomYieldInstruction
  232. {
  233. public override bool keepWaiting { get {
  234. return t.active && t.CompletedLoops() < elapsedLoops;
  235. }}
  236. readonly Tween t;
  237. readonly int elapsedLoops;
  238. public WaitForElapsedLoops(Tween tween, int elapsedLoops)
  239. {
  240. t = tween;
  241. this.elapsedLoops = elapsedLoops;
  242. }
  243. }
  244. public class WaitForPosition : CustomYieldInstruction
  245. {
  246. public override bool keepWaiting { get {
  247. return t.active && t.position * (t.CompletedLoops() + 1) < position;
  248. }}
  249. readonly Tween t;
  250. readonly float position;
  251. public WaitForPosition(Tween tween, float position)
  252. {
  253. t = tween;
  254. this.position = position;
  255. }
  256. }
  257. public class WaitForStart : CustomYieldInstruction
  258. {
  259. public override bool keepWaiting { get {
  260. return t.active && !t.playedOnce;
  261. }}
  262. readonly Tween t;
  263. public WaitForStart(Tween tween)
  264. {
  265. t = tween;
  266. }
  267. }
  268. }
  269. #endif
  270. }