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
0
Question by mandeepr · Jul 19, 2010 at 10:17 PM · editoreditor-scripting

EditorApplication.update resetting

I've set the EditorApplication callback to one of my own, and I've simply put a debug output in it, but as soon as an asset import happens, it only continues to function for a couple seconds afterwards. I subclassed the AssetPostprocessor.GetPostprocessOrder to hook it in from the start:

public class BackgroundUpdater : AssetPostprocessor { private static EditorApplication.CallbackFunction s_backgroundUpdateCB = new EditorApplication.CallbackFunction(BackgroundUpdateFunc);

 public override int GetPostprocessOrder()
 {
     EditorApplication.update = s_backgroundUpdateCB;

     return base.GetPostprocessOrder();
 }

 void BackgroundUpdateFunc()
 {
     Debug.Log("running background update func: " + EditorApplication.timeSinceStartup.ToString());
 }

}

The problem I have with this is that everytime a script (any script, not even related to this one) is reimported/recompiled, either automatically or by the context menu from within Unity, My function stops outputting to the console, therefore it is ceasing to function as an update function...

Any ideas?

Comment
Add comment · Show 1
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 mandeepr · Jul 23, 2010 at 04:36 PM 0
Share

does anyone else in the unity world have any ideas? I'd offer a bounty but I only have 2 reputation points

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Oncle-Ben · Feb 11, 2014 at 05:15 PM

You may set up a static class to be constructed each time the editor starts using the "InitializeOnLoad" attribute:

 [InitializeOnLoad]
 public static class MyStaticInitializer 
 {
   static MyStaticInitializer()
   {
     // hook up your EditorApplication callbacks here
   }
 }

Word of warning, this will also be called each time a script is recompiled.

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

Answer by jonas-echterhoff · Jul 20, 2010 at 08:33 AM

When a script is imported, all the assemblies are recompiled, which makes all scripts reload, and resets this setup. You could try adding a static constructor to your script to set up your update function, as that will be called on assembly reload.

static BackgroundUpdater()
{
     EditorApplication.update = s_backgroundUpdateCB;
}
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 mandeepr · Jul 20, 2010 at 06:28 PM 1
Share

in unity, static constructors only get called when something from that class is referenced. This can get fixed by adding an empty GetPostProcessOrder to that class, but then we're back at square one. Also, using the static constructor to set the update function still has the same problem where after a couple seconds, the update function just ceases to run.

Thanks for the good effort though!

avatar image
1

Answer by MattMaker · Sep 12, 2011 at 02:56 AM

a better place for setting up and tearing down delegates is in a matching pair of OnEnabled() / OnDisabled functions.

instead of hooking anything relating to asset importing, I suggest putting this logic in a class that extends EditorWindow. Set your delegate in its OnEnable(), and unset it in OnDisable(). As long as your window exists, it will maintain the EditorApplication.update for you. When a recompile occurs, state is lost for just a moment, but it won't matter, because OnDisable and OnEnable will immediately be called, and things will continue as before.

Also, for safety's sake, use += to assign a delegate rather than =.

 using UnityEngine;

using UnityEditor; using System.Collections;

//[InitializeOnLoad] public class EditorUpdateExample : EditorWindow {

 private EditorApplication.CallbackFunction s_backgroundUpdateCB;

 int executionCount = 0;
 public void BackgroundUpdateFunc()
 {
     executionCount++;
     Debug.Log("running background update func: " + EditorApplication.timeSinceStartup.ToString() + " " + (EditorApplication.isCompiling ? "compiling":"compiled") + " " + executionCount + " delegate: " + EditorApplication.update);
 }
 
 [ MenuItem( "Window/Background Updater" ) ]
 public static void Launch()
 {
     EditorWindow window = GetWindow( typeof( EditorUpdateExample ) );
     window.Show();
 } 
 
 void OnEnable() {
     Debug.Log(this + " OnEnable");
     s_backgroundUpdateCB = new EditorApplication.CallbackFunction(BackgroundUpdateFunc);
     EditorApplication.update += s_backgroundUpdateCB;
 }
 
 void OnDisable() {
     Debug.Log(this + " OnDisable");
     EditorApplication.update -= s_backgroundUpdateCB;
 }
 
 void OnProjectChange() {
     Debug.LogWarning("OnProjectChange");
 }
 
 public void OnGUI()
 {
     EditorGUILayout.SelectableLabel((EditorApplication.update!=null)?EditorApplication.update.ToString():"NONE");
 }

}

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

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

2 People are following this question.

avatar image avatar image

Related Questions

AssetPostprocessor: How to persist changes to a model 1 Answer

breaking out of OnGUI() ticks? 1 Answer

How can I simulate the "f" hotkey in the editor? 4 Answers

Unity API for checking if an object was modified 3 Answers

Resizing an array of gameObjects 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