Skip to content

Commit e75aab1

Browse files
Merge branch 'release/1.0.1'
2 parents 05048e8 + 180711d commit e75aab1

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
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+
}

Utility/Assets/Joyixir/Utility/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.

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

Lines changed: 2 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)

0 commit comments

Comments
 (0)