DOTweenModuleUtils.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2018/07/13
  3. using System;
  4. using System.Reflection;
  5. using UnityEngine;
  6. using DG.Tweening.Core;
  7. using DG.Tweening.Plugins.Core.PathCore;
  8. using DG.Tweening.Plugins.Options;
  9. #pragma warning disable 1591
  10. namespace DG.Tweening
  11. {
  12. /// <summary>
  13. /// Utility functions that deal with available Modules and rewrite them.
  14. /// Modules defines:
  15. /// - DOTAUDIO
  16. /// - DOTPHYSICS
  17. /// - DOTPHYSICS2D
  18. /// - DOTSPRITE
  19. /// - DOTUI
  20. /// Extra defines set and used for implementation of external assets:
  21. /// - DOTWEEN_TMP ► TextMesh Pro
  22. /// - DOTWEEN_TK2D ► 2D Toolkit
  23. /// </summary>
  24. public static class DOTweenModuleUtils
  25. {
  26. #pragma warning disable UDR0001
  27. static bool _initialized;
  28. #pragma warning restore UDR0001
  29. #region Reflection
  30. /// <summary>
  31. /// Called via Reflection by DOTweenComponent on Awake
  32. /// </summary>
  33. #pragma warning disable UDR0001
  34. #if UNITY_2018_1_OR_NEWER
  35. [UnityEngine.Scripting.Preserve]
  36. #endif
  37. public static void Init()
  38. {
  39. if (_initialized) return;
  40. _initialized = true;
  41. DOTweenExternalCommand.SetOrientationOnPath -= Physics.SetOrientationOnPath;
  42. DOTweenExternalCommand.SetOrientationOnPath += Physics.SetOrientationOnPath;
  43. #if UNITY_EDITOR
  44. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1
  45. UnityEditor.EditorApplication.playmodeStateChanged -= PlaymodeStateChanged;
  46. UnityEditor.EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
  47. #else
  48. UnityEditor.EditorApplication.playModeStateChanged -= PlaymodeStateChanged;
  49. UnityEditor.EditorApplication.playModeStateChanged += PlaymodeStateChanged;
  50. #endif
  51. #endif
  52. }
  53. #pragma warning restore UDR0001
  54. #if UNITY_2018_1_OR_NEWER
  55. #pragma warning disable
  56. [UnityEngine.Scripting.Preserve]
  57. // Just used to preserve methods when building, never called
  58. static void Preserver()
  59. {
  60. Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
  61. MethodInfo mi = typeof(MonoBehaviour).GetMethod("Stub");
  62. }
  63. #pragma warning restore
  64. #endif
  65. #endregion
  66. #if UNITY_EDITOR
  67. // Fires OnApplicationPause in DOTweenComponent even when Editor is paused (otherwise it's only fired at runtime)
  68. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1
  69. static void PlaymodeStateChanged()
  70. #else
  71. static void PlaymodeStateChanged(UnityEditor.PlayModeStateChange state)
  72. #endif
  73. {
  74. if (DOTween.instance == null) return;
  75. DOTween.instance.OnApplicationPause(UnityEditor.EditorApplication.isPaused);
  76. }
  77. #endif
  78. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  79. // ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
  80. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  81. public static class Physics
  82. {
  83. // Called via DOTweenExternalCommand callback
  84. public static void SetOrientationOnPath(PathOptions options, Tween t, Quaternion newRot, Transform trans)
  85. {
  86. #if !DOTWEEN_NOPHYSICS // PHYSICS_MARKER
  87. if (options.isRigidbody) ((Rigidbody)t.target).rotation = newRot;
  88. else trans.rotation = newRot;
  89. #else
  90. trans.rotation = newRot;
  91. #endif
  92. }
  93. // Returns FALSE if the DOTween's Physics2D Module is disabled, or if there's no Rigidbody2D attached
  94. public static bool HasRigidbody2D(Component target)
  95. {
  96. #if !DOTWEEN_NOPHYSICS2D // PHYSICS2D_MARKER
  97. return target.GetComponent<Rigidbody2D>() != null;
  98. #else
  99. return false;
  100. #endif
  101. }
  102. #region Called via Reflection
  103. // Called via Reflection by DOTweenPathInspector
  104. // Returns FALSE if the DOTween's Physics Module is disabled, or if there's no rigidbody attached
  105. #if UNITY_2018_1_OR_NEWER
  106. [UnityEngine.Scripting.Preserve]
  107. #endif
  108. public static bool HasRigidbody(Component target)
  109. {
  110. #if !DOTWEEN_NOPHYSICS // PHYSICS_MARKER
  111. return target.GetComponent<Rigidbody>() != null;
  112. #else
  113. return false;
  114. #endif
  115. }
  116. // Called via Reflection by DOTweenPath
  117. #if UNITY_2018_1_OR_NEWER
  118. [UnityEngine.Scripting.Preserve]
  119. #endif
  120. public static TweenerCore<Vector3, Path, PathOptions> CreateDOTweenPathTween(
  121. MonoBehaviour target, bool tweenRigidbody, bool isLocal, Path path, float duration, PathMode pathMode
  122. ){
  123. TweenerCore<Vector3, Path, PathOptions> t = null;
  124. bool rBodyFoundAndTweened = false;
  125. #if !DOTWEEN_NOPHYSICS // PHYSICS_MARKER
  126. if (tweenRigidbody) {
  127. Rigidbody rBody = target.GetComponent<Rigidbody>();
  128. if (rBody != null) {
  129. rBodyFoundAndTweened = true;
  130. t = isLocal
  131. ? rBody.DOLocalPath(path, duration, pathMode)
  132. : rBody.DOPath(path, duration, pathMode);
  133. }
  134. }
  135. #endif
  136. #if !DOTWEEN_NOPHYSICS2D // PHYSICS2D_MARKER
  137. if (!rBodyFoundAndTweened && tweenRigidbody) {
  138. Rigidbody2D rBody2D = target.GetComponent<Rigidbody2D>();
  139. if (rBody2D != null) {
  140. rBodyFoundAndTweened = true;
  141. t = isLocal
  142. ? rBody2D.DOLocalPath(path, duration, pathMode)
  143. : rBody2D.DOPath(path, duration, pathMode);
  144. }
  145. }
  146. #endif
  147. if (!rBodyFoundAndTweened) {
  148. t = isLocal
  149. ? target.transform.DOLocalPath(path, duration, pathMode)
  150. : target.transform.DOPath(path, duration, pathMode);
  151. }
  152. return t;
  153. }
  154. #endregion
  155. }
  156. }
  157. }