Last active
April 5, 2023 04:20
-
-
Save Invertex/cd76e8c5cbc8b69f14a9d34896d439a3 to your computer and use it in GitHub Desktop.
Unity - Auto-hide UI in GameView when not in play mode.
This file contains hidden or 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 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