Skip to content

Instantly share code, notes, and snippets.

@kraj0t
Last active February 14, 2022 23:38
Show Gist options
  • Save kraj0t/6fef0d2493cde445eaefcd634013215a to your computer and use it in GitHub Desktop.
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
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();
}
}
// 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