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 Unity3D ColorHSV Class - Pastebin.com

(sorry if this isn’t clear, but any help would be appreciated)

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);
	}
}

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 :smiley: