- Home /
Get Inspector to display class as color field
Hello there,
I am making a custom colour class that uses HSV values instead of RGB. I am nearly done, I have completed all the conversion and it can now implicitly convert HSV to Color and vise versa.
All I need to do know is display HSV varibles as a color varible in the inspector, but I have no idea how or if it is even possible! I know I will have to use UnityEditor or even maybe Property Drawers, But I don't know where to start.
if its even possible, I wish for the ColorHSV variables to appear as normal Color variables in the inspector, without having to recreate my own colour swatch using sliders and textures. How can I do this (examples would be appreciated)?
note: As stated before, the HSV class (ColorHSV) can implicitly convert to and from unitys color class (Color)
edit: made the question a tad clearer
edit: class can be found here http://pastebin.com/3P7MbeVS
(sorry if this isn't clear, but any help would be appreciated)
Not sure I totally understand what you mean :p But I think you want to have the hue and saturation editable and have the resulting colour show in the inspector?
You could use sliders to set the hue and saturation values and then convert from hsv using your script to a unity color and display it in a swatch.
https://docs.unity3d.com/Documentation/ScriptReference/EditorGUIUtility.DrawColorSwatch.html
Yes, what you said is basicly what I wish to archive. I wish for the ColorHSV variables to appear as normal Color variables in the inspector, without having to recreate my own colour swatch using sliders and textures
Answer by Lachee1 · Apr 16, 2014 at 08:38 AM
I have managed to get it to work using PropertyDrawers and EditorGUI.ColorField. Here is the editor code:
[CustomPropertyDrawer(typeof(ColorHSV))]
public class ColorHSVDrawer: PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
float h = property.FindPropertyRelative("_h").floatValue;
float s = property.FindPropertyRelative("_s").floatValue;
float v = property.FindPropertyRelative("_v").floatValue;
float a = property.FindPropertyRelative("_a").floatValue;
ColorHSV c = EditorGUI.ColorField(position,label,new ColorHSV(h,s,v,a));
property.FindPropertyRelative("_h").floatValue = c.h;
property.FindPropertyRelative("_s").floatValue = c.s;
property.FindPropertyRelative("_v").floatValue = c.v;
property.FindPropertyRelative("_a").floatValue = c.a;
}
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
return 16F;
}
}
and the changes I had to make with the class:
/// <value>The Hue (0 to 360)</value>
[SerializeField]
private float _h;
public float h
{
get {
return _h;
}
private set {
_h = value;
}
}
/// <value>The Saturation (0 to 1)</value>
[SerializeField]
private float _s;
public float s
{
get {
return _s;
}
private set {
_s = value;
}
}
/// <value>The Value (0 to 1)</value>
[SerializeField]
private float _v;
public float v
{
get {
return _v;
}
private set {
_v = value;
}
}
/// <value>The Alpha (0 to 1)</value>
[SerializeField]
private float _a;
public float a
{
get {
return _a;
}
private set {
_a = value;
}
}
Not sure if this is the most efficient way of doing it, but it works :D
Answer by SkaredCreations · Apr 13, 2014 at 09:08 AM
You said that your class has proper conversion to/from Color, so if I can suppose something like the following:
[System.Serializable]
public class ColorHSV {
public Color color {
get { return Color.white; /*get Color from your class values*/ }
set { /*set your class values from Color*/ }
}
}
public class MyScript : MonoBehaviour {
public ColorHSV myColor;
}
Then I can write the following inspector script (to be placed inside a folder called "Editor" somewhere in your Assets folder tree):
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(MyScript))]
public class MyScriptInspector : Editor {
public override void OnInspectorGUI ()
{
//Uncomment the following to show also the default inspector view
//base.OnInspectorGUI ();
MyScript myScript = target as MyScript;
myScript.myColor.color = EditorGUILayout.ColorField("Select the color", myScript.myColor.color);
// if ColorHSV implicitly converts to/from Color, then comment the above and uncomment the following:
//myScript.myColor = EditorGUILayout.ColorField("Select the color", myScript.myColor);
}
}
That works well, thank you. However I wish to make the editor to do it automaticly, not having to write a custom inspector code for every script that uses it. I have uploaded a copy of the class to help, it can be located here http://pastebin.com/3P7$$anonymous$$beVS it might not use proper formating or use best practice, so im sorry in advance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing two different objects renderer colour 1 Answer
HSV Selector? 1 Answer
HSV to RGB without EditorGUIUtility.HSVToRGB 3 Answers