Search Unity

Editor Color Display Field

Discussion in 'Immediate Mode GUI (IMGUI)' started by alexisrabadan, Sep 24, 2015.

  1. alexisrabadan

    alexisrabadan

    Joined:
    Aug 26, 2014
    Posts:
    82
    Hi all,

    In need of a way to display a color without the color picker field, as it is simply for display purposes only and the developer won't be editing the color,

    Is there a way to alter the EditorGUILayout.ColorField so that is read only, or does anybody know of another way around this.

    Thanks
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I would create a CustomPropertyDrawer and PropertyAttribute for your field, then, in the drawer, draw a white box tinted with the properties colorValue:

    e.g:

    Code (CSharp):
    1. class MyClass : MonoBehaviour {
    2.  
    3.     [ReadonlyColor]
    4.     public Color myColor;
    5. }
    6.  
    7. class ReadonlyColorAttribute : PropertyAttribute {
    8. }
    9.  
    10. [CustomPropertyDrawer(typeof(ReadonlyColorAttribute))]
    11. class ReadonlyColorDrawer : PropertyDrawer {
    12.  
    13.     public override OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    14.    
    15.         if (property.propertyType == SerializedPropertyType.Color) {
    16.            
    17.             Color guiColor = GUI.color;
    18.            
    19.             GUI.color = property.colorValue;          
    20.             GUI.DrawTexture(position, EditorGUIUtility.whiteTexture);
    21.             GUI.color = guiColor;
    22.         }
    23.         else {
    24.        
    25.             EditorGUI.PropertyField(position, property, label);
    26.         }
    27.     }
    28. }
    That code is untested, might be errors.