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?
Do you also want to disable their components, or just hide them - make them invisible?
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.
This is a very clever solution, will work will with UI stuff too
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
It only hide the object from the Hierarchy panel... It doesn't make it invisible on the Scene view.
And how do I implement that in the scope of a tool? We are rewriting the Hierarchy panel to add new features (such as folders, icon, color, etc). One feature is a "visible" icon to hide and unhide item while editing the level. Kinda annoying to add a MonoBehaviour around just to make a tool work.
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);
}
}
}
Do you also want to disable their components, or just hide them - make them invisible?
– DavidDebnarInvisible, in the Scene View, only in editor mode.
– LightStriker