This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
RequireLifecycleSystem.cs
66 lines (54 loc) · 2.22 KB
/
RequireLifecycleSystem.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
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
namespace Improbable.Gdk.Subscriptions
{
[DisableAutoCreation]
[UpdateInGroup(typeof(RequireLifecycleGroup))]
public class RequireLifecycleSystem : ComponentSystem
{
private readonly List<MonoBehaviour> behavioursToEnable = new List<MonoBehaviour>();
private CommandCallbackSystem commandCallbackSystem;
private ComponentCallbackSystem componentCallbackSystem;
private ComponentConstraintsCallbackSystem componentConstraintsCallbackSystem;
private WorkerFlagCallbackSystem workerFlagCallbackSystem;
// todo make this order the behaviours somehow
internal void EnableMonoBehaviour(MonoBehaviour behaviour)
{
behavioursToEnable.Add(behaviour);
}
internal void DisableMonoBehaviour(MonoBehaviour behaviour)
{
// Behaviours can be disabled immediately, as conflicting information can not arrive in the same frame.
behaviour.enabled = false;
behavioursToEnable.Remove(behaviour);
}
protected override void OnCreate()
{
base.OnCreate();
commandCallbackSystem = World.GetExistingSystem<CommandCallbackSystem>();
componentCallbackSystem = World.GetExistingSystem<ComponentCallbackSystem>();
componentConstraintsCallbackSystem = World.GetExistingSystem<ComponentConstraintsCallbackSystem>();
workerFlagCallbackSystem = World.GetExistingSystem<WorkerFlagCallbackSystem>();
}
protected override void OnUpdate()
{
componentConstraintsCallbackSystem.Invoke();
componentCallbackSystem.InvokeCallbacks();
if (behavioursToEnable.Count > 0)
{
foreach (var behaviour in behavioursToEnable)
{
if (behaviour == null)
{
continue;
}
behaviour.enabled = true;
}
behavioursToEnable.Clear();
}
commandCallbackSystem.InvokeCallbacks();
workerFlagCallbackSystem.InvokeCallbacks();
}
}
}