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