-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectManager.cs
More file actions
149 lines (123 loc) · 4.66 KB
/
ObjectManager.cs
File metadata and controls
149 lines (123 loc) · 4.66 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using DonutDiner.Framework.Data;
using System.Collections.Generic;
using UnityEngine;
namespace DonutDiner.Framework
{
// Class for indexing of important game objects and efficient access.
// One per scene, is not persistent.
// First implemented by Ben Russell
public class ObjectManager : MonoBehaviour
{
#region Fields
public List<SerializableObject> SerializedObjects = null;
public CharacterController PlayerController;
#endregion
#region Unity Methods
#if UNITY_EDITOR
private void Reset()
{
// On creation in editor
SerializedObjects = new List<SerializableObject>();
// Add objects present in the scene
SerializableObject[] serializableObjects = FindObjectsOfType<SerializableObject>();
foreach (var serializableObject in serializableObjects)
{
if (!string.IsNullOrEmpty(serializableObject.transform.root.gameObject.scene.name))
{
serializableObject.AddToObjectManager();
}
}
}
#endif
private void Start()
{
GameManager.Instance().ObjectManager = this;
PlayerController = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterController>();
if (Application.isEditor)
{
// Sanity check to make sure there are no duplicate GUID's
for (int i = 0; i < SerializedObjects.Count; ++i)
{
for (int j = i; j < SerializedObjects.Count; ++j)
{
if (i != j
&& SerializedObjects[i].GetGuid() == SerializedObjects[j].GetGuid())
{
Debug.LogWarning(SerializedObjects[i].gameObject.name + " and " + SerializedObjects[j].gameObject.name + " share a GUID! Please alert Programming (Ben)");
}
}
}
}
}
#endregion
#region Public Methods
public static ObjectManager Instance()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
ObjectManager objectManager = FindObjectOfType<ObjectManager>();
if (objectManager)
{
return objectManager;
}
}
#endif
return GameManager.Instance().ObjectManager;
}
public bool AddSerializableObject(SerializableObject serializableObject)
{
bool guidAlreadyPresent = false;
// Traverse list backwards to allow for easy cleaning of null references
for (int i = SerializedObjects.Count - 1; i >= 0; i--)
{
if (SerializedObjects[i] == null)
{
// Clean up null reference
SerializedObjects.RemoveAt(i);
}
else
{
if (SerializedObjects[i] == serializableObject)
{
// Object already in list, nothing more to do
return false;
}
if (SerializedObjects[i].GetGuid() == serializableObject.GetGuid())
{
guidAlreadyPresent = true;
}
}
}
// Add new object
SerializedObjects.Add(serializableObject);
// Return whether object needs a new GUID. (Because it's a duplicated object)
return guidAlreadyPresent;
}
public SerializableObject GetObjectByGuid(string guid)
{
foreach (SerializableObject serializableObject in SerializedObjects)
{
if (serializableObject.GetGuid() == guid)
{
return serializableObject;
}
}
// Not found!
return null;
}
public void DestroyUnserializedObjects()
{
// Destroy Serialized Objects not found in save file (They weren't around to be saved)
for (int i = SerializedObjects.Count - 1; i >= 0; i--)
{
if (!SerializedObjects[i].GetLoadedFromFile())
{
Destroy(SerializedObjects[i].gameObject);
SerializedObjects.RemoveAt(i);
}
}
}
#endregion
}
}