-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConstantDeltaTimeSystem.cs
46 lines (41 loc) · 1.42 KB
/
ConstantDeltaTimeSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using Unity.Core;
using Unity.Entities;
namespace E7.EcsTesting
{
/// <summary>
/// This is for test only. It disables the regular one and can hack any delta time instead.
/// </summary>
[UpdateInGroup(typeof(InitializationSystemGroup))]
[UpdateAfter(typeof(UpdateWorldTimeSystem))]
[DisableAutoCreation]
public class ConstantDeltaTimeSystem : ComponentSystem
{
float defaultDeltaTime = 1 / 60f;
private float simulatedElapsedTime;
private float constantDeltaTime;
protected override void OnCreate()
{
//Only works if Unity's time system is there already, this would
//try to replace it. You should not add this system before Unity's.
var timeSystem = World.GetExistingSystem<UpdateWorldTimeSystem>();
if (timeSystem != null)
{
timeSystem.Enabled = false;
}
this.constantDeltaTime = defaultDeltaTime;
}
public void ForceDeltaTime(float dt)
{
this.constantDeltaTime = dt;
}
public void RestoreDeltaTime() => this.constantDeltaTime = defaultDeltaTime;
protected override void OnUpdate()
{
simulatedElapsedTime += constantDeltaTime;
World.SetTime(new TimeData(
elapsedTime: simulatedElapsedTime,
deltaTime: constantDeltaTime
));
}
}
}