greenbutton
greenbutton

Reputation: 31

Unity Editor: ExecuteInEditMode vs Editor scripts

What are the use-cases for using ExecuteInEditMode and what are for Editor scripts? When to use one instead of another?

Upvotes: 2

Views: 6475

Answers (2)

user9474008
user9474008

Reputation:

ExecuteInEditMode - This is an attribute for scripts, denoted as [ExecuteInEditMode]. By default, MonoBehaviours are only executed in play mode. By adding this attribute, any instance of the MonoBehaviour will have its callback functions executed while the Editor is not in playmode. Use-Cases for this include, but are not limited to:

  1. Position constraining – your script may help you position your game objects by constraint object positions using a custom algorithm.
  2. Connecting objects – if the ComponentA of the ObjectA requires instance of the ComponentB that is somewhere on the scene, and you are able to find it using your code, then you can do it automatically instead of making the designer do it manually later.
  3. In-Editor warnings and errors – Unity will generate a standard set of errors if something is wrong with your code or the initial Unity components setup. Still, you won’t receive any warning if you create a room without doors or windows by mistake. This can be done by a custom script, so the designer is warned when editing.
  4. Management scripts – These are scripts that are keeping things in order. Let’s say that your scene has 5 cameras, but all these cameras should have the same FOV parameters. Instead of changing those manually (yes, you can select all of them, but there’s no guarantee that someone else will do the same) you can make your management script adjust all values using one master value. As a result all single camera parameters will be locked and can be changed only by using the management script.

The Knights of Unity have released a tutorial on some sample functionality for ExecuteInEditMode that expands on this.

Editor Scripts - This is a collection of scripts that extend the Editor class, a Base class to derive custom Editors from. This can be used to create your own custom inspector guis and editors for your objects. For more information, check out this video on Editor Scripting. Since Editor Scripts are programmed, you may also want to view the Scripting API.

Upvotes: 6

Krzysztof Bogdan
Krzysztof Bogdan

Reputation: 963

With Editor you customize MonoBehaviour appearance.

For ExecuteInEditMode most use cases would be modification of Scene View, for example Mesh Renderer (I do not know if Mesh Renderer uses ExecuteInEditMode but it could), it will render Mesh in game but it does also render this mesh in scene view.

Some other use cases: validation, communication with other components, modification other components, modification gameobjects, basically you can do most of things that you could do in-game and in-editor.

Upvotes: 0

Related Questions