-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAtomScheduler.cs
84 lines (69 loc) · 2.3 KB
/
AtomScheduler.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
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
73
74
75
76
77
78
79
80
81
82
83
84
using System.Collections.Generic;
using System.Diagnostics;
using Unity.IL2CPP.CompilerServices;
using UnityEngine;
using UnityEngine.Profiling;
namespace UniMob.Core
{
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
public sealed class AtomScheduler : MonoBehaviour
{
public static readonly Stopwatch SyncTimer = new Stopwatch();
#if UNITY_EDITOR || DEVELOPMENT_BUILD
private static readonly CustomSampler ProfilerSampler = CustomSampler.Create("UniMob.Sync");
#endif
private static Queue<AtomBase> _updatingCurrentFrame = new Queue<AtomBase>();
private static Queue<AtomBase> _updatingNextFrame = new Queue<AtomBase>();
private static AtomScheduler _current;
private static bool _dirty;
private void Update()
{
if (!_dirty)
{
return;
}
_dirty = false;
Sync();
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Initialize()
{
_current = null;
_dirty = false;
}
internal static void Actualize(AtomBase atom)
{
_dirty = true;
_updatingNextFrame.Enqueue(atom);
if (ReferenceEquals(_current, null) && Application.isPlaying)
{
var go = new GameObject(nameof(AtomScheduler));
_current = go.AddComponent<AtomScheduler>();
DontDestroyOnLoad(_current);
}
}
public static void Sync()
{
#if UNITY_EDITOR || DEVELOPMENT_BUILD
ProfilerSampler.Begin();
#endif
SyncTimer.Restart();
var toSwap = _updatingCurrentFrame;
_updatingCurrentFrame = _updatingNextFrame;
_updatingNextFrame = toSwap;
while (_updatingCurrentFrame.Count > 0)
{
var atom = _updatingCurrentFrame.Dequeue();
if (atom.options.Has(AtomOptions.Active) && atom.state != AtomState.Actual)
{
atom.Actualize();
}
}
SyncTimer.Stop();
#if UNITY_EDITOR || DEVELOPMENT_BUILD
ProfilerSampler.End();
#endif
}
}
}