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 MikeGameDev · Jul 10, 2014 at 09:40 PM · editorvaluecompiledefault

Editor Script Reverts to Default State on Script Compilition

Hello everyone,

Here is my situation: I have an editor script which can perform a bunch of tasks. Whether or not it executes a function is based on some simple bools which by default are set to false. It's up to the user to set them to true. It looks something like this:

 [ExecuteInEditMode]
 class MyScript : MonoBehaviour
 {
       // user can change these from the inspector
       public static bool performTaskA = false; 
       public static bool performTaskB = false;

      // update function is added to EditorApplication.update
      void static Update()
      {
            if (performTaskA)
                taskA();
            if (performTaskB)
                taskB();
      }
 
 }

My problem is whenever a script (any script) is compiled, the bools get reset to their default value of false. How can I get around 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 rutter · Jul 10, 2014 at 09:46 PM

The editor will unload (and then reload) all assemblies when compiling; any transient data in memory will be lost during that process. That's by design, but it can bite you if you're not expecting it.

Editor scripts need to be aware of this, so they can compensate for it.

If you have a lot of data you need to serialize, you might do it using components in a scene, prefab, or ScriptableObject. I've also seen plugins that use JSON files and the like.

In your case, it looks like you have only a few simple fields, so you can probably use something lightweight like EditorPrefs?

 EditorPrefs.SetBool("performTaskA", true);

 ...

 if (EditorPrefs.GetBool("performTaskA")) {
     taskA();
 }
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 MikeGameDev · Jul 10, 2014 at 09:59 PM 0
Share

Beautiful answer. It seems that the EditorApplication.update delegate is also reset on script compile, how can I re-add my function after compilation?

avatar image rutter · Jul 10, 2014 at 10:32 PM 0
Share

Hmm. That delegate might be tricky, unless you have something in the scene that catches the problem. $$anonymous$$aybe a component with ExecuteInEdit$$anonymous$$ode?

Over time, I've tried to keep most of my editor scripts compartmentalized: they're active only when something specifically tells or needs them to be. In that line of thinking, something that's "always on" would usually be attached in the scene.

avatar image
0

Answer by Bunny83 · Jul 10, 2014 at 10:06 PM

First of all that's not an editor script. It's a normal runtime script which just also runs at edit time (due to ExecuteInEditMode). MonoBehaviour scripts are always runtime scripts and will be part of your build game.

Besides that, when Unity recompiles a script, the whole scriptine environment is reloaded. So basically everything is lost. However Unity does serialize everything before the reload and recreates and deserializes everything afterwards, but the usual serialization rules apply. So static variables aren't serialized since they don't belong to an instance.

Using static variables can cause a lot of problems so it's generally better to use instance variables. Since you didn't showed the code which actually sets those booleans nor did you explain how those are actually set, i can't offer you a solution since there are many ways to do this. Give us some more information.

Comment
Add comment · Show 3 · 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 MikeGameDev · Jul 10, 2014 at 10:22 PM 0
Share

I did not know that about $$anonymous$$onoBehaviour scripts. Should I ins$$anonymous$$d drop the inheritance from $$anonymous$$onoBehaviour and do something like this:

     class $$anonymous$$yScript
     {
           // ...
     
          // Add Update to the EditorApplication.update delegate
          static void Update()
          {
                 //...
          }
     
     }

Regarding the values being reset, I will be switching to instance variables.

As I mentioned in my comment to rutter: it seems that the EditorApplication.update delegate is also reset on script compile, how can I re-add my function after compilation?

Thank you!

avatar image Bunny83 · Jul 11, 2014 at 12:17 AM 0
Share

Yes you should drop the inheritance. If you just want to hook the update delegate, just use the "InitializeOnLoad" attribute on your class. It will force the static constructor to run as soon as the environment is ready.

See the manual on this topic:
http://docs.unity3d.com/$$anonymous$$anual/RunningEditorCodeOnLaunch.html

$$anonymous$$ay i ask what kind of task need to be run at a rate of 100 times per second? In most cases editor scripts are either custom inspectors, editorwindows or wizards. Using the update delegate will make you code run all the time while the editor is open.

avatar image MikeGameDev · Jul 11, 2014 at 04:42 AM 0
Share

Very nice answer! That is exactly what I need.

I didn't show it in my samples, but I actually created a simple timer class to control the frequency of execution, the pseudo code is as follows:

 // called 100 times per second
 static void Update()
 {
      // To my knowledge UnityEngine.Time only works in Game
      // So I created a simple timer class which computes
      // deltaTime based on EditorApplication.timeSinceStartup.
      // If X amount of time has passed, the timer class sets
      // a bool (xAmountOfTimeHasPassed) to true.
      $$anonymous$$yTimerClass.update()
 
      // In my case X = 0.033 seconds, so this flag will be 
      // true every 30 frames.
      if ($$anonymous$$yTimerClass.xAmountOfTimeHasPassed)
      {
           // code to be executed every 30 frames here
           // **sub the body of the update function in the original question here.**
      }
 }

$$anonymous$$y script relies on a plugin which interfaces with a special peripheral which provides another means of input for the editor.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

default script editor now working anymore 0 Answers

Change Default Script Folder 5 Answers

Unity Script Editor Not Working 1 Answer

Script Editors for Unity 3 Answers

Hiding editor extension scripts 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