Skip to content

Instantly share code, notes, and snippets.

@mmechtley
Last active July 3, 2021 20:05
Show Gist options
  • Save mmechtley/a0e6efb53034197504b4e594428791e6 to your computer and use it in GitHub Desktop.
Save mmechtley/a0e6efb53034197504b4e594428791e6 to your computer and use it in GitHub Desktop.
Generic MonoBehaviour inspector for Unity that adds type-specific color-coding to the inspector header for visual distinction. Doesn't work when inspectors are collapsed though. ):
using UnityEngine;
namespace Code.Editor.Utilities
{
public static class EditorLayoutUtil
{
public enum RectDirection
{
Horizontal,
Vertical
}
public static (Rect leftTop, Rect rightBottom) Split( this Rect position, float size, RectDirection direction=RectDirection.Horizontal )
{
if(direction == RectDirection.Horizontal)
{
return (
new Rect( position.x, position.y, size, position.height ),
new Rect( position.x + size, position.y, position.width-size, position.height )
);
}
else
{
return (
new Rect( position.x, position.y, position.width, size ),
new Rect( position.x, position.y + size, position.width, position.height - size )
);
}
}
}
}
using System;
using System.Collections.Generic;
using Code.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Code.Editor.Editors
{
[CustomEditor(typeof(MonoBehaviour), true, isFallback = true)]
public class MonoBehaviourInspector : UnityEditor.Editor
{
private static float PickerWidth => 2f * EditorGUIUtility.singleLineHeight;
private static readonly Dictionary<string, Color> colorCache = new Dictionary<string, Color>();
public override void OnInspectorGUI()
{
var type = target.GetType();
var inspectorStyle = (GUIStyle) "IN Title";
var width = EditorStyles.inspectorDefaultMargins.fixedWidth;
var mockContent = new GUIContent( type.Name, AssetPreview.GetMiniTypeThumbnail( type ) );
var height = inspectorStyle.CalcHeight( mockContent, width )
+ inspectorStyle.margin.vertical
+ inspectorStyle.padding.vertical;
var topPadding = EditorStyles.inspectorDefaultMargins.padding.top;
var colorPos = EditorGUILayout.GetControlRect( true, height, GUILayout.ExpandWidth( true ) );
// HAHAHA We can GIVE THE RECT BACK??? XDXDXD
EditorGUILayout.GetControlRect( true, -height-topPadding, GUILayout.ExpandWidth( true ) );
colorPos.y -= colorPos.height + topPadding;
var pickerPos = EditorGUILayout.GetControlRect( true, EditorGUIUtility.singleLineHeight, GUILayout.ExpandWidth( true ) );
EditorGUILayout.GetControlRect( true, -EditorGUIUtility.singleLineHeight-topPadding, GUILayout.ExpandWidth( true ) );
( pickerPos, _ ) = pickerPos.Split( EditorGUIUtility.labelWidth );
( _, pickerPos ) = pickerPos.Split( pickerPos.width - PickerWidth );
DrawColoredBackground( colorPos, pickerPos, type );
base.OnInspectorGUI();
}
private static Color getColor( string key )
{
if( colorCache.TryGetValue( key, out var cached ) )
{
return cached;
}
var currentColor = EditorPrefs.GetString( key, "" );
if( !string.IsNullOrEmpty( currentColor ) )
{
var components = currentColor.Split( ',' );
if( components.Length == 4
&& float.TryParse( components[0], out var r )
&& float.TryParse( components[1], out var g )
&& float.TryParse( components[2], out var b )
&& float.TryParse( components[3], out var a ) )
{
colorCache[key] = new Color( r, g, b, a );
return colorCache[key];
}
}
colorCache[key] = Color.clear;
return colorCache[key];
}
public static void DrawColoredBackground( Rect position, Rect pickerPosition, Type type )
{
var prefKey = $"ComponentColor_{type.Name}";
var color = getColor( prefKey );
var prevColor = GUI.color;
GUI.color = color;
GUI.Box( position, GUIContent.none, WhiteTextureStyle );
GUI.color = prevColor;
EditorGUI.BeginChangeCheck();
var newColor = EditorGUI.ColorField( pickerPosition, color );
if( EditorGUI.EndChangeCheck() )
{
saveColor( prefKey, newColor );
}
}
private static void saveColor( string key, Color color )
{
colorCache[key] = color;
EditorPrefs.SetString( key, $"{color.r:f2},{color.g:f2},{color.b:f2},{color.a:f2}" );
}
private static GUIStyle whiteTextureStyle;
private static GUIStyle WhiteTextureStyle
{
get
{
if (whiteTextureStyle != null)
return whiteTextureStyle;
whiteTextureStyle = new GUIStyle
{
normal = {background = EditorGUIUtility.whiteTexture}
};
return whiteTextureStyle;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment