diff --git a/Package/Utility/Runtime/Utils/TimeScaler.cs b/Package/Utility/Runtime/Utils/TimeScaler.cs new file mode 100644 index 0000000..da17d69 --- /dev/null +++ b/Package/Utility/Runtime/Utils/TimeScaler.cs @@ -0,0 +1,43 @@ +using UnityEngine; + +namespace Joyixir.Utils +{ + public class TimeScaler : MonoBehaviour + { + private static float _defaultFixedDeltaTime; + private static bool _timeScalerInitialized; + private static bool _timeAlreadyScaled; + + private void Awake() + { + if (_timeScalerInitialized) return; + _defaultFixedDeltaTime = Time.fixedDeltaTime; + _timeScalerInitialized = true; + } + + public static void ScaleUnityTime(float slowdownFactor) + { + if(!_timeScalerInitialized) + Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't."); + if (_timeAlreadyScaled) + { + Debug.LogWarning("Time is already scaled, pretending to scale unity default time scale."); + SetUnityTimeScalesToNormal(); + } + + Time.timeScale = slowdownFactor; + Time.fixedDeltaTime = Time.timeScale * _defaultFixedDeltaTime; + _timeAlreadyScaled = true; + } + + public static void SetUnityTimeScalesToNormal() + { + if(!_timeScalerInitialized) + Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't."); + if (!_timeAlreadyScaled) return; + Time.timeScale = 1; + Time.fixedDeltaTime = _defaultFixedDeltaTime; + _timeAlreadyScaled = false; + } + } +} \ No newline at end of file diff --git a/Package/Utility/Runtime/Utils/TimeScaler.cs.meta b/Package/Utility/Runtime/Utils/TimeScaler.cs.meta new file mode 100644 index 0000000..ef9da31 --- /dev/null +++ b/Package/Utility/Runtime/Utils/TimeScaler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 120b8ec436d894ecd9fa444741d49387 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Package/Utility/Runtime/Utils/Utils.cs b/Package/Utility/Runtime/Utils/Utils.cs index 4f08661..788b94d 100755 --- a/Package/Utility/Runtime/Utils/Utils.cs +++ b/Package/Utility/Runtime/Utils/Utils.cs @@ -139,8 +139,8 @@ public static string GetCurrentStateName(this Animator animator, int layer = 0) var info = animator.GetCurrentAnimatorStateInfo(layer); return (from clip in animator.runtimeAnimatorController.animationClips - where info.IsName(clip.name) - select clip.name).FirstOrDefault(); + where info.IsName(clip.name) + select clip.name).FirstOrDefault(); } public static void ResetAllTriggers(this Animator animator) @@ -255,5 +255,32 @@ public static AnimationCurve GenerateCurveFromVector2(this AnimationCurve thisCu return thisCurve; } + + public static float GetCurrentClipLength(this Animator animator, int layer = 0) + { + var info = animator.GetCurrentAnimatorStateInfo(layer); + + return (from clip in animator.runtimeAnimatorController.animationClips + where info.IsName(clip.name) + select clip.length).FirstOrDefault(); + } + + public static bool IsInState(Animator animator, string stateName, out AnimatorStateInfo animatorStateInfo) + { + if (animator != null) + { + for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++) + { + AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layerIndex); + if (info.IsName(stateName)) + { + animatorStateInfo = info; + return true; + } + } + } + animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0); + return false; + } } } \ No newline at end of file diff --git a/Package/Utility/Tests/com.joyixir.utility.Tests.asmdef b/Package/Utility/Tests/com.joyixir.utility.Tests.asmdef index 281d7ef..bc44fe4 100644 --- a/Package/Utility/Tests/com.joyixir.utility.Tests.asmdef +++ b/Package/Utility/Tests/com.joyixir.utility.Tests.asmdef @@ -1,3 +1,20 @@ { - "name": "com.joyixir.utility.Tests" -} + "name": "com.joyixir.utility.Tests", + "rootNamespace": "", + "references": [ + "GUID:40a991db332574dbfb8099ed06e34f25", + "GUID:7c04f0dfa9243c04681a55d90d3ff3fc", + "GUID:e9745f6a32442194c8dc5a43e9ab86f9" + ], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Package/Utility/package.json b/Package/Utility/package.json index 2e98f55..8b6cf58 100644 --- a/Package/Utility/package.json +++ b/Package/Utility/package.json @@ -1,6 +1,6 @@ { "name": "com.joyixir.utility", - "version": "1.0.0", + "version": "1.0.2", "displayName": "Utility", "description": "Utility package containing Animator, Vector, List etc. extensions to help developers.", "documentationUrl": "https://github.com/joyixir/utility", @@ -17,5 +17,9 @@ "email": "dev@joyixir.com", "url": "https://www.joyixir.com" }, - "type": "tool" -} + "type": "tool", + "repository": { + "type" : "git", + "url" : "https://github.com/joyixir/utility.git" + } +} \ No newline at end of file diff --git a/README.md b/README.md index bda8539..bd63640 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,21 @@ Open Window/PackageManager and head to My Registries. Install your desired versi You can just clone the repo and do whatever you like with it. Even to make it better. +# Use +### Utils +Just look around to see if there's any useful extensions for you. There are extensions for AnimationCurve, Vector3, List, Animator, IEnumerable, Collider Bounds, KeyFrame + +There are also some handy functions that you can use(Normalizers, etc). +### TimeScaler +Attach TimeScaler to a GameObject +Do +```csharp +TimeScaler.ScaleUnityTime(float: slowDownFactor); +``` +To undo any scale factor, do: +```csharp +TimeScaler.SetUnityTimeScalesToNormal(); +``` ## License MIT \ No newline at end of file diff --git a/Utility/Assets/Joyixir/Utility/Utils/TimeScaler.cs b/Utility/Assets/Joyixir/Utility/Utils/TimeScaler.cs index 4d12c66..da17d69 100644 --- a/Utility/Assets/Joyixir/Utility/Utils/TimeScaler.cs +++ b/Utility/Assets/Joyixir/Utility/Utils/TimeScaler.cs @@ -17,7 +17,7 @@ private void Awake() public static void ScaleUnityTime(float slowdownFactor) { - if (!_timeScalerInitialized) + if(!_timeScalerInitialized) Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't."); if (_timeAlreadyScaled) { @@ -32,7 +32,7 @@ public static void ScaleUnityTime(float slowdownFactor) public static void SetUnityTimeScalesToNormal() { - if (!_timeScalerInitialized) + if(!_timeScalerInitialized) Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't."); if (!_timeAlreadyScaled) return; Time.timeScale = 1; diff --git a/Utility/Assets/Joyixir/Utility/Utils/Utils.cs b/Utility/Assets/Joyixir/Utility/Utils/Utils.cs index b82a0d3..788b94d 100755 --- a/Utility/Assets/Joyixir/Utility/Utils/Utils.cs +++ b/Utility/Assets/Joyixir/Utility/Utils/Utils.cs @@ -255,5 +255,32 @@ public static AnimationCurve GenerateCurveFromVector2(this AnimationCurve thisCu return thisCurve; } + + public static float GetCurrentClipLength(this Animator animator, int layer = 0) + { + var info = animator.GetCurrentAnimatorStateInfo(layer); + + return (from clip in animator.runtimeAnimatorController.animationClips + where info.IsName(clip.name) + select clip.length).FirstOrDefault(); + } + + public static bool IsInState(Animator animator, string stateName, out AnimatorStateInfo animatorStateInfo) + { + if (animator != null) + { + for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++) + { + AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layerIndex); + if (info.IsName(stateName)) + { + animatorStateInfo = info; + return true; + } + } + } + animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0); + return false; + } } } \ No newline at end of file