Hi,
I need to know moment when any new GameObject is added to hierarchy (from project window, CtrlD, code etc). I know about EditorApplication.hierarchyWindowChanged but it gives me no informatian what type of change it was. In general i want sth like “SuperHelpfulEditorClass.onGameObiectCreated”. Any ideas?
As iabulko suggested, use hierarchWindowChange and keep track of the number of objects.
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class SuperHelpfulEditorClass {
public delegate void HierarchyChangeAction(GameObject go);
public static event HierarchyChangeAction onGameObjectCreated;
static int objectCount = 0;
static SuperHelpfulEditorClass()
{
EditorApplication.hierarchyWindowChanged += OnHierarchyWindowChanged;
}
static void OnHierarchyWindowChanged () {
GameObject[] objects = (GameObject[]) Resources.FindObjectsOfTypeAll (typeof (GameObject));
if (objects.Length > objectCount)
{
GameObject go = Selection.activeGameObject;
if (onGameObjectCreated != null)
onGameObjectCreated(go);
}
objectCount = objects.Length;
}
}
New GameObjects are selected by default, not sure how reliable this is, so it might have to change. The event can be used like this:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class ShowCreatedGameObjectName {
static ShowCreatedGameObjectName()
{
SuperHelpfulEditorClass.onGameObjectCreated += OnGameObjectCreated;
}
static void OnGameObjectCreated(GameObject go)
{
Debug.Log("GameObject was created.
GameObject.name = " + go.name);
}
}