This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEgoSystems.cs
105 lines (90 loc) · 2.81 KB
/
EgoSystems.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
public static class EgoSystems
{
static EgoSystem[] _systems = new EgoSystem[]{};
public static EgoSystem[] systems { get { return _systems; } }
public static void Add( params EgoSystem[] systems )
{
_systems = systems;
}
public static void Start()
{
var sceneCount = SceneManager.sceneCount;
for( var sceneIndex = 0; sceneIndex < sceneCount; sceneIndex++ )
{
var scene = SceneManager.GetSceneAt( sceneIndex );
var rootGameObjects = scene.GetRootGameObjects();
// Attach an EgoComponent Component to every GameObject in the scene
foreach( var go in rootGameObjects )
{
InitEgoComponent( go );
}
// Add every GameObject to any relevant system
foreach( var system in _systems )
{
foreach( var go in rootGameObjects )
{
var egoComponent = go.GetComponent<EgoComponent>();
system.CreateBundles( egoComponent );
}
}
}
EgoEvents.Start();
// Start all Systems
foreach( var system in _systems )
{
system.Start();
}
// Invoke all queued Events
EgoEvents.Invoke();
// Clean up Destroyed Components & GameObjects
EgoCleanUp.CleanUp();
}
/// <summary>
/// Attaches and Initializes an EgoComponent on the given transform
/// and all of its children (recursively)
/// </summary>
/// <param name="transform"></param>
static void InitEgoComponent( GameObject gameObject )
{
var egoComponent = gameObject.GetComponent<EgoComponent>();
if( egoComponent == null ) { egoComponent = gameObject.AddComponent<EgoComponent>(); }
egoComponent.CreateMask();
var transform = gameObject.transform;
var childCount = transform.childCount;
for( var i = 0; i < childCount; i++ )
{
InitEgoComponent( transform.GetChild( i ).gameObject );
}
}
public static void Update()
{
// Update all Systems
foreach( var system in _systems )
{
#if UNITY_EDITOR
if ( system.enabled ) system.Update();
#else
system.Update();
#endif
}
// Invoke all queued Events
EgoEvents.Invoke();
// Clean up Destroyed Components & GameObjects
EgoCleanUp.CleanUp();
}
public static void FixedUpdate()
{
// Update all Systems
foreach( var system in _systems )
{
#if UNITY_EDITOR
if( system.enabled ) system.FixedUpdate();
#else
system.FixedUpdate();
#endif
}
}
}