Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
amirhosseinjoyixir committed Aug 30, 2021
2 parents 180711d + 7ab5c7a commit e67637f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 9 deletions.
43 changes: 43 additions & 0 deletions Package/Utility/Runtime/Utils/TimeScaler.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions Package/Utility/Runtime/Utils/TimeScaler.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions Package/Utility/Runtime/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}
}
21 changes: 19 additions & 2 deletions Package/Utility/Tests/com.joyixir.utility.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 7 additions & 3 deletions Package/Utility/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -17,5 +17,9 @@
"email": "[email protected]",
"url": "https://www.joyixir.com"
},
"type": "tool"
}
"type": "tool",
"repository": {
"type" : "git",
"url" : "https://github.com/joyixir/utility.git"
}
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color>, 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
4 changes: 2 additions & 2 deletions Utility/Assets/Joyixir/Utility/Utils/TimeScaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions Utility/Assets/Joyixir/Utility/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit e67637f

Please sign in to comment.