Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xenix2012 · Sep 14, 2016 at 12:27 PM · c#gameobjecteditoreventhierarchy

Detecting moment when GameObiect is created in hierarchy.

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xenix2012 · Sep 14, 2016 at 03:54 PM 0
Share

Up, anyone?

avatar image iabulko xenix2012 · Sep 14, 2016 at 05:01 PM 1
Share

A don't think that exists, but you can always count all the gameobjects in scene when EditorApplication.hierarchyWindowChanged and check if that number is higher than it was before ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Jessespike · Sep 16, 2016 at 06:26 PM

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.\nGameObject.name = " + go.name);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yerendi · Sep 17, 2016 at 05:29 AM 0
Share

Thanks for answer! (I'm xenix2012 but from another account because I can't log out from this one on my phone :/). I've did this same (and I've added onObjectDestroyed event too) BUT performance of FindObjectOfTypeAll is... horrible. If anyone has any diffrend idea I would be glad to hear it :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there anyway to batch renaming via Editor [not on Runtime] multiple game objects in the hierarchy ? 4 Answers

Hierarchy Foldout Variables Wanted 3 Answers

keep gameObject in hierarchy folded when not selected 3 Answers

Initialising List array for use in a custom Editor 1 Answer

Best practice for copying Components from one GameObject to another? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges