Skip to content

Instantly share code, notes, and snippets.

@BadCoyoteStudios
Created April 26, 2020 16:36
Show Gist options
  • Save BadCoyoteStudios/a29eed73ae34ceb1eac1a15187e90b32 to your computer and use it in GitHub Desktop.
Save BadCoyoteStudios/a29eed73ae34ceb1eac1a15187e90b32 to your computer and use it in GitHub Desktop.
A simple in-editor visualizer for the Colors class generated by Blobinet.
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Simple browser for the Colors class
/// available @ https://gist.github.com/Blobinet/d37f3bc259a43bfbfecec7dd5e376354
/// </summary>
public class ColorsBrowser : EditorWindow
{
//public static ColorsBrowser I { get { return GetWindow<ColorsBrowser>(); } }
Dictionary<string, Color> color_dict;
List<string> colors_list;
int pageNum = 0;
int pageMax = 0;
int colorsPerRow = 4;
int rowsPerPage = 10;
int colorsPerPage;
int blockWidth = 180;
int blockHeight = 40;
[MenuItem("Window/BadCoyote/Colors Browser %&#c")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(ColorsBrowser));
var win = GetWindow(typeof(ColorsBrowser));
win.minSize = new Vector2(900, 900);
}
void OnEnable()
{
Initialize();
}
void Initialize()
{
color_dict = new Dictionary<string, Color>();
colors_list = new List<string>();
int iters = 0;
Type type = typeof(Colors);
foreach (var p in type.GetFields(BindingFlags.Public|BindingFlags.Static))
{
var n = p.Name;
var v = p.GetValue(null);
color_dict[n.ToString()] = (Color)v;
colors_list.Add(n.ToString());
iters++;
}
colorsPerPage = colorsPerRow * rowsPerPage;
pageMax = colors_list.Count / colorsPerPage;
}
void OnGUI()
{
if (color_dict.Count < 1 || colors_list.Count < 1)
{
return;
}
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("", GUILayout.Width(160));
if (GUILayout.Button("Page " + pageNum, GUILayout.Width(200), GUILayout.Height(40)))
{
// Non-functional button, just for displaying the current page number.
}
EditorGUILayout.LabelField("", GUILayout.Width(100));
if (GUILayout.Button("Home\n<<", GUILayout.Width(60), GUILayout.Height(40)))
{
if (pageNum != 0)
{
pageNum = 0;
Refresh();
}
}
if (GUILayout.Button("Prev\n<", GUILayout.Width(60), GUILayout.Height(40)))
{
if (pageNum > 0)
{
pageNum--;
Refresh();
}
}
if (GUILayout.Button("Next\n>", GUILayout.Width(60), GUILayout.Height(40)))
{
if (pageNum < pageMax)
{
pageNum++;
Refresh();
}
}
if (GUILayout.Button("End\n>>", GUILayout.Width(60), GUILayout.Height(40)))
{
if (pageNum != pageMax)
{
pageNum = pageMax;
Refresh();
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
EditorGUILayout.Space();
GUILayout.BeginArea(new Rect(10, 60, 1300, 1100));
for (int i = (pageNum * colorsPerPage); i < ((pageNum + 1) * colorsPerPage); i++) // -1
{
if (i < colors_list.Count)
{
int iter = i - pageNum * colorsPerPage;
GUI.Label(new Rect(
80 + (iter % colorsPerRow) * (blockWidth + 10), // rect x
10 + (iter / colorsPerRow) * (blockHeight + 40), // rect y
blockWidth, // rect w
blockHeight), // rect h
//$"{i}:{iter}: " +
$"{colors_list[i]}");//, guiStyle);
Rect thisRect = new Rect(
80 + (iter % colorsPerRow) * (blockWidth + 10), // rect x
40 + (iter / colorsPerRow) * (blockHeight + 40), // rect y
blockWidth, // rect w
blockHeight); // rect h
GUI.DrawTexture(thisRect, GenerateColorBlock(
color_dict[colors_list[i]], blockWidth, blockHeight),
ScaleMode.ScaleToFit, true);
}
}
GUILayout.EndArea();
}
Texture2D GenerateColorBlock(Color col, int w, int h)
{
Texture2D tex = new Texture2D(w, h);
tex.filterMode = FilterMode.Point;
var fillColorArray = tex.GetPixels();
for (int i = 0; i < fillColorArray.Length; i++)
{
fillColorArray[i] = col;
}
tex.SetPixels(fillColorArray);
tex.Apply(true);
return tex;
}
Texture2D GenerateTextureFromColorArray(Color[] caTexture, int w, int h)
{
Texture2D tex = new Texture2D(w, h);
tex.filterMode = FilterMode.Point;
tex.SetPixels(caTexture);
tex.Apply(true);
return tex;
}
bool GUIButtonTexture2D(Rect r, Texture2D t)
{
GUI.DrawTexture(r, t);
return GUI.Button(r, "", "");
}
public void Refresh()
{
Repaint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment