Skip to content

Commit e67637f

Browse files
Merge branch 'release/1.0.2'
2 parents 180711d + 7ab5c7a commit e67637f

File tree

8 files changed

+153
-9
lines changed

8 files changed

+153
-9
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using UnityEngine;
2+
3+
namespace Joyixir.Utils
4+
{
5+
public class TimeScaler : MonoBehaviour
6+
{
7+
private static float _defaultFixedDeltaTime;
8+
private static bool _timeScalerInitialized;
9+
private static bool _timeAlreadyScaled;
10+
11+
private void Awake()
12+
{
13+
if (_timeScalerInitialized) return;
14+
_defaultFixedDeltaTime = Time.fixedDeltaTime;
15+
_timeScalerInitialized = true;
16+
}
17+
18+
public static void ScaleUnityTime(float slowdownFactor)
19+
{
20+
if(!_timeScalerInitialized)
21+
Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't.");
22+
if (_timeAlreadyScaled)
23+
{
24+
Debug.LogWarning("Time is already scaled, pretending to scale unity default time scale.");
25+
SetUnityTimeScalesToNormal();
26+
}
27+
28+
Time.timeScale = slowdownFactor;
29+
Time.fixedDeltaTime = Time.timeScale * _defaultFixedDeltaTime;
30+
_timeAlreadyScaled = true;
31+
}
32+
33+
public static void SetUnityTimeScalesToNormal()
34+
{
35+
if(!_timeScalerInitialized)
36+
Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't.");
37+
if (!_timeAlreadyScaled) return;
38+
Time.timeScale = 1;
39+
Time.fixedDeltaTime = _defaultFixedDeltaTime;
40+
_timeAlreadyScaled = false;
41+
}
42+
}
43+
}

Package/Utility/Runtime/Utils/TimeScaler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package/Utility/Runtime/Utils/Utils.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public static string GetCurrentStateName(this Animator animator, int layer = 0)
139139
var info = animator.GetCurrentAnimatorStateInfo(layer);
140140

141141
return (from clip in animator.runtimeAnimatorController.animationClips
142-
where info.IsName(clip.name)
143-
select clip.name).FirstOrDefault();
142+
where info.IsName(clip.name)
143+
select clip.name).FirstOrDefault();
144144
}
145145

146146
public static void ResetAllTriggers(this Animator animator)
@@ -255,5 +255,32 @@ public static AnimationCurve GenerateCurveFromVector2(this AnimationCurve thisCu
255255

256256
return thisCurve;
257257
}
258+
259+
public static float GetCurrentClipLength(this Animator animator, int layer = 0)
260+
{
261+
var info = animator.GetCurrentAnimatorStateInfo(layer);
262+
263+
return (from clip in animator.runtimeAnimatorController.animationClips
264+
where info.IsName(clip.name)
265+
select clip.length).FirstOrDefault();
266+
}
267+
268+
public static bool IsInState(Animator animator, string stateName, out AnimatorStateInfo animatorStateInfo)
269+
{
270+
if (animator != null)
271+
{
272+
for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++)
273+
{
274+
AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layerIndex);
275+
if (info.IsName(stateName))
276+
{
277+
animatorStateInfo = info;
278+
return true;
279+
}
280+
}
281+
}
282+
animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
283+
return false;
284+
}
258285
}
259286
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
{
2-
"name": "com.joyixir.utility.Tests"
3-
}
2+
"name": "com.joyixir.utility.Tests",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:40a991db332574dbfb8099ed06e34f25",
6+
"GUID:7c04f0dfa9243c04681a55d90d3ff3fc",
7+
"GUID:e9745f6a32442194c8dc5a43e9ab86f9"
8+
],
9+
"includePlatforms": [
10+
"Editor"
11+
],
12+
"excludePlatforms": [],
13+
"allowUnsafeCode": false,
14+
"overrideReferences": false,
15+
"precompiledReferences": [],
16+
"autoReferenced": true,
17+
"defineConstraints": [],
18+
"versionDefines": [],
19+
"noEngineReferences": false
20+
}

Package/Utility/package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.joyixir.utility",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"displayName": "Utility",
55
"description": "Utility package containing Animator, Vector, List etc. extensions to help developers.",
66
"documentationUrl": "https://github.com/joyixir/utility",
@@ -17,5 +17,9 @@
1717
"email": "[email protected]",
1818
"url": "https://www.joyixir.com"
1919
},
20-
"type": "tool"
21-
}
20+
"type": "tool",
21+
"repository": {
22+
"type" : "git",
23+
"url" : "https://github.com/joyixir/utility.git"
24+
}
25+
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ Open Window/PackageManager and head to My Registries. Install your desired versi
3737
You can just clone the repo and do whatever you like with it. Even to make it better.
3838

3939

40+
# Use
41+
### Utils
42+
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
43+
44+
There are also some handy functions that you can use(Normalizers, etc).
45+
### TimeScaler
46+
Attach TimeScaler to a GameObject
47+
Do
48+
```csharp
49+
TimeScaler.ScaleUnityTime(float: slowDownFactor);
50+
```
51+
To undo any scale factor, do:
52+
```csharp
53+
TimeScaler.SetUnityTimeScalesToNormal();
54+
```
4055
## License
4156

4257
MIT

Utility/Assets/Joyixir/Utility/Utils/TimeScaler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private void Awake()
1717

1818
public static void ScaleUnityTime(float slowdownFactor)
1919
{
20-
if (!_timeScalerInitialized)
20+
if(!_timeScalerInitialized)
2121
Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't.");
2222
if (_timeAlreadyScaled)
2323
{
@@ -32,7 +32,7 @@ public static void ScaleUnityTime(float slowdownFactor)
3232

3333
public static void SetUnityTimeScalesToNormal()
3434
{
35-
if (!_timeScalerInitialized)
35+
if(!_timeScalerInitialized)
3636
Debug.LogError("Attach timescaler to a unity game object, you may see unexpected behavior if you don't.");
3737
if (!_timeAlreadyScaled) return;
3838
Time.timeScale = 1;

Utility/Assets/Joyixir/Utility/Utils/Utils.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,32 @@ public static AnimationCurve GenerateCurveFromVector2(this AnimationCurve thisCu
255255

256256
return thisCurve;
257257
}
258+
259+
public static float GetCurrentClipLength(this Animator animator, int layer = 0)
260+
{
261+
var info = animator.GetCurrentAnimatorStateInfo(layer);
262+
263+
return (from clip in animator.runtimeAnimatorController.animationClips
264+
where info.IsName(clip.name)
265+
select clip.length).FirstOrDefault();
266+
}
267+
268+
public static bool IsInState(Animator animator, string stateName, out AnimatorStateInfo animatorStateInfo)
269+
{
270+
if (animator != null)
271+
{
272+
for (int layerIndex = 0; layerIndex < animator.layerCount; layerIndex++)
273+
{
274+
AnimatorStateInfo info = animator.GetCurrentAnimatorStateInfo(layerIndex);
275+
if (info.IsName(stateName))
276+
{
277+
animatorStateInfo = info;
278+
return true;
279+
}
280+
}
281+
}
282+
animatorStateInfo = animator.GetCurrentAnimatorStateInfo(0);
283+
return false;
284+
}
258285
}
259286
}

0 commit comments

Comments
 (0)