Search Unity

Resolved PropertyDrawer OnGUI position Rect Wrong Values

Discussion in 'Immediate Mode GUI (IMGUI)' started by owen_proto, May 9, 2021.

  1. owen_proto

    owen_proto

    Joined:
    Mar 18, 2018
    Posts:
    120
    PropertyDrawer.OnGUI's position parameter throws junk values between the real values (I'm using 2021.2.0a14, not sure about other versions). I've attached a picture of the console that shows position.width being logged when resizing the inspector's width. the "1" values are incorrect. If the property starts at an indented position (like a property being drawn in a list), it throws a negative value.

    Here are some scripts to quickly reproduce the issue - put MyMonoBehaviour on an object and resize the inspector's width:

    Code (CSharp):
    1. using System;
    2.  
    3. [Serializable]
    4. public class MyClass
    5. {
    6. }
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(MyClass))]
    5. public class MyClassPropertyDrawer : PropertyDrawer
    6. {
    7.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    8.     {
    9.         Debug.Log(position.width);
    10.     }
    11. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MyMonoBehaviour : MonoBehaviour
    4. {
    5.     public MyClass myClass;
    6. }
     

    Attached Files:

    Last edited: May 9, 2021
  2. akianto

    akianto

    Joined:
    Aug 9, 2018
    Posts:
    3
    I suppose that the reason of your issue in event types. Unity passes lots of events, and each of them calls OnGUI. Some events don't know about right Rect position and size. You can simply check this by this code

    Code (CSharp):
    1. public override void OnGUI(Rect _Rect, SerializedProperty _Property, GUIContent _Label)
    2.     {
    3.         Debug.Log($"Property rect {_Rect}, Evt type {Event.current.type}");
    4.     }
    And you will realize that you're receiving wrong rect size only in "Layout" event, however at Repaint event it's quite correct. Check the image below.

    Actually UI renders only on Repaint event.

    Don't care about wrong Rect on Layout event :)
     

    Attached Files:

    booferei, Jubessin and owen_proto like this.
  3. owen_proto

    owen_proto

    Joined:
    Mar 18, 2018
    Posts:
    120
    Thanks for the reply! It totally escaped me to check for the gui event type. I'm not sure how I've avoided issues with this previously in property drawers having written them many times. Thanks again.