Search Unity

Override Vector3 property drawer

Discussion in 'Immediate Mode GUI (IMGUI)' started by dgoyette, Mar 23, 2018.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    I wanted to create a simple property drawer override for Vector3, so that the labels "x", "y", and "z" appear in the same color as the axis colors in the global preferences. I've created property drawers for my own custom ScriptableObjects and MonoBehaviors, but it seems like my PropertyDrawer for Vector3 isn't being used at all. Is this not the way to cause all Vector3s in all inspectors to use a different style/layout?

    Code (csharp):
    1.  
    2.     [CustomPropertyDrawer(typeof(Vector3))]
    3.     public class ColorfulVector3PropertyDrawer : PropertyDrawer {
    4.  
    5.         // Doesn't matter what's here...Unity seems to just show the default Vector3 property drawer
    6.     }
    7.  
    Thanks,

    -Dan
     
  2. lokinwai

    lokinwai

    Joined:
    Feb 11, 2015
    Posts:
    174
    Only custom class work for PropertyDrawer with object type, you may try PropertyAttribute for Vector3.
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Thanks, Iokinwai. In that case, I'd need to manually put the attribute on all Vector3 fields that I wanted to exhibit this behavior. That's not ideal, since I especially wanted to override the display of the Position and Rotation fields in the Transform inspector, and I can't add attributes to the properties of Transform. Maybe I should look at making a custom drawer for Transform instead, if there's no way to globally override the drawer for Vector3.

    Thanks for the help.

    -Dan