Last active
February 14, 2022 23:38
-
-
Save kraj0t/6fef0d2493cde445eaefcd634013215a to your computer and use it in GitHub Desktop.
GUIBackgroundColorScope - quickly set color of your GUI (buttons, panels...) same as EditorGUI.DisabledScope and others
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 System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public struct GUIBackgroundColorScope : IDisposable | |
{ | |
private static readonly Stack<Color> _colorStack = new Stack<Color>(); | |
public GUIBackgroundColorScope(Color color) | |
{ | |
_colorStack.Push(GUI.color); | |
GUI.backgroundColor = color; | |
} | |
public void Dispose() | |
{ | |
GUI.backgroundColor = _colorStack.Pop(); | |
} | |
} |
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
// Example usage | |
private void MyGUIMethod() | |
{ | |
using var greenScope = new GUIBackgroundColorScope(Color.green); | |
if (GUILayout.Button("Look at me, I'm green!")) | |
{ | |
using var cyanScope = new GUIBackgroundColorScope(Color.cyan); | |
EditorGUILayout.HelpBox("And I am cyan!", MessageType.Info); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment