Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by JustSid · Aug 21, 2012 at 06:23 PM · editoreditor-scripting

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?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 21, 2012 at 08:13 PM 2
Share

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.

avatar image JustSid · Aug 21, 2012 at 08:42 PM 1
Share

@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.

avatar image
2

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.

Comment
Add comment · Show 4 · 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 JustSid · Aug 22, 2012 at 09:03 AM 0
Share

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.

avatar image Bunny83 · Aug 22, 2012 at 10:07 AM 1
Share

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 ;)

avatar image JustSid · Aug 22, 2012 at 10:20 AM 0
Share

Great hint, thanks, didn't knew that I could just derive from ScriptableObject to get this behaviour!

avatar image Bunny83 · Aug 22, 2012 at 10:27 AM 0
Share

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

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

8 People are following this question.

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

Related Questions

HideFlags on children of visible objects. 3 Answers

Instance of [Editor] couldn't be created because there is no script with that name. 1 Answer

Undo.RecordObject isn't working. 7 Answers

Can you run custom code when changing scene perspective? 1 Answer

Event for loading component in the editor 2 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