How to get a notification o A component or child of a GameObject has been added to GameObject

In any case, can I be notified (possibly with some method / event raised) when Componentadded to GameObject(and even a child GameObject)?

I would like to receive notifications (possibly in some editor scripts) when, for example, some events occur in the editor:

  • A Componentwas attached to the instanceGameObject
  • A is Component attached toPrefab
  • A GameObjectbecame a child of another GameObjectistance

Is it possible? If so, how?


EDIT

I found out the delegate about parenting: EditorApplication.hierarchyWindowChanged

According to the document, it is called:

.

transform.parent , gameObject.name, ..

, , .

+5
2

( ) :

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

public class LevelEditorHelper : MonoBehaviour
{
    LevelEditorHelper()
    {
        EditorApplication.hierarchyWindowChanged -= MyHierarchyChangedCallback;
        EditorApplication.hierarchyWindowChanged += MyHierarchyChangedCallback;
    }

    private static void MyHierarchyChangedCallback()
    {
        Debug.Log("Hierarchy has changed");
    }
}
#endif

( ):

GameObject activeObj = Selection.activeGameObject;
if(activeObj != null) {
    ...

, , , , , (, ), , , .

.. , , #if UNITY_EDITOR , (, Start , , ), , , . - , , .

+1

"" . .

  • Awake() , parent gameObject (SendMessage ) [note: , NEW)

  • , . Update() , SendMessage .

    2 , - . , ( ), , , .

-1

All Articles