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
3
Question by ThePunisher · Jan 19, 2012 at 08:06 PM · edit mode

Executing a script in edit mode

I have created a custom inspector for a script I made, and within this custom inspector there is a button which I want to use to "Test run" the script while in edit mode. The behavior I need is exactly like that of a particle system when it is selected during edit mode (so I know this HAS to be possible).

Is there a way of achieving the above mentioned behavior?

By the way, I have tried using the [ExecuteInEditMode] attribute, but this only seems to call the update method when something in the editor changes or I manually move the object. I could be using it wrong, but there isn't much to it unless I completely missed something. If anybody has a link to a good example of this being used, I would appreciate it if it was linked here.

After a little more research I found out that placing and selecting a Particle system into the scene will cause my script's update method to get constantly called while in edit mode. Does anybody know how unity achieves this behavior? They must be calling some method that forces the editor to update all scripts with the [ExecuteInEditMode] attribute.

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 ThePunisher · Jan 19, 2012 at 08:18 PM 0
Share

I came across the trick where you call EditorUtility.SetDirty(target) within the Editor script's OnInspectorGUI method, but is this really the best way to get a script's Update method to get repeatedly called?

3 Replies

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

Answer by .sanders · Jan 22, 2012 at 04:36 AM

Alright I found a workaround for you which I think might work, is also elegant and might have the same result as the Particle Emitter in unity . Just write a CustomEditor for your component and define an OnEnable() and OnDisable() method on it. In the OnEnable you add a CallbackFuntion delegate on the EditorApplication.update event. And in the OnDisable you remove that CallbackFunction. Something like this:

 [CustomEditor(typeof(MyScript))]
 public class MyScriptEditor : Editor
 {
     private void CallbackFunction()
     {
         Debug.Log("updating...");
         // Call the update of the MyScript
     }

     void OnEnable()
     {
         EditorApplication.update += CallbackFunction;
     }

     void OnDisable()
     {
         EditorApplication.update -= CallbackFunction;
     }
 }

So as long as you have selected the right GameObject, it's update will get called. I tried with the Debug.Log and got updates regularly. Try it and see if it works. You might also want to call the Awake() or Start() in the OnEnable() method (if needed)

Cheers! And let me know if this does the job for you.

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 ThePunisher · Jan 23, 2012 at 01:54 PM 0
Share

Yep, that works. Thanks a lot .sanders

avatar image Xatoku · Jan 06, 2013 at 08:40 PM 0
Share

How do you call the update function though?

avatar image Insu · Jan 10, 2014 at 03:37 AM 0
Share

To Question above, try something like this.

 private void CallbackFunction()
 {
   $$anonymous$$yScript mTarget = target as $$anonymous$$yScript;
   mTraget.Update();
 }

As side note it can also be used to keep a coroutine alive in the editor.

Setting the target as dirty will make the editor call update and run other functions on the object, in my case 'IsRunning' is a variable used to indicate that the coroutine is active, if you need update to be called without a break just remove the if statement or use example above.

 private void CallbackFunction()
 {
   $$anonymous$$yScript mTarget = target as $$anonymous$$yScript;
 
   if (mTarget.isRunning) {
     EditorUtility.SetDirty(mTarget);
   }
 }
avatar image
0

Answer by .sanders · Jan 19, 2012 at 11:33 PM

I don't know if it's possible but can't you just call the Update() of the script in the Update() of the CustomEditor script? Either make the method public or use the sendMessage() to the corresponding GameObject.

So as long as the CustomEditor is visible (which is when you've selected a GameObject that has your script attached to it), your script will also execute it's Update() function. Of course you can tweak that afterwards as you like

Hope this helps, if this works let me know alright!

cheers

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 ThePunisher · Jan 20, 2012 at 08:06 PM 0
Share

Thanks for the idea .sanders. Since my custom inspector class is deriving from Editor, it doesn't seem to have an actual Update method. Ins$$anonymous$$d I wrote my logic in OnInspectorGUI() which seems to behave the same way. I manually called the script's update loop to get my object to move, just like you suggested, and it works! The only problem was that it didn't really update the object within the scene as often as I initially hoped, so I ended up calling SceneView.RepaintAll() within the OnInspectorGUI method (after updating the object of course).

avatar image ThePunisher · Jan 20, 2012 at 08:08 PM 0
Share

I am still curious as to how Unity makes the Particle Emitter work in edit mode, but I suppose that will be another question since your suggestion does the job.

avatar image .sanders · Jan 22, 2012 at 04:19 AM 0
Share

Hmm it's strange indeed that the OnInspectorGUI() is not called that much. I would think it gets called as often as the OnGUI() in-game, which is twice every frame. I'll try and find that out. Can't think of anything else right now to get the Update() called for the GameObject as often as in running mode.

I'll get back to you.

avatar image .sanders · Jan 22, 2012 at 04:37 AM 0
Share

@ThePunisher I think I might have found what you are looking for. Check the next answer I wrote...

avatar image
0

Answer by -_-_-_-_-_ · Apr 26, 2016 at 06:43 AM

In the Scripting API, it was mentioned that [ExecuteInEditMode] will only call update if something has been changed, because there are no frames running in edit mode, but the regular UnityEngine.MonoBehavior.Update() is invoked each frame, and if are really no frames the regular Update() can't really be invoked by Unity. See this. And talking about Particles, they are simulated which means that while running, they're in their little local play mode with nothing else in it.

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

8 People are following this question.

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

Related Questions

Catch a unity undo/redo event. 2 Answers

Why my ParticleSystem tree object loops correctly in edit mode, but only part of it in play mode? 0 Answers

Problem to moving objects with collider in edit mode 2 Answers

Control the way objects are selected in edit mode 0 Answers

Turn on Shuriken "Simulate" from script in Edit mode? 1 Answer


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