- Home /
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?
Do you also want to disable their components, or just hide them - make them invisible?
Answer by LightStriker · Nov 19, 2013 at 06:04 PM
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
Answer by nerik · Jan 30, 2014 at 02:50 PM
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.
If you want to Hide the Canvas UI just hide the layer UI in the Layer Combobox
Answer by Mene · Nov 18, 2013 at 10:27 PM
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
http://docs.unity3d.com/Documentation/ScriptReference/HideFlags.HideInHierarchy.html
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. $$anonymous$$inda annoying to add a $$anonymous$$onoBehaviour around just to make a tool work.
I havn't worked with tools much yet. The problem is you would need to enable the renderer again, if and only iff your tool has disabled it. That means you need to react to things like build and the play-button. The easiest way i can come up with right now is to use the tool to attach this behavior to all nodes with your hide-in-editor feature. This would also make sure it works on vanilla unity editors. But as I said I havn't worked much with extensions/tools yet. BTW I think you can use the HideInHierarchy-Property I first sugested to hide the component, too. This way it won't "pollute" the editor view.
Answer by BillyMFT · Nov 29, 2016 at 01:42 PM
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.
Answer by stechmann · Jun 07, 2018 at 01:02 PM
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);
}
}
}
Your answer
Follow this Question
Related Questions
Object Visibility 2 Answers
Objects Appearing 1 Answer
Unity 2019.4.9f1 - Toggle Show/Hide Gameobject with one keystroke. 1 Answer
[FIXED & CLOSED!] How do I show and hide 3D text? 2 Answers
Re-hiding a guiTexture 1 Answer