Search Unity

Editor only components

Discussion in 'Editor & General Support' started by hoesterey, Mar 24, 2017.

  1. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Hi,
    I understand I can use #if UNITY_EDITOR to ensure some component fields don't show up in the build. Is there a way to ensure a entire monobehavior only shows up in editor?

    I'd like to add a "note" component to some objects but want it to be removed when the project is built.

    Any thoughts on how to make this happen?
    Thanks!
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    You could use the PostProcessScene attribute on a method in an editor script which searches and removes the component from game objects. I do this with a couple of editor-only development components.
     
    hoesterey likes this.
  3. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Love it thanks!
     
  4. IsaiahKelly

    IsaiahKelly

    Joined:
    Nov 11, 2012
    Posts:
    418
    Actually, all you have to do is set the HideFlags on your MonoBehaviour, like so.

    Code (CSharp):
    1. private void Reset()
    2. {
    3.     hideFlags = HideFlags.DontSaveInBuild;
    4. }
    In this example I am doing so in Reset, to ensure it is set when you add the script to a GameObject.

    All UnityEngine.Objects contain a HideFlags bit mask, meaning you can set this on any Component or GameObject you want to exclude from builds. The only exception being the Transform component, which can not be removed itself without also removing the GameObject it is attached to, since it's a required component of the GameObject.
     
    EZaca, Alaadel and andymads like this.