Search Unity

GameObject OnCreate?

Discussion in 'Scripting' started by SteveJ, Nov 3, 2014.

  1. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    It's either not possible, or I just can't find it...

    When I drag a prefab from my Project panel into my scene Hierarchy, is there an "OnCreate" method that I can execute on that object to run some initial configuration on it?
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Start()
     
    MendyBerger likes this.
  3. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    No - I want something that executes in the Editor. Not in "Play" mode.
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    I just updated Unity to the most recent. In this version, the hierarchy no longer lists items alphabetically. This might be fine, but my project was organized alphabetically. This new feature rearranged my hierarchy. Some googling led me to this.

    It allows for your hierarchy to be set alphabetically, or not. Obviously an editor feature. I do not fully grasp what is happening in this script and how it works, but, one can effect the editor via a script, placed in the editor folder.


    Sort hierarchy by alphabetical order ?
     
  5. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    I still need an event to react to though. Hopefully someone will have the answer.
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    What do you mean by configuration?
     
  7. joni-giuro

    joni-giuro

    Joined:
    Nov 21, 2013
    Posts:
    435
  8. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Brilliant! I hadn't thought to read the manual (he said sarcastically) :)

    Anyway, I've rigged up a dodgy way of doing it with OnHierarchyChange(), but I'd love to know if there's a "good" way to do it.
     
  9. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Actually, OnHierarchyChange() is pretty useless for this as it relies on that particular Editor Window being active at the time the object is dragged into the Hierarchy :(

    Back to the drawing board.
     
  10. joni-giuro

    joni-giuro

    Joined:
    Nov 21, 2013
    Posts:
    435
  11. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
  12. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
  13. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    I needed this myself as well. So I wrote this to deal with it:
    https://code.google.com/p/spacepupp...uppyUnityFrameworkEditor/EditorSceneEvents.cs

    It's an editor script and only operates at edit time.

    I specifically wanted it so that when you added a prefab to the scene, if you held the shift key, it automatically unlinked it from the prefab itself.

    You can see that in action here:
    https://code.google.com/p/spacepupp...uppyUnityFrameworkEditor/Tools/PrefabTools.cs

    This can serve as a 'how to' use the script in question.
     
  14. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Very awesome! Thanks for sharing. I'll grab some pieces of that and it should get my issue sorted out. Thanks!
     
  15. ComradeVanti

    ComradeVanti

    Joined:
    May 22, 2015
    Posts:
    25
    Sorry for necroing.
    If anybody is still looking into this:

    If your script has the [ExecuteInEditMode] attribute, OnEnable will be called when its added to the scene. So just put that script on your gameobject and let it do the initialization.

    Be weary that the method will also be called though when you put your gameobject into the assets window to make a prefab.

    Hope this helps :D
     
  16. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    If it's just a small piece of code, put it in Start() and do this:

    Code (csharp):
    1.  
    2. #if UNITY_EDITOR
    3. if(!Application.isPlaying)
    4. {
    5.     //Do some stuff
    6. }
    7. #endif
    8.  
    The reason I like the above approach is that code block only even compiles if you're in the editor. If you build out to your target platform (like Standalone, UWP, iOS or whatever), that bit of code won't even be compiled so your Application.isPlaying check won't even be there in your production deployment.

    Edit: Oops, didn't realize I was replying to a necro'd thread. :)
     
  17. CristianLH_U

    CristianLH_U

    Joined:
    Jul 16, 2019
    Posts:
    7
    I had recently the same problem here. I just wanted to call a piece of code on a component just after the creation of an object dragging a prefab to the scene (that was in order to assign a hash data tag field automatically, that must be different for each instance).

    I've achieved an elegant solution (or I think so). Basically, I created a script OnAwakeEditorListener that defines an interface IAwakeEditorListener, and a MonoBehaviour that executes in editor. This monobehaviour, in its Awake method call to the method OnEditorAwake of all components defined on the same gameobject that implements the IAwakeEditorListener. So I just added the OnAwakeEditorListener component to my prefab and implement the method OnEditorAwake in my first component.

    Code (CSharp):
    1.  
    2. public interface IAwakeEditorListener
    3. {
    4.      void OnEditorAwake();
    5. }
    6.  
    7.     [ExecuteInEditMode]
    8.     public class OnAwakeEditorListener : MonoBehaviour
    9.     {
    10.         void Awake()
    11.         {
    12.             Debug.Log("Editor causes this Awake");
    13.             foreach (IAwakeEditorListener c in GetComponents<IAwakeEditorListener>())
    14.             {
    15.                 c.OnEditorAwake();
    16.             }
    17.         }
    18.     }
    19.  
    20. public class MyOtherComponent : MonoBehaviour, IAwakeEditorListener
    21. {
    22.         public void OnEditorAwake()
    23.         {
    24.                // My piece of code
    25.         }
    26. }
    27.  
     
    andreyefimov2010 likes this.