DOTweenModuleUtils.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.
  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. static bool _initialized;
  27. #region Reflection
  28. /// <summary>
  29. /// Called via Reflection by DOTweenComponent on Awake
  30. /// </summary>
  31. #if UNITY_2018_1_OR_NEWER
  32. [UnityEngine.Scripting.Preserve]
  33. #endif
  34. public static void Init()
  35. {
  36. if (_initialized) return;
  37. _initialized = true;
  38. DOTweenExternalCommand.SetOrientationOnPath += Physics.SetOrientationOnPath;
  39. #if UNITY_EDITOR
  40. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1
  41. UnityEditor.EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
  42. #else
  43. UnityEditor.EditorApplication.playModeStateChanged += PlaymodeStateChanged;
  44. #endif
  45. #endif
  46. }
  47. #if UNITY_2018_1_OR_NEWER
  48. #pragma warning disable
  49. [UnityEngine.Scripting.Preserve]
  50. // Just used to preserve methods when building, never called
  51. static void Preserver()
  52. {
  53. Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
  54. MethodInfo mi = typeof(MonoBehaviour).GetMethod("Stub");
  55. }
  56. #pragma warning restore
  57. #endif
  58. #endregion
  59. #if UNITY_EDITOR
  60. // Fires OnApplicationPause in DOTweenComponent even when Editor is paused (otherwise it's only fired at runtime)
  61. #if UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5 || UNITY_2017_1
  62. static void PlaymodeStateChanged()
  63. #else
  64. static void PlaymodeStateChanged(UnityEditor.PlayModeStateChange state)
  65. #endif
  66. {
  67. if (DOTween.instance == null) return;
  68. DOTween.instance.OnApplicationPause(UnityEditor.EditorApplication.isPaused);
  69. }
  70. #endif
  71. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  72. // ███ INTERNAL CLASSES ████████████████████████████████████████████████████████████████████████████████████████████████
  73. // █████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
  74. public static class Physics
  75. {
  76. // Called via DOTweenExternalCommand callback
  77. public static void SetOrientationOnPath(PathOptions options, Tween t, Quaternion newRot, Transform trans)
  78. {
  79. #if true // PHYSICS_MARKER
  80. if (options.isRigidbody) ((Rigidbody)t.target).rotation = newRot;
  81. else trans.rotation = newRot;
  82. #else
  83. trans.rotation = newRot;
  84. #endif
  85. }
  86. // Returns FALSE if the DOTween's Physics2D Module is disabled, or if there's no Rigidbody2D attached
  87. public static bool HasRigidbody2D(Component target)
  88. {
  89. #if true // PHYSICS2D_MARKER
  90. return target.GetComponent<Rigidbody2D>() != null;
  91. #else
  92. return false;
  93. #endif
  94. }
  95. #region Called via Reflection
  96. // Called via Reflection by DOTweenPathInspector
  97. // Returns FALSE if the DOTween's Physics Module is disabled, or if there's no rigidbody attached
  98. #if UNITY_2018_1_OR_NEWER
  99. [UnityEngine.Scripting.Preserve]
  100. #endif
  101. public static bool HasRigidbody(Component target)
  102. {
  103. #if true // PHYSICS_MARKER
  104. return target.GetComponent<Rigidbody>() != null;
  105. #else
  106. return false;
  107. #endif
  108. }
  109. // Called via Reflection by DOTweenPath
  110. #if UNITY_2018_1_OR_NEWER
  111. [UnityEngine.Scripting.Preserve]
  112. #endif
  113. public static TweenerCore<Vector3, Path, PathOptions> CreateDOTweenPathTween(
  114. MonoBehaviour target, bool tweenRigidbody, bool isLocal, Path path, float duration, PathMode pathMode
  115. ){
  116. TweenerCore<Vector3, Path, PathOptions> t = null;
  117. bool rBodyFoundAndTweened = false;
  118. #if true // PHYSICS_MARKER
  119. if (tweenRigidbody) {
  120. Rigidbody rBody = target.GetComponent<Rigidbody>();
  121. if (rBody != null) {
  122. rBodyFoundAndTweened = true;
  123. t = isLocal
  124. ? rBody.DOLocalPath(path, duration, pathMode)
  125. : rBody.DOPath(path, duration, pathMode);
  126. }
  127. }
  128. #endif
  129. #if true // PHYSICS2D_MARKER
  130. if (!rBodyFoundAndTweened && tweenRigidbody) {
  131. Rigidbody2D rBody2D = target.GetComponent<Rigidbody2D>();
  132. if (rBody2D != null) {
  133. rBodyFoundAndTweened = true;
  134. t = isLocal
  135. ? rBody2D.DOLocalPath(path, duration, pathMode)
  136. : rBody2D.DOPath(path, duration, pathMode);
  137. }
  138. }
  139. #endif
  140. if (!rBodyFoundAndTweened) {
  141. t = isLocal
  142. ? target.transform.DOLocalPath(path, duration, pathMode)
  143. : target.transform.DOPath(path, duration, pathMode);
  144. }
  145. return t;
  146. }
  147. #endregion
  148. }
  149. }
  150. }