DOTweenModuleUnityVersion.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. //#if UNITY_2018_1_OR_NEWER && (NET_4_6 || NET_STANDARD_2_0)
  8. //using Task = System.Threading.Tasks.Task;
  9. //#endif
  10. #pragma warning disable 1591
  11. namespace DG.Tweening
  12. {
  13. /// <summary>
  14. /// Shortcuts/functions that are not strictly related to specific Modules
  15. /// but are available only on some Unity versions
  16. /// </summary>
  17. public static class DOTweenModuleUnityVersion
  18. {
  19. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1_OR_NEWER
  20. #region Unity 4.3 or Newer
  21. #region Material
  22. /// <summary>Tweens a Material's color using the given gradient
  23. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  24. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  25. /// <param name="gradient">The gradient to use</param><param name="duration">The duration of the tween</param>
  26. public static Sequence DOGradientColor(this Material target, Gradient gradient, float duration)
  27. {
  28. Sequence s = DOTween.Sequence();
  29. GradientColorKey[] colors = gradient.colorKeys;
  30. int len = colors.Length;
  31. for (int i = 0; i < len; ++i) {
  32. GradientColorKey c = colors[i];
  33. if (i == 0 && c.time <= 0) {
  34. target.color = c.color;
  35. continue;
  36. }
  37. float colorDuration = i == len - 1
  38. ? duration - s.Duration(false) // Verifies that total duration is correct
  39. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  40. s.Append(target.DOColor(c.color, colorDuration).SetEase(Ease.Linear));
  41. }
  42. s.SetTarget(target);
  43. return s;
  44. }
  45. /// <summary>Tweens a Material's named color property using the given gradient
  46. /// (NOTE 1: only uses the colors of the gradient, not the alphas - NOTE 2: creates a Sequence, not a Tweener).
  47. /// Also stores the image as the tween's target so it can be used for filtered operations</summary>
  48. /// <param name="gradient">The gradient to use</param>
  49. /// <param name="property">The name of the material property to tween (like _Tint or _SpecColor)</param>
  50. /// <param name="duration">The duration of the tween</param>
  51. public static Sequence DOGradientColor(this Material target, Gradient gradient, string property, float duration)
  52. {
  53. Sequence s = DOTween.Sequence();
  54. GradientColorKey[] colors = gradient.colorKeys;
  55. int len = colors.Length;
  56. for (int i = 0; i < len; ++i) {
  57. GradientColorKey c = colors[i];
  58. if (i == 0 && c.time <= 0) {
  59. target.SetColor(property, c.color);
  60. continue;
  61. }
  62. float colorDuration = i == len - 1
  63. ? duration - s.Duration(false) // Verifies that total duration is correct
  64. : duration * (i == 0 ? c.time : c.time - colors[i - 1].time);
  65. s.Append(target.DOColor(c.color, property, colorDuration).SetEase(Ease.Linear));
  66. }
  67. s.SetTarget(target);
  68. return s;
  69. }
  70. #endregion
  71. #endregion
  72. #endif
  73. #if UNITY_5_3_OR_NEWER || UNITY_2017_1_OR_NEWER
  74. #region Unity 5.3 or Newer
  75. #region CustomYieldInstructions
  76. /// <summary>
  77. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or complete.
  78. /// It can be used inside a coroutine as a yield.
  79. /// <para>Example usage:</para><code>yield return myTween.WaitForCompletion(true);</code>
  80. /// </summary>
  81. public static CustomYieldInstruction WaitForCompletion(this Tween t, bool returnCustomYieldInstruction)
  82. {
  83. if (!t.active) {
  84. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  85. return null;
  86. }
  87. return new DOTweenCYInstruction.WaitForCompletion(t);
  88. }
  89. /// <summary>
  90. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or rewinded.
  91. /// It can be used inside a coroutine as a yield.
  92. /// <para>Example usage:</para><code>yield return myTween.WaitForRewind();</code>
  93. /// </summary>
  94. public static CustomYieldInstruction WaitForRewind(this Tween t, bool returnCustomYieldInstruction)
  95. {
  96. if (!t.active) {
  97. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  98. return null;
  99. }
  100. return new DOTweenCYInstruction.WaitForRewind(t);
  101. }
  102. /// <summary>
  103. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed.
  104. /// It can be used inside a coroutine as a yield.
  105. /// <para>Example usage:</para><code>yield return myTween.WaitForKill();</code>
  106. /// </summary>
  107. public static CustomYieldInstruction WaitForKill(this Tween t, bool returnCustomYieldInstruction)
  108. {
  109. if (!t.active) {
  110. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  111. return null;
  112. }
  113. return new DOTweenCYInstruction.WaitForKill(t);
  114. }
  115. /// <summary>
  116. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or has gone through the given amount of loops.
  117. /// It can be used inside a coroutine as a yield.
  118. /// <para>Example usage:</para><code>yield return myTween.WaitForElapsedLoops(2);</code>
  119. /// </summary>
  120. /// <param name="elapsedLoops">Elapsed loops to wait for</param>
  121. public static CustomYieldInstruction WaitForElapsedLoops(this Tween t, int elapsedLoops, bool returnCustomYieldInstruction)
  122. {
  123. if (!t.active) {
  124. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  125. return null;
  126. }
  127. return new DOTweenCYInstruction.WaitForElapsedLoops(t, elapsedLoops);
  128. }
  129. /// <summary>
  130. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed
  131. /// or has reached the given time position (loops included, delays excluded).
  132. /// It can be used inside a coroutine as a yield.
  133. /// <para>Example usage:</para><code>yield return myTween.WaitForPosition(2.5f);</code>
  134. /// </summary>
  135. /// <param name="position">Position (loops included, delays excluded) to wait for</param>
  136. public static CustomYieldInstruction WaitForPosition(this Tween t, float position, bool returnCustomYieldInstruction)
  137. {
  138. if (!t.active) {
  139. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  140. return null;
  141. }
  142. return new DOTweenCYInstruction.WaitForPosition(t, position);
  143. }
  144. /// <summary>
  145. /// Returns a <see cref="CustomYieldInstruction"/> that waits until the tween is killed or started
  146. /// (meaning when the tween is set in a playing state the first time, after any eventual delay).
  147. /// It can be used inside a coroutine as a yield.
  148. /// <para>Example usage:</para><code>yield return myTween.WaitForStart();</code>
  149. /// </summary>
  150. public static CustomYieldInstruction WaitForStart(this Tween t, bool returnCustomYieldInstruction)
  151. {
  152. if (!t.active) {
  153. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  154. return null;
  155. }
  156. return new DOTweenCYInstruction.WaitForStart(t);
  157. }
  158. #endregion
  159. #endregion
  160. #endif
  161. #if UNITY_2018_1_OR_NEWER
  162. #region Unity 2018.1 or Newer
  163. #region Material
  164. /// <summary>Tweens a Material's named texture offset property with the given ID to the given value.
  165. /// Also stores the material as the tween's target so it can be used for filtered operations</summary>
  166. /// <param name="endValue">The end value to reach</param>
  167. /// <param name="propertyID">The ID of the material property to tween (also called nameID in Unity's manual)</param>
  168. /// <param name="duration">The duration of the tween</param>
  169. public static TweenerCore<Vector2, Vector2, VectorOptions> DOOffset(this Material target, Vector2 endValue, int propertyID, float duration)
  170. {
  171. if (!target.HasProperty(propertyID)) {
  172. if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(propertyID);
  173. return null;
  174. }
  175. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.GetTextureOffset(propertyID), x => target.SetTextureOffset(propertyID, x), endValue, duration);
  176. t.SetTarget(target);
  177. return t;
  178. }
  179. /// <summary>Tweens a Material's named texture scale property with the given ID to the given value.
  180. /// Also stores the material as the tween's target so it can be used for filtered operations</summary>
  181. /// <param name="endValue">The end value to reach</param>
  182. /// <param name="propertyID">The ID of the material property to tween (also called nameID in Unity's manual)</param>
  183. /// <param name="duration">The duration of the tween</param>
  184. public static TweenerCore<Vector2, Vector2, VectorOptions> DOTiling(this Material target, Vector2 endValue, int propertyID, float duration)
  185. {
  186. if (!target.HasProperty(propertyID)) {
  187. if (Debugger.logPriority > 0) Debugger.LogMissingMaterialProperty(propertyID);
  188. return null;
  189. }
  190. TweenerCore<Vector2, Vector2, VectorOptions> t = DOTween.To(() => target.GetTextureScale(propertyID), x => target.SetTextureScale(propertyID, x), endValue, duration);
  191. t.SetTarget(target);
  192. return t;
  193. }
  194. #endregion
  195. #region .NET 4.6 or Newer
  196. #if UNITY_2018_1_OR_NEWER && (NET_4_6 || NET_STANDARD_2_0)
  197. #region Async Instructions
  198. /// <summary>
  199. /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or complete.
  200. /// It can be used inside an async operation.
  201. /// <para>Example usage:</para><code>await myTween.WaitForCompletion();</code>
  202. /// </summary>
  203. public static async System.Threading.Tasks.Task AsyncWaitForCompletion(this Tween t)
  204. {
  205. if (!t.active) {
  206. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  207. return;
  208. }
  209. while (t.active && !t.IsComplete()) await System.Threading.Tasks.Task.Yield();
  210. }
  211. /// <summary>
  212. /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or rewinded.
  213. /// It can be used inside an async operation.
  214. /// <para>Example usage:</para><code>await myTween.AsyncWaitForRewind();</code>
  215. /// </summary>
  216. public static async System.Threading.Tasks.Task AsyncWaitForRewind(this Tween t)
  217. {
  218. if (!t.active) {
  219. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  220. return;
  221. }
  222. while (t.active && (!t.playedOnce || t.position * (t.CompletedLoops() + 1) > 0)) await System.Threading.Tasks.Task.Yield();
  223. }
  224. /// <summary>
  225. /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed.
  226. /// It can be used inside an async operation.
  227. /// <para>Example usage:</para><code>await myTween.AsyncWaitForKill();</code>
  228. /// </summary>
  229. public static async System.Threading.Tasks.Task AsyncWaitForKill(this Tween t)
  230. {
  231. if (!t.active) {
  232. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  233. return;
  234. }
  235. while (t.active) await System.Threading.Tasks.Task.Yield();
  236. }
  237. /// <summary>
  238. /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or has gone through the given amount of loops.
  239. /// It can be used inside an async operation.
  240. /// <para>Example usage:</para><code>await myTween.AsyncWaitForElapsedLoops();</code>
  241. /// </summary>
  242. /// <param name="elapsedLoops">Elapsed loops to wait for</param>
  243. public static async System.Threading.Tasks.Task AsyncWaitForElapsedLoops(this Tween t, int elapsedLoops)
  244. {
  245. if (!t.active) {
  246. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  247. return;
  248. }
  249. while (t.active && t.CompletedLoops() < elapsedLoops) await System.Threading.Tasks.Task.Yield();
  250. }
  251. /// <summary>
  252. /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed or started
  253. /// (meaning when the tween is set in a playing state the first time, after any eventual delay).
  254. /// It can be used inside an async operation.
  255. /// <para>Example usage:</para><code>await myTween.AsyncWaitForPosition();</code>
  256. /// </summary>
  257. /// <param name="position">Position (loops included, delays excluded) to wait for</param>
  258. public static async System.Threading.Tasks.Task AsyncWaitForPosition(this Tween t, float position)
  259. {
  260. if (!t.active) {
  261. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  262. return;
  263. }
  264. while (t.active && t.position * (t.CompletedLoops() + 1) < position) await System.Threading.Tasks.Task.Yield();
  265. }
  266. /// <summary>
  267. /// Returns an async <see cref="System.Threading.Tasks.Task"/> that waits until the tween is killed.
  268. /// It can be used inside an async operation.
  269. /// <para>Example usage:</para><code>await myTween.AsyncWaitForKill();</code>
  270. /// </summary>
  271. public static async System.Threading.Tasks.Task AsyncWaitForStart(this Tween t)
  272. {
  273. if (!t.active) {
  274. if (Debugger.logPriority > 0) Debugger.LogInvalidTween(t);
  275. return;
  276. }
  277. while (t.active && !t.playedOnce) await System.Threading.Tasks.Task.Yield();
  278. }
  279. #endregion
  280. #endif
  281. #endregion
  282. #endregion
  283. #endif
  284. }
  285. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  286. // ███ CLASSES █████████████████████████████████████████████████████████████████████████████████████████████████████████
  287. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  288. #if UNITY_5_3_OR_NEWER || UNITY_2017_1_OR_NEWER
  289. public static class DOTweenCYInstruction
  290. {
  291. public class WaitForCompletion : CustomYieldInstruction
  292. {
  293. public override bool keepWaiting { get {
  294. return t.active && !t.IsComplete();
  295. }}
  296. readonly Tween t;
  297. public WaitForCompletion(Tween tween)
  298. {
  299. t = tween;
  300. }
  301. }
  302. public class WaitForRewind : CustomYieldInstruction
  303. {
  304. public override bool keepWaiting { get {
  305. return t.active && (!t.playedOnce || t.position * (t.CompletedLoops() + 1) > 0);
  306. }}
  307. readonly Tween t;
  308. public WaitForRewind(Tween tween)
  309. {
  310. t = tween;
  311. }
  312. }
  313. public class WaitForKill : CustomYieldInstruction
  314. {
  315. public override bool keepWaiting { get {
  316. return t.active;
  317. }}
  318. readonly Tween t;
  319. public WaitForKill(Tween tween)
  320. {
  321. t = tween;
  322. }
  323. }
  324. public class WaitForElapsedLoops : CustomYieldInstruction
  325. {
  326. public override bool keepWaiting { get {
  327. return t.active && t.CompletedLoops() < elapsedLoops;
  328. }}
  329. readonly Tween t;
  330. readonly int elapsedLoops;
  331. public WaitForElapsedLoops(Tween tween, int elapsedLoops)
  332. {
  333. t = tween;
  334. this.elapsedLoops = elapsedLoops;
  335. }
  336. }
  337. public class WaitForPosition : CustomYieldInstruction
  338. {
  339. public override bool keepWaiting { get {
  340. return t.active && t.position * (t.CompletedLoops() + 1) < position;
  341. }}
  342. readonly Tween t;
  343. readonly float position;
  344. public WaitForPosition(Tween tween, float position)
  345. {
  346. t = tween;
  347. this.position = position;
  348. }
  349. }
  350. public class WaitForStart : CustomYieldInstruction
  351. {
  352. public override bool keepWaiting { get {
  353. return t.active && !t.playedOnce;
  354. }}
  355. readonly Tween t;
  356. public WaitForStart(Tween tween)
  357. {
  358. t = tween;
  359. }
  360. }
  361. }
  362. #endif
  363. }