- Home /
Editor plugin that runs all the time?
I would like to create an editor plugin that runs all the time while Unity runs, which also gets events like when the hierarchy or project changes (much like a custom `EditorWindow` subclass but without the UI). I don't want to present anything to the user, I just want to have it in the background listening for changes and doing some magic if needed. Is this possible and if yes how would I achieve this?
Answer by JustSid · Aug 21, 2012 at 08:03 PM
To answer my own question, the solution is a normal class and adding the `[InitializeOnLoad]` attribute to it. To get the callbacks like `OnHierarchyChange` fired, also add the appropriate callbacks to `EditorApplication`. Example:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad]
public class Foobar
{
static Foobar()
{
EditorApplication.hierarchyWindowChanged += OnHierarchyChange;
}
static void OnHierarchyChange()
{
// Blahblah
}
}
Whine: The `InitializeOnLoad` attribute is not listed in the Attribute list of the Unity reference manual... At least not directly.
Nice! haven't seen this attribute either, but i've just found this:
http://docs.unity3d.com/Documentation/$$anonymous$$anual/RunningEditorCodeOnLaunch.html
So this should definately work. I would have suggested a static constructor as well, but it's not called until you access the class. InitializeOnLoad should solve this problem.
@Bunny83 Yes, InitializeOnLoad does solve the problem. And yeah, the document isn't really easy to find (unless one googles for InitializeOnLoad in which case it shows up easily). Sadly it seems like most Editor related stuff isn't very well documented.
Answer by Bunny83 · Aug 21, 2012 at 07:37 PM
Well, in general you have to keep in mind that when ever Unity compiles a script in your project, everything is reloaded and recreated. That means everything that is in managed code will be reset whenever something compiles. There are almost no editor specific events where you can hook into. The best place would be a AssetModificationProcessor or AssetPostprocessor. In there you can register a delegate on EditorApplication.update or any other delegate you can find on EditorApplication.
Just a follow up on this one because I ran into the exact same problem with the recompilation (and I had to save some state prior to this). The solution turned out to be: Setting a delegate for `EditorApplication.Update` and inside of this checking for `EditorApplication.isCompiling` which is true when the editor starts to compiling the script. I'm not exactly sure if the editor will gracefully kill your objects when its done with compiling, so it might be a wise idea to save the state as fast as possible.
Well, if you have a "true" Unity object like a ScriptableObject which you would create from your static function, you will receive OnEnabled and OnDisabled.
Don't forget to set the hideflags to HideAndDontSave since you don't want to store it in the scene ;)
Great hint, thanks, didn't knew that I could just derive from ScriptableObject to get this behaviour!
Well, you could even create a GameObject with a $$anonymous$$onoBehaviour attached ;) just keep in $$anonymous$$d to set the hideflags, so it won't go into the scene. AFAI$$anonymous$$ if you use EditorUtility.CreateGameObjectWithHideFlags it won't mark your scene as "dirty". $$anonymous$$eep in $$anonymous$$d that every single untiy object has it's own hideflags (Gameobject, components, material, mesh, ...).
Your answer