Last active
December 26, 2024 18:22
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
Tools.lockedLayers |= UI_LAYER; | |
} | |
else | |
{ | |
Tools.visibleLayers |= UI_LAYER; | |
Tools.lockedLayers &= ~UI_LAYER; | |
} | |
} | |
Handles.EndGUI(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.