-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCustomFixedUpdate.cs
More file actions
72 lines (64 loc) · 1.97 KB
/
Copy pathCustomFixedUpdate.cs
File metadata and controls
72 lines (64 loc) · 1.97 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using UnityEngine;
using System.Collections;
public class CustomFixedUpdate
{
public delegate void OnFixedUpdateCallback(float aDeltaTime);
private float m_FixedTimeStep;
private float m_Timer = 0;
private OnFixedUpdateCallback m_Callback;
private float m_MaxAllowedTimeStep = 0f;
public float MaxAllowedTimeStep
{
get { return m_MaxAllowedTimeStep; }
set { m_MaxAllowedTimeStep = value;}
}
public float deltaTime
{
get { return m_FixedTimeStep; }
set {m_FixedTimeStep = Mathf.Max (value, 0.000001f); } // max rate: 1000000
}
public float updateRate
{
get { return 1.0f / deltaTime; }
set { deltaTime = 1.0f / value; }
}
public CustomFixedUpdate(float aTimeStep, OnFixedUpdateCallback aCallback, float aMaxAllowedTimestep)
{
if (aCallback == null)
throw new System.ArgumentException("CustomFixedUpdate needs a valid callback");
if (aTimeStep <= 0f)
throw new System.ArgumentException("TimeStep needs to be greater than 0");
deltaTime = aTimeStep;
m_Callback = aCallback;
m_MaxAllowedTimeStep = aMaxAllowedTimestep;
}
public CustomFixedUpdate(float aTimeStep, OnFixedUpdateCallback aCallback) : this(aTimeStep, aCallback, 0f) {}
public CustomFixedUpdate(OnFixedUpdateCallback aCallback) : this(0.01f, aCallback, 0f) {}
public CustomFixedUpdate(OnFixedUpdateCallback aCallback, float aFPS, float aMaxAllowedTimestep) : this(1f/aFPS, aCallback, aMaxAllowedTimestep){}
public CustomFixedUpdate(OnFixedUpdateCallback aCallback, float aFPS) : this(aCallback, aFPS, 0f){}
public void Update(float aDeltaTime)
{
m_Timer -= aDeltaTime;
if (m_MaxAllowedTimeStep > 0)
{
float timeout = Time.realtimeSinceStartup + m_MaxAllowedTimeStep;
while(m_Timer < 0f && Time.realtimeSinceStartup < timeout)
{
m_Callback(m_FixedTimeStep);
m_Timer += m_FixedTimeStep;
}
}
else
{
while(m_Timer < 0f)
{
m_Callback(m_FixedTimeStep);
m_Timer += m_FixedTimeStep;
}
}
}
public void Update()
{
Update(Time.deltaTime);
}
}