Last active
June 30, 2022 20:07
-
-
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
This file contains 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 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