Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question PropertyField with no property drawers

Discussion in 'Scripting' started by Toack, Aug 16, 2023.

  1. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    107
    I have a script with this fields

    Code (CSharp):
    1.         [Header("Clockwise vertices starting at top-left")]
    2.         [SerializeField] private Vector2 corner1;
    3.         [SerializeField] private Vector2 corner2;
    4.         [SerializeField] private Vector2 corner3;
    5.         [SerializeField] private Vector2 corner4;
    Now I'm creating a custom inspector to draw the corners fields in a nicer way using PropertyLabel. But then 'corner1' is drawing the Header.

    upload_2023-8-16_10-28-21.png

    Is there a way to prevent it?
     
  2. Saniell

    Saniell

    Joined:
    Oct 24, 2015
    Posts:
    164
    Draw field directly using EditorGUI.Vector2Field.
    Also why do you use header if you're making custom inspector anyway?
     
    Bunny83 likes this.
  3. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    107
    Thanks for the suggestion.

    So it is not possible then?
     
  4. Saniell

    Saniell

    Joined:
    Oct 24, 2015
    Posts:
    164
    Not to my knowledge. EditorGUI.PropertyField always uses attributes assigned to a field
     
  5. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    107
    Thanks for the responses, I will see my way around implementing the field directly with EditorGUI.Vector2Field.
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    Depending on what you actually want to achieve it might make sense to actually wrap your 4 variables into a serializable class and just create a propertydrawer for that custom class instead. So the propertydrawer would reserve as much vertical space as it needs (GetPropertyHeight) to lay out the 4 fields the way you want.

    In this day and age you almost never need to write a CustomInspector as most stuff should be done through propertydrawers. That way you get multi-object editing, DecoratorDrawers support (like that Header attribute) out of the box. A CustomInspector for a certain MonoBehaviour or ScriptableObject might make sense if you need / want a fundamentally different UI for your component / object.
     
    spiney199 likes this.