-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedPrefab.cs
More file actions
276 lines (203 loc) · 7.64 KB
/
Copy pathNestedPrefab.cs
File metadata and controls
276 lines (203 loc) · 7.64 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public interface IPrefabGeneration
{
GameObject GenerateFrom(string prefabPath);
}
public class NestedPrefab : MonoBehaviour {
[HideInInspector]
[SerializeField]
public List<NestedPrefabData> nestedPrefabsData;
[HideInInspector]
[SerializeField]
public List<GameObjectData> emptyObjectsData;
private const string pathSeparator = "//Nested//";
[HideInInspector]
[SerializeField]
public bool prefabDetailsVisibility = false;
private Dictionary<int, Transform> hierarchyDict;
public IPrefabGeneration prefabGenerator = null;
void Awake()
{
if (prefabGenerator == null) {
prefabGenerator = new DefaultPrefabGeneration();
}
}
// Use this for initialization
public void SavePrefabData () {
nestedPrefabsData = new List<NestedPrefabData>();
emptyObjectsData = new List<GameObjectData>();
currentId = 0;
SavePrefabData(transform, "", currentId);
}
[ContextMenu("Toggle Prefabs Details")]
public void TogglePrefabsDetails()
{
prefabDetailsVisibility = !prefabDetailsVisibility;
}
private static int currentId = 0; // Id 0 corresponds to Prefab Root
public void SavePrefabData (Transform trans, string relativePath, int pathId) {
Transform child;
GameObject prefabParent;
#if UNITY_EDITOR
for (int i = 0; i < trans.childCount; i++)
{
child = trans.GetChild(i);
prefabParent = PrefabUtility.GetPrefabParent(child.gameObject) as GameObject;
if (prefabParent == null)
{
GameObjectData goData = new GameObjectData();
currentId++;
goData.id = currentId;
goData.hierarchyPath = relativePath;
goData.hierarchyPathId = pathId;
goData.CopyFrom(child);
emptyObjectsData.Add(goData);
SavePrefabData(child, relativePath + pathSeparator + child.name, goData.id);
}
else {
NestedPrefabData data = new NestedPrefabData();
data.prefabPath = AssetDatabase.GetAssetPath(prefabParent);
// Debug.Log("Prefab path = " + data.prefabPath);
data.hierarchyPath = relativePath;
// Debug.Log("prefabHierarchyPath " + data.hierarchyPath);
data.hierarchyPathId = pathId;
data.CopyFrom(child);
nestedPrefabsData.Add(data);
}
}
#endif
}
/// <summary>
/// Main method to generate prefabs using all saved data
/// Attention: All children objects are going to be removed
/// </summary>
public void GeneratePrefabs(bool askPermission = true)
{
#if UNITY_EDITOR
if (askPermission && !EditorUtility.DisplayDialog("Do want to update children objects?",
"Are you sure you want to update children objects? All current changes would be lost", "Yes", "I am not sure"))
{
return;
}
// Store the states of 'root' and its children.
Undo.RegisterFullObjectHierarchyUndo(gameObject, "NestedPrefab update");
#endif
// Destroy all children objects in order to recreate them according to internal data
DestroyChildren();
// Create empty objects
UpdateTransformFromIds();
// Generate all prefabs
for (int i = 0; i < nestedPrefabsData.Count; i++)
{
GeneratePrefab(nestedPrefabsData[i]);
}
}
void GeneratePrefab(NestedPrefabData prefabData)
{
// Debug.Log("NestedPrefab::GeneratePrefab - prefabData " + prefabData.prefabPath + " ");
Transform parent = GetHierarchyTransform(prefabData.hierarchyPathId);
GameObject clone = prefabGenerator.GenerateFrom(prefabData.prefabPath);
if (clone == null)
{
Debug.LogError("Error when trying to generate prefab " + prefabData.prefabPath + " !", this);
}
clone.transform.parent = parent;
prefabData.CopyDataTo(clone.transform);
// Recursive generation
NestedPrefab nestedPrefab = clone.GetComponent<NestedPrefab>();
if (nestedPrefab != null)
{
nestedPrefab.prefabGenerator = prefabGenerator;
nestedPrefab.GeneratePrefabs(false); // Avoid to ask permission twice
}
}
void UpdateTransformFromIds()
{
hierarchyDict = new Dictionary<int, Transform>();
hierarchyDict.Add(0, transform);
UpdateTransformFromIds(0, transform);
}
void UpdateTransformFromIds(int id, Transform parent)
{
Transform empty;
for (int i = 0; i < emptyObjectsData.Count; i++)
{
if (emptyObjectsData[i].hierarchyPathId == id) {
empty = CreateEmpty(emptyObjectsData[i], parent);
hierarchyDict.Add(emptyObjectsData[i].id, empty);
UpdateTransformFromIds(emptyObjectsData[i].id, empty);
}
}
}
Transform CreateEmpty(GameObjectData data, Transform parent)
{
GameObject newGo = new GameObject(data.name);
newGo.transform.parent = parent;
data.CopyDataTo(newGo.transform);
return newGo.transform;
}
Transform GetHierarchyTransform(int hierarchyPathId) {
return hierarchyDict[hierarchyPathId];
}
void DestroyChildren()
{
for (int i = transform.childCount-1; i >= 0; i--)
{
DestroyImmediate(transform.GetChild(i).gameObject);
}
}
}
public class DefaultPrefabGeneration : IPrefabGeneration
{
public GameObject GenerateFrom(string prefabPath) {
GameObject prefab = null, clone = null;
#if UNITY_EDITOR
if (!Application.isPlaying) {
prefab =
(AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Transform)) as Transform).gameObject;
clone = PrefabUtility.InstantiatePrefab(prefab) as GameObject;
}
#else
prefab = Resources.Load(prefabPath) as GameObject;
clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
#endif
return clone;
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(NestedPrefab))]
public class NestedPrefabEditor : Editor {
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
NestedPrefab nestedPrefab = target as NestedPrefab;
if (nestedPrefab == null) return;
nestedPrefab.prefabGenerator = new DefaultPrefabGeneration();
if (GUILayout.Button("Save Nested Prefabs")) {
nestedPrefab.SavePrefabData();
}
if (GUILayout.Button("Revert Prefabs")) {
nestedPrefab.GeneratePrefabs();
}
if (nestedPrefab.prefabDetailsVisibility)
{
EditorGUILayout.Space();
EditorGUILayout.LabelField("List of Nested Prefabs:", EditorStyles.boldLabel);
EditorGUILayout.Space();
for (int i = 0; i < nestedPrefab.nestedPrefabsData.Count; i++)
{
NestedPrefabData prefabData = nestedPrefab.nestedPrefabsData[i];
// GameObject prefabParent = PrefabUtility.GetPrefabParent(prefabData.prefabObject) as GameObject;
// string prefabPath = AssetDatabase.GetAssetPath(prefabParent);
EditorGUILayout.LabelField("" + i + ". " + prefabData.hierarchyPath + " from " + prefabData.prefabPath);
// PrefabUtility.GetPropertyModifications
}
}
}
}
#endif