Search Unity

Editor Only Objects

Discussion in 'Scripting' started by cranky, Dec 5, 2014.

  1. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Hey guys, quick question:

    Is there a way to have editor only objects? For example, I have a Cubemap Renderer object that will only ever be used in the editor. It has a camera on it, so it can't be in the game at run time. Disabling/enabling is a bit inconvenient, so I was hoping for an automated way of doing it. I figured either a DebugOnly behavior or maybe another way to use some sort of tag already included with Unity?

    If I were to use a DebugOnly behavior, how could I make it not include it in the build? Perhaps and editor script?

    Thanks, guys!
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    you could use something like this to disable the gameobject when in play mode
    Code (csharp):
    1.  
    2. [ExecuteInEditMode]
    3. public class CurveGenerator : MonoBehaviour {
    4.  
    5. #if UNITY_EDITOR
    6. void Update () {
    7. if(UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) {
    8. this.enabled = false;
    9. } else {
    10. // editor code here!
    11. }
    12. }
    13. #endif
    14.  
    15. }
    Got the code from this link http://blog.brendanvance.com/2014/0...y-script-execution-in-unity3d/comment-page-1/

    Also remember to search the API reference http://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html
     
    Maddieman likes this.
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I had a script like this. The problem with this script is that the object will still load just to be destroyed.
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class DebugOnly : MonoBehaviour
    4. {
    5.   void Awake()
    6.   {
    7.     if (!Debug.isDebugBuild && Application.isPlaying)
    8.       Destroy(gameObject);
    9.   }
    10. }
    There is also the "EditorOnly" tag which should remove the object from actual builds, while still in the Unity Editor.
     
  4. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    Thanks for the responses, guys. I figured an OnEnable function to disable the object during runtime would suffice as a last resort, but it would be nice to prevent the object from even making it into the build. I thought maybe a build preprocessor, but then how could I tell it to not include the object in the build at all?

    The EditorOnly tag would not remove the entire GameObject from the build, if I am understanding it correctly. I am going to have quite a lot of objects in my hierarchy view at runtime, so minimal objects would be nice.

    Thanks for the help! :)

    EDIT: Sorry, it's a postprocessor actually: http://docs.unity3d.com/ScriptReference/Callbacks.PostProcessSceneAttribute.html

    Would calling Destroy in that callback destroy it in the hierarchy permanently, or just in the build?
     
  5. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    EditorOnly should remove the GameObject from the build.