-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGameObjectCreator.cs
197 lines (170 loc) · 6.75 KB
/
GameObjectCreator.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using RSG.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace RSG
{
/// <summary>
/// Interface for procedural creation and management of game objects.
/// All procedural game objects are automatically cleaned up on shutdown.
/// </summary>
public interface IGameObjectCreator
{
/// <summary>
/// Instantiate one game object from another.
/// Game object is parented under the 'procedural game object parent'.
/// </summary>
GameObject Instantiate(string name, GameObject source);
/// <summary>
/// Instantiate one game object from another under a particular parent.
/// </summary>
GameObject Instantiate(string name, GameObject source, GameObject parent);
/// <summary>
/// Create a named object under a particular parent
/// </summary>
GameObject Create(string name, GameObject parent);
/// <summary>
/// Create a named object.
/// Game object is parented under the 'procedural game object parent'.
/// </summary>
GameObject Create(string name);
/// <summary>
/// Create a permanent game object that can survive 'scene reload'.
/// Game object is parented under the 'procedural game object parent'.
/// </summary>
GameObject CreatePermanent(string name);
/// <summary>
/// Load a game object using Unity's resource API
/// </summary>
GameObject Load(string newGameObjectName, string resourcePath);
}
/// <summary>
/// Interface for procedural creation and management of game objects.
/// All procedural game objects are automatically cleaned up on shutdown.
/// </summary>
[Singleton(typeof(IGameObjectCreator))]
public class GameObjectCreator : IGameObjectCreator, IStartable
{
private static readonly string ProceduralGameObjectsRootName = "_Procedural";
private static readonly string PermanentProceduralGameObjectsRootName = "_PermanentProcedural";
/// <summary>
/// Instantiate one game object from another.
/// </summary>
public GameObject Instantiate(string name, GameObject source)
{
Argument.StringNotNullOrEmpty(() => name);
Argument.NotNull(() => source);
return Instantiate(name, source, GetProceduralRootObject());
}
/// <summary>
/// Instantiate one game object from another under a particular parent.
/// </summary>
public GameObject Instantiate(string name, GameObject source, GameObject parent)
{
Argument.StringNotNullOrEmpty(() => name);
Argument.NotNull(() => source);
Argument.NotNull(() => parent);
var gameObject = (GameObject)GameObject.Instantiate(source);
gameObject.transform.SetParent(parent.transform);
gameObject.name = name;
return gameObject;
}
/// <summary>
/// Create a named object under a particular parent
/// </summary>
public GameObject Create(string name, GameObject parent)
{
Argument.StringNotNullOrEmpty(() => name);
Argument.NotNull(() => parent);
var gameObject = new GameObject(name);
gameObject.transform.SetParent(parent.transform);
return gameObject;
}
/// <summary>
/// Create a named object.
/// </summary>
public GameObject Create(string name)
{
Argument.StringNotNullOrEmpty(() => name);
return Create(name, GetProceduralRootObject());
}
/// <summary>
/// Load a game object using Unity's resource API
/// </summary>
public GameObject Load(string newGameObjectName, string resourcePath)
{
Argument.StringNotNullOrEmpty(() => newGameObjectName);
Argument.StringNotNullOrEmpty(() => resourcePath);
var loadedObj = Resources.Load(resourcePath);
if (loadedObj == null)
{
throw new ApplicationException("Failed to load assets from path: " + resourcePath);
}
return Instantiate(newGameObjectName, (GameObject)loadedObj);
}
/// <summary>
/// Helper function to find/create a procedural root object.
/// </summary>
private GameObject GetProceduralRootObject()
{
var parent = GameObject.Find(ProceduralGameObjectsRootName);
if (parent == null)
{
parent = new GameObject(ProceduralGameObjectsRootName);
}
return parent;
}
/// <summary>
/// Create a permanent game object that can survive 'scene reload'.
/// Game object is parented under the 'procedural game object parent'.
/// </summary>
public GameObject CreatePermanent(string name)
{
Argument.StringNotNullOrEmpty(() => name);
return CreatePermanentGameObject(name);
}
/// <summary>
/// Global function for creating a permanent game object, used to create Unity singletons.
/// </summary>
public static GameObject CreatePermanentGameObject(string name)
{
var parent = GetPermanentProceduralRootObject();
var gameObject = new GameObject(name);
gameObject.transform.SetParent(parent.transform);
gameObject.hideFlags = HideFlags.DontSave;
return gameObject;
}
/// <summary>
/// Helper function to find/create a procedural root object.
/// </summary>
public static GameObject GetPermanentProceduralRootObject()
{
var parent = GameObject.Find(PermanentProceduralGameObjectsRootName);
if (parent == null)
{
parent = new GameObject(PermanentProceduralGameObjectsRootName)
{
hideFlags = HideFlags.DontSave
};
GameObject.DontDestroyOnLoad(parent);
}
return parent;
}
public void Startup()
{
}
/// <summary>
/// Shutdown and destroy all permanent procedural game objects.
/// </summary>
public void Shutdown()
{
var parent = GameObject.Find(PermanentProceduralGameObjectsRootName);
if (parent != null)
{
GameObject.DestroyImmediate(parent);
}
}
}
}