Ok, now this has puzzled me for a day or two, and I can't really figure it out. Here it goes.
Does anybody have any idea how the particle emitter is able to update in edit mode? I know about the [ExecuteInEditMode] attribute, but that only seems to be called when the unity Editor has changed (new object selected or public members in the inspector were modified). I have reason to believe that the particle emitter is either triggering a specific event or calling some specific editor method that is forcing the editor to update more frequently. Here is the reason why I think this may be happening. Copy the following code into a new c# script called Example and drag the script onto a GameObject in the scene.
public class Example : MonoBehaviour
print("Update for " + this.name + " was called.");
Move the object around, or click on different assets in the project hierarchy, and you will notice that the print statement appears in the console window only when you change something. I know this is desired behavior because you wouldn't want the editor updating everything part all the time if nothing is changing, of course.
Ok, now clear the console window and add a particle emitter. Select the particle emitter if it isn't already selected and take a look at the console window. You will notice that the print statement for our script is being continuously called. If the particle emitter was simply calling the Update method of all of the particles then it shouldn't trigger the Update method on our script, yet it does. So then what is the particle emitter doing?
The real question is...how can I get the same update behavior as the particle emitter while in edit mode of the editor (not play mode)? I know this may be sound awkward, but thanks for any suggestions, ideas, or comments in advanced.
This is a good question, and I probably don't know the answer, but is it possible Particle Emitter is doing its business in the OnDrawGizmos() method? This is how I generally get things to show up only in the Scene window. Of course, that does not explain why that code would be triggering the Update method.
Hi Smorpheus, thanks for your response. I haven't looked at OnDrawGizmos() yet, so I'll make sure I look into that. I agree with you though, I don't think that explains why it would trigger my scripts update.
Answer by Bunny83
Jan 21, 2012 at 01:55 AM
First i'd like to say that a particle emitter is not a script, it's a seperate component (not even a Behaviour, see the class hierarchy). Most of the built-in components are written in native code and just have an Mono / .NET interface for scripting. So if you want to achieve something similar it will work different than a particle emitter / camera / cloth / ...
If you want to implement an continous update in the editor you can use the `EditorApplication.update` -delegate. Keep in mind that all editor functions / classes (the whole UnityEditor namespace) is not available at runtime. So you have to use either conditional compilation to exclude this extra code from your build, or implement the functionality in a custom inspector. With a custom inspector it would only work while it's selected in the inspector.
Hi Bunny83, thank you for taking the time to respond. I understand the Editor scripts (or anything that references UnityEditor for that matter) can't be part of the build. As a matter of fact, I need this logic for a custom inspector and I need the object to be selected, so that would work out perfectly. Let me give your delegate suggestion a try.
That worked perfectly, Bunny83! Thanks a lot, you've really helped me here.
Can you post your solution? I'm trying to get a particle system to emit from the editor as well. I'm using the EditorApplication.update delegate as Bunny described. Here's my code:
EditorApplication.update += EditorUpdate;
EditorApplication.update -= EditorUpdate;
//this doesn't work
myParticleSystem.Emit(pos, Vector3.zero, particleSize, particleLifetime, color)
//but this does!
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
7 People are following this question.
What Does "ExecuteInEditMode" Actually Do?
using ExecuteInEditMode to create platforms in the editor
Turn on Shuriken "Simulate" from script in Edit mode?
ExecuteInEditMode : scene wants to be saved even after no change
Manually triggering a script from the editor (utility, macro etc.)