Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Any way to use property attributes in custom inspector?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Sharlatan, Jun 13, 2015.

  1. Sharlatan

    Sharlatan

    Joined:
    Jul 23, 2013
    Posts:
    111
    Hey everyone! I've got a question concerning property attributes and custom inspectors: Is it possible in anyway to use the former when using the latter?

    I was working on a script and the [TextArea(int, int)] attribute was just working perfectly for a certain string I had in the inspector. But then the need to make some attributes in the inspector read only when certain conditions are met, forced me to write a custom inspector.

    Since I can't just use the normal properties anymore, I tried to use the EditorGUILayout.TextArea() within a ScrollView. It kinda works but it's not nearly as good as the TextArea I get when using the attribute. Some issues I have with it:
    • I can't just set how many lines I want at least or at most
    • The height used in the inspector is always equal to MaxHeight, even when not needed
    • If the TextArea gets higher than the ScrollView and vertical scroll bars start to appear, the scroll view doesn't automatically scroll down
    • etc
    I know this are all things I could theoretically implement myself. But to get it working as smooth as the TextArea property does would probably be quiet some work and why waste my time reinventing the wheel if I maybe don't have to?

    That's why I'm wondering myself if there's any way to use property attributes in custom inspectors or if I at least can find the source code for the [TextArea] PropertyDrawer somewhere.

    Thanks very much for your time and help!
     
  2. skalev

    skalev

    Joined:
    Feb 16, 2012
    Posts:
    264
    In your custom inspector, get the property, and just use EditorGUI.PropertyField() / EditorGUILayout.PropertyField.

    This will cause the custom inspector to draw the field based on any defined drawers.