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?
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?
Start()
No - I want something that executes in the Editor. Not in “Play” mode.
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.
I still need an event to react to though. Hopefully someone will have the answer.
What do you mean by configuration?
Try reading through this, you might find what you’re looking for Unity - Manual: Extending the Editor with IMGUI
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.
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.
That one is interesting - hadn’t seen that before. Will see if it works.
I needed this myself as well. So I wrote this to deal with it:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyUnityFrameworkEditor/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/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyUnityFrameworkEditor/Tools/PrefabTools.cs
This can serve as a ‘how to’ use the script in question.
Very awesome! Thanks for sharing. I’ll grab some pieces of that and it should get my issue sorted out. Thanks!
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
If it’s just a small piece of code, put it in Start() and do this:
#if UNITY_EDITOR
if(!Application.isPlaying)
{
//Do some stuff
}
#endif
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.
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.
public interface IAwakeEditorListener
{
void OnEditorAwake();
}
[ExecuteInEditMode]
public class OnAwakeEditorListener : MonoBehaviour
{
void Awake()
{
Debug.Log("Editor causes this Awake");
foreach (IAwakeEditorListener c in GetComponents<IAwakeEditorListener>())
{
c.OnEditorAwake();
}
}
}
public class MyOtherComponent : MonoBehaviour, IAwakeEditorListener
{
public void OnEditorAwake()
{
// My piece of code
}
}