SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
13 KiB

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