Hide Object in Editor Only

I know about GameObject.SetActive or Renderer.enable… However, both offer the problem that if you forget about them, they won’t show up in game (or will even turn off scripts and behaviours).

I need a way to hide object only in editor mode, WITHOUT changing the normal in-game behaviour.

Is there such a way?

For me the easiest solution was to use layers.
Create a “HideInEditor” layer, then use the dropdown at the top right of the Editor (“Layers”) to toggle the layer visibibility.

The solution we took was rather complex, since every Unity’s flags are also used in-game.

We went when a Editor-only dictionary that keeps the real value of visibility of every object. If you modify the visibility manually in the Inspector, we take it as being the real value. If you modify it within our tool, it’s a “fake” value that is reverted when you try to save the scene or when you change the play state of the editor.

Using this, our users can hide part of the scene without fear of messing up something in-game.

18146-hierarchy.png

edit

I guess you could do this:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class HideInEditor : MonoBehaviour {

	void OnEnable () {
		renderer.enabled = !Application.isEditor || Application.isPlaying;
	}

	void OnDisable () {
		renderer.enabled = true;
	}
}

Not sure if the Application.isEditor test is really needed, but that works for me. Just attach it to what ever you want to hide and hide it by enabling the Behavior. Disabling will show it again.

Before edit

This should do the trick

Here’s Mene’s script adapted for Canvas elements.

using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class HideCanvasElementInEditor : MonoBehaviour {
	void OnEnable () {
		CanvasRenderer[] canvasRenderers = transform.GetComponentsInChildren<CanvasRenderer>();
		float a;
		if(!Application.isEditor || Application.isPlaying){
			a = 1f;
		}else{
			a = 0;
		}
		foreach(CanvasRenderer cr in canvasRenderers){
			cr.SetAlpha(a);
		}
	}

	void OnDisable () {
		CanvasRenderer[] canvasRenderers = transform.GetComponentsInChildren<CanvasRenderer>();
		foreach(CanvasRenderer cr in canvasRenderers){
			cr.SetAlpha(1f);
		}
	}
}

SetAlpha is working for me but there might be a more efficient way of doing it.

here is a fairly simple solution. create an empty gameobject, attach this script, then simply add all objects you want to hide into the “hide” list:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class HideInEditor : MonoBehaviour {
 
     public GameObject[] hide;
 
     void Update()
     {
         foreach(GameObject go in hide) {
 
             if (Application.isPlaying)
                 go.SetActive(true);
             else
                 go.SetActive(false);
         }
     }
 }