Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lachee1 · Apr 13, 2014 at 06:02 AM · colorproperty drawer

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)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikeNewall · Apr 13, 2014 at 06:58 AM 0
Share

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

avatar image Lachee1 · Apr 13, 2014 at 07:23 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lachee1 · Apr 13, 2014 at 10:12 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

HSV Selector? 1 Answer

HSV to RGB without EditorGUIUtility.HSVToRGB 3 Answers

Material doesn't have a color property '_Color' 4 Answers

Accessing material of particle renderer?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges