Skip to content

Commit b3fbb98

Browse files
committed
Add logging function for GameObject/Prefab type information.
Also add a test prefab with a test script that logs in OnValidate
1 parent f219208 commit b3fbb98

8 files changed

Lines changed: 171 additions & 46 deletions
Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using UnityEngine;
22
using UnityEditor;
33
using UnityEditor.SceneManagement;
4+
using UnityEditor.Experimental.SceneManagement;
45

56
public class GameObjectType
67
{
78
[MenuItem("Prefabs/Query/Get GameObject Info")]
8-
static public void PrintGameObjectInfo()
9+
static void LogSelectedGameObjectInfo()
910
{
1011
var go = Selection.activeGameObject;
1112
if (go == null)
@@ -14,49 +15,6 @@ static public void PrintGameObjectInfo()
1415
return;
1516
}
1617

17-
var mainStage = StageUtility.GetMainStageHandle();
18-
19-
// Lets determine which stage we are in first because getting Prefab info depends on it
20-
var currentStage = StageUtility.GetStageHandle(go);
21-
if (currentStage == mainStage)
22-
{
23-
if (PrefabUtility.IsPartOfPrefabInstance(go))
24-
{
25-
var type = PrefabUtility.GetPrefabAssetType(go);
26-
var path = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(go));
27-
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));
28-
}
29-
else
30-
{
31-
Debug.Log("Selected GameObject is a plain GameObject in the MainStage");
32-
}
33-
}
34-
else
35-
{
36-
var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(go);
37-
if (prefabStage != null)
38-
{
39-
if (PrefabUtility.IsPartOfPrefabInstance(go))
40-
{
41-
var type = PrefabUtility.GetPrefabAssetType(go);
42-
var nestedPrefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(go));
43-
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));
44-
}
45-
else
46-
{
47-
var prefabAssetRoot = AssetDatabase.LoadAssetAtPath<GameObject>(prefabStage.prefabAssetPath);
48-
var type = PrefabUtility.GetPrefabAssetType(prefabAssetRoot);
49-
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));
50-
}
51-
}
52-
else if (EditorSceneManager.IsPreviewSceneObject(go))
53-
{
54-
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.");
55-
}
56-
else
57-
{
58-
Debug.LogError("Unknown GameObject Info");
59-
}
60-
}
18+
GameObjectTypeLogging.LogGameObjectInformation(go);
6119
}
6220
}

Assets/Prefabs/Cube.prefab

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ MeshRenderer:
5252
m_Enabled: 1
5353
m_CastShadows: 1
5454
m_ReceiveShadows: 1
55-
m_DynamicOccludee: 1
55+
m_DynamicOccludee: 0
5656
m_MotionVectors: 1
5757
m_LightProbeUsage: 1
5858
m_ReflectionProbeUsage: 1
@@ -101,6 +101,8 @@ MonoBehaviour:
101101
m_GameObject: {fileID: 447069473986574363}
102102
m_Enabled: 1
103103
m_EditorHideFlags: 0
104+
m_GeneratorAsset: {fileID: 0}
104105
m_Script: {fileID: 11500000, guid: a24dc3c81b3984a2190f4db74724b5b3, type: 3}
105106
m_Name:
106107
m_EditorClassIdentifier:
108+
rotationSpeed: 20
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &8395073242791717799
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
serializedVersion: 6
10+
m_Component:
11+
- component: {fileID: 6208340675808237100}
12+
- component: {fileID: 7782794724655838501}
13+
m_Layer: 0
14+
m_Name: PrefabWithEventLogging
15+
m_TagString: Untagged
16+
m_Icon: {fileID: 0}
17+
m_NavMeshLayer: 0
18+
m_StaticEditorFlags: 0
19+
m_IsActive: 1
20+
--- !u!4 &6208340675808237100
21+
Transform:
22+
m_ObjectHideFlags: 0
23+
m_CorrespondingSourceObject: {fileID: 0}
24+
m_PrefabInstance: {fileID: 0}
25+
m_PrefabAsset: {fileID: 0}
26+
m_GameObject: {fileID: 8395073242791717799}
27+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
28+
m_LocalPosition: {x: 0, y: 0, z: 0}
29+
m_LocalScale: {x: 1.62, y: 1, z: 1}
30+
m_Children: []
31+
m_Father: {fileID: 0}
32+
m_RootOrder: 0
33+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
34+
--- !u!114 &7782794724655838501
35+
MonoBehaviour:
36+
m_ObjectHideFlags: 0
37+
m_CorrespondingSourceObject: {fileID: 0}
38+
m_PrefabInstance: {fileID: 0}
39+
m_PrefabAsset: {fileID: 0}
40+
m_GameObject: {fileID: 8395073242791717799}
41+
m_Enabled: 1
42+
m_EditorHideFlags: 0
43+
m_GeneratorAsset: {fileID: 0}
44+
m_Script: {fileID: 11500000, guid: 909c348b433a8ac48932e12bb09fe624, type: 3}
45+
m_Name:
46+
m_EditorClassIdentifier:

Assets/Prefabs/PrefabWithEventLogging.prefab.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
using UnityEditor.Experimental.SceneManagement;
5+
using UnityEditor.SceneManagement;
6+
using UnityEngine;
7+
8+
static public class GameObjectTypeLogging
9+
{
10+
static public void LogGameObjectInformation(GameObject go)
11+
{
12+
// First check if input GameObject is persistent before checking what stage the GameObject is in
13+
if (EditorUtility.IsPersistent(go))
14+
{
15+
if (!PrefabUtility.IsPartOfPrefabAsset(go))
16+
{
17+
// If LogGameObjectTypeInformation() is called from OnValidate() the GameObject can be part of the events from the import pipeline for the persistent objects.
18+
Debug.Log("Two events are fired under OnValidate(): First time is when saving objects to .prefab file. Second event is when reading .prefab file during import");
19+
}
20+
else
21+
{
22+
Debug.Log("GameObject is part of an imported Prefab Asset (from the Library folder)");
23+
}
24+
return;
25+
}
26+
27+
// If the GameObject is not persistent let's determine which stage we are in first because getting Prefab info depends on it
28+
var mainStage = StageUtility.GetMainStageHandle();
29+
var currentStage = StageUtility.GetStageHandle(go);
30+
if (currentStage == mainStage)
31+
{
32+
if (PrefabUtility.IsPartOfPrefabInstance(go))
33+
{
34+
var type = PrefabUtility.GetPrefabAssetType(go);
35+
var path = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(go));
36+
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));
37+
}
38+
else
39+
{
40+
Debug.Log("GameObject is a plain GameObject in the MainStage");
41+
}
42+
}
43+
else
44+
{
45+
var prefabStage = PrefabStageUtility.GetPrefabStage(go);
46+
if (prefabStage != null)
47+
{
48+
if (PrefabUtility.IsPartOfPrefabInstance(go))
49+
{
50+
var type = PrefabUtility.GetPrefabAssetType(go);
51+
var nestedPrefabPath = AssetDatabase.GetAssetPath(PrefabUtility.GetCorrespondingObjectFromSource(go));
52+
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));
53+
}
54+
else
55+
{
56+
var prefabAssetRoot = AssetDatabase.LoadAssetAtPath<GameObject>(prefabStage.prefabAssetPath);
57+
var type = PrefabUtility.GetPrefabAssetType(prefabAssetRoot);
58+
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));
59+
}
60+
}
61+
else if (EditorSceneManager.IsPreviewSceneObject(go))
62+
{
63+
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.");
64+
}
65+
else
66+
{
67+
Debug.LogError("Unknown GameObject Info");
68+
}
69+
}
70+
}
71+
}

Assets/Scripts/GameObjectTypeLogging.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class LogGameObjecTypeOnEvents : MonoBehaviour
6+
{
7+
private void Awake()
8+
{
9+
Debug.Log("Awake");
10+
GameObjectTypeLogging.LogGameObjectInformation(gameObject);
11+
}
12+
13+
private void OnValidate()
14+
{
15+
Debug.Log("OnValidate");
16+
GameObjectTypeLogging.LogGameObjectInformation(gameObject);
17+
18+
}
19+
}

Assets/Scripts/LogGameObjecTypeOnEvents.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)