- Home /
Create a Component Script for Editor Only
Hi guys, I'm working on some editor stuff right now.
so, I'm trying to create a component script that can be attached to any game objects in a scene, but this component are just for managing editor stuff, so when I'm build my game, this script will not included in my game.
how I do that? trying to tag the Game Object to EditorOnly
just make the whole script including game object not included in build, also trying to move my component script to Editor folder just make it disappear from add component menu.
You could remove the component inside Awake() or Start() methods.
hmm, seems working, but I've already mark my script as ExecuteInEdit$$anonymous$$ode
. Don't it will also removed automatically when I'm starting Unity?
Answer by maccabbe · May 11, 2015 at 10:01 PM
You could try using directives. For instance Application.Quit() did not work for Unity Editor so I added code inside a directive that did work in Unity Editor
Application.Quit();
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying=false;
#endif
hmmm, not the best one, but well this seems making sense. combined with Antony comment, my script will look like this :
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[ExecuteInEdit$$anonymous$$ode]
#endif
public class EditorData : $$anonymous$$onoBehaviour{
void Awake(){
#if !UNITY_EDITOR
Destroy(this);
#else
//the rest of code
#endif
}
#if UNITY_EDITOR
//the rest of editor functions
#endif
}
thanks! never $$anonymous$$ding why not use directives ins$$anonymous$$d.
If you want to make that a bit more readable, I suggest having a base class with shared functionality, then deriving it into "editor-only" and "game-only" subclasses. Then you can use the directive once to instantiate the correct one, and the code will be much more readable and easy to maintain.
Your answer
Follow this Question
Related Questions
RequireComponent at variable level? 1 Answer
Initialising List array for use in a custom Editor 1 Answer
Multiple Cars not working 1 Answer
Get if any component in gameObject has changed 1 Answer
Distribute terrain in zones 3 Answers