Skip to content

Instantly share code, notes, and snippets.

@Invertex
Last active April 5, 2023 04:20
Show Gist options
  • Save Invertex/cd76e8c5cbc8b69f14a9d34896d439a3 to your computer and use it in GitHub Desktop.
Save Invertex/cd76e8c5cbc8b69f14a9d34896d439a3 to your computer and use it in GitHub Desktop.
Unity - Auto-hide UI in GameView when not in play mode.
using UnityEngine;
//Place on Camera you want to stop rendering UI while out of Play mode.
//UI Canvas needs to have its "Render Mode" be ing either "World Space" or "Screen Space - Camera" for this to work.
[ExecuteAlways, ExecuteInEditMode, RequireComponent(typeof(Camera))]
public class GameWindowHideUIUntilPlay : MonoBehaviour
{
void OnEnable()
{
ToggleUIDisplay(Application.isPlaying);
}
void OnDisable()
{
ToggleUIDisplay(true);
}
void ToggleUIDisplay(bool visible)
{
var cam = GetComponent<Camera>();
if(visible)
{
cam.cullingMask |= LayerMask.GetMask("UI"); //Add UI Layer from camera
}
else
{
cam.cullingMask &= ~LayerMask.GetMask("UI"); //Remove UI Layer from camera
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment