Skip to content

Instantly share code, notes, and snippets.

@rodrigod89
Last active June 30, 2022 20:07
Show Gist options
  • Save rodrigod89/4d4a30515a347aaa571ccaa66fcec30e to your computer and use it in GitHub Desktop.
Save rodrigod89/4d4a30515a347aaa571ccaa66fcec30e to your computer and use it in GitHub Desktop.
Unity Custom editor: Add a Hex input field to the color picker for faster color assignment
using UnityEditor;
using UnityEngine;
// Add this under an Editor folder in the Unity project
[CustomPropertyDrawer(typeof(Color))]
public class ColorHexDrawer : PropertyDrawer
{
// Draw the property inside the given rect
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
position.width -= 80f;
EditorGUI.PropertyField(position, property, label);
Color color = property.colorValue;
var newP = new Rect(position);
newP.x += newP.width;
newP.width = 80f;
string hex = EditorGUI.TextField(newP, ColorUtility.ToHtmlStringRGBA(color));
if (ColorUtility.TryParseHtmlString("#" + hex, out Color newColor))
{
if (color != newColor)
{
property.colorValue = newColor;
}
}
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment