forked from Unity-Technologies/PrefabAPIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectTypeLogging.cs
More file actions
148 lines (134 loc) · 6.98 KB
/
Copy pathGameObjectTypeLogging.cs
File metadata and controls
148 lines (134 loc) · 6.98 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
using System.Text;
using UnityEditor;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.SceneManagement;
using UnityEngine;
static public class GameObjectTypeLogging
{
static public void LogStageInformation(GameObject go)
{
// First check if input GameObject is persistent before checking what stage the GameObject is in
if (EditorUtility.IsPersistent(go))
{
if (!PrefabUtility.IsPartOfPrefabAsset(go))
{
Debug.Log("The GameObject is a temporary object created during import. OnValidate() is called two times with a temporary object during import: First time is when saving cloned objects to .prefab file. Second event is when reading .prefab file objects during import");
}
else
{
Debug.Log("GameObject is part of an imported Prefab Asset (from the Library folder)");
}
return;
}
// If the GameObject is not persistent let's determine which stage we are in first because getting Prefab info depends on it
var mainStage = StageUtility.GetMainStageHandle();
var currentStage = StageUtility.GetStageHandle(go);
if (currentStage == mainStage)
{
if (PrefabUtility.IsPartOfPrefabInstance(go))
{
var type = PrefabUtility.GetPrefabAssetType(go);
var path = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(go));
Debug.Log(string.Format("GameObject is part of a Prefab Instance in the MainStage and is of type: {0}. It comes from the prefab asset: {1}", type, path));
}
else
{
Debug.Log("GameObject is a plain GameObject in the MainStage");
}
}
else
{
var prefabStage = PrefabStageUtility.GetPrefabStage(go);
if (prefabStage != null)
{
if (PrefabUtility.IsPartOfPrefabInstance(go))
{
var type = PrefabUtility.GetPrefabAssetType(go);
var nestedPrefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(go));
Debug.Log(string.Format("GameObject is in a PrefabStage. The GameObject is part of a nested Prefab Instance and is of type: {0}. The opened Prefab asset is: {1} and the nested Prefab asset is: {2}", type, prefabStage.prefabAssetPath, nestedPrefabPath));
}
else
{
var prefabAssetRoot = AssetDatabase.LoadAssetAtPath<GameObject>(prefabStage.prefabAssetPath);
var type = PrefabUtility.GetPrefabAssetType(prefabAssetRoot);
Debug.Log(string.Format("GameObject is in a PrefabStage. The opened Prefab is of type: {0}. The GameObject comes from the prefab asset: {1}", type, prefabStage.prefabAssetPath));
}
}
else if (EditorSceneManager.IsPreviewSceneObject(go))
{
Debug.Log("GameObject is not in the MainStage, nor in a PrefabStage. But it is in a PreviewScene so could be used for Preview rendering or other utilities.");
}
else
{
Debug.LogError("Unknown GameObject Info");
}
}
}
static public void LogPrefabInformation(GameObject go)
{
StringBuilder stringBuilder = new StringBuilder();
// First check if input GameObject is persistent before checking what stage the GameObject is in
if (EditorUtility.IsPersistent(go))
{
if (!PrefabUtility.IsPartOfPrefabAsset(go))
{
stringBuilder.Append("The GameObject is a temporary object created during import. OnValidate() is called two times with a temporary object during import: First time is when saving cloned objects to .prefab file. Second event is when reading .prefab file objects during import");
}
else
{
stringBuilder.Append("GameObject is part of an imported Prefab Asset (from the Library folder).\n");
stringBuilder.AppendLine("Prefab Asset: " + GetPrefabInfoString(go));
}
Debug.Log(stringBuilder.ToString());
return;
}
PrefabStage prefabStage = PrefabStageUtility.GetPrefabStage(go);
if (prefabStage != null)
{
GameObject openPrefabThatContentsIsPartOf = AssetDatabase.LoadAssetAtPath<GameObject>(prefabStage.prefabAssetPath);
stringBuilder.AppendFormat(
"The GameObject is part of the Prefab contents of the Prefab Asset:\n{0}\n\n",
GetPrefabInfoString(openPrefabThatContentsIsPartOf));
}
if (!PrefabUtility.IsPartOfPrefabInstance(go))
{
stringBuilder.Append("The GameObject is a plain GameObject (not part of a Prefab instance).\n");
}
else
{
// This is the Prefab Asset that can be applied to via the Overrides dropdown.
GameObject outermostPrefabAssetObject = PrefabUtility.GetCorrespondingObjectFromSource(go);
// This is the Prefab Asset that determines the icon that is shown in the Hierarchy for the nearest root.
GameObject nearestRootPrefabAssetObject = AssetDatabase.LoadAssetAtPath<GameObject>(PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go));
// This is the Prefab Asset where the original version of the object comes from.
GameObject originalPrefabAssetObject = PrefabUtility.GetCorrespondingObjectFromOriginalSource(go);
stringBuilder.AppendFormat(
@"Prefab Asset of the outermost Prefab instance the input GameObject is part of is:
{0}
Prefab Asset of the nearest Prefab instance root the input GameObject is part of is:
{1}
Prefab Asset of the innermost Prefab instance the input GameObject is part of is:
{2}
Complete nesting chain from outermost to original:
",
GetPrefabInfoString(outermostPrefabAssetObject),
GetPrefabInfoString(nearestRootPrefabAssetObject),
GetPrefabInfoString(originalPrefabAssetObject));
GameObject current = outermostPrefabAssetObject;
while (current != null)
{
stringBuilder.AppendLine(GetPrefabInfoString(current));
current = PrefabUtility.GetCorrespondingObjectFromSource(current);
}
}
stringBuilder.AppendLine("");
Debug.Log(stringBuilder.ToString());
}
static string GetPrefabInfoString(GameObject prefabAssetGameObject)
{
string name = prefabAssetGameObject.transform.root.gameObject.name;
string assetPath = AssetDatabase.GetAssetPath(prefabAssetGameObject);
PrefabAssetType type = PrefabUtility.GetPrefabAssetType(prefabAssetGameObject);
return string.Format("<b>{0}</b> (type: {1}) at '{2}'", name, type, assetPath);
}
}