Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Last active January 11, 2020 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasirkula/e4068629a8a97e1bf0dedfc3cc1ed27f to your computer and use it in GitHub Desktop.
Save yasirkula/e4068629a8a97e1bf0dedfc3cc1ed27f to your computer and use it in GitHub Desktop.
Quickly toggle the visibility of UI layer (canvases) in Unity's Scene view
using UnityEditor;
using UnityEngine;
public class UIToggler
{
private const int UI_LAYER = 1 << 5;
[InitializeOnLoadMethod]
private static void Init()
{
#if UNITY_2019_1_OR_NEWER
SceneView.duringSceneGui -= OnSceneGUI;
SceneView.duringSceneGui += OnSceneGUI;
#else
SceneView.onSceneGUIDelegate -= OnSceneGUI;
SceneView.onSceneGUIDelegate += OnSceneGUI;
#endif
}
private static void OnSceneGUI( SceneView sceneView )
{
Handles.BeginGUI();
bool uiVisible = ( Tools.visibleLayers & UI_LAYER ) == UI_LAYER;
if( GUI.Button( new Rect( 0f, 0f, 125f, 25f ), uiVisible ? "Hide Canvas" : "Show Canvas" ) )
{
if( uiVisible )
Tools.visibleLayers &= ~UI_LAYER;
else
Tools.visibleLayers |= UI_LAYER;
}
Handles.EndGUI();
}
}
@yasirkula
Copy link
Author

How To

Simply create a folder called Editor inside your Project window and add this script inside it. Then, click the small button at the top left corner of the Scene view to toggle the UI objects' visibility in Scene view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment