DOTweenModuleUtils.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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;
  117. #if true // PHYSICS_MARKER
  118. Rigidbody rBody = tweenRigidbody ? target.GetComponent<Rigidbody>() : null;
  119. if (tweenRigidbody && rBody != null) {
  120. t = isLocal
  121. ? rBody.DOLocalPath(path, duration, pathMode)
  122. : rBody.DOPath(path, duration, pathMode);
  123. } else {
  124. t = isLocal
  125. ? target.transform.DOLocalPath(path, duration, pathMode)
  126. : target.transform.DOPath(path, duration, pathMode);
  127. }
  128. #else
  129. t = isLocal
  130. ? target.transform.DOLocalPath(path, duration, pathMode)
  131. : target.transform.DOPath(path, duration, pathMode);
  132. #endif
  133. return t;
  134. }
  135. #endregion
  136. }
  137. }
  138. }