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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

PropertyDrawer.OnGUI Rect Parameter Busted

Discussion in 'Scripting' started by topsekret, Jul 20, 2016.

  1. topsekret

    topsekret

    Joined:
    Apr 18, 2013
    Posts:
    69
    I am trying to make a custom PropertyDrawer for a custom PropertyAttribute. For some reason, every frame that OnGUI is called, the Rect that is passed to the function alternates between being normalized (x,y at the origin with width and height of 1) and having coordinates relative to the inspector panel (ex: x,y at (14, 319) and width and height of (459, 16)).

    I am trying to do some math to inline a text field with a button. It works great on the frames where Unity gives me the rect relative to the inspector panel, but doesn't work at all on the frames where Unity gives me the normalized rect.

    How the heck are you supposed to deal with this? I don't see any special handling in Unity's example about PropertyDrawers. Why can't Unity just pass me the rect relative to the inspector panel each frame?
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Can you possibly post your code so that we can help you fix it?
     
  3. topsekret

    topsekret

    Joined:
    Apr 18, 2013
    Posts:
    69
    So it turns out that my specific issue was due to my code not having the line:
    Code (CSharp):
    1. position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    like in the example. Without that line, my label and my string field were sharing the same rectangle, so when I was adding buttons to the right, if the buttons were wide enough, they could draw over the label.

    So the thing about getting two very different sizes of rectangles each frame (even if the inspector was not changing size) kinda led me on a wild goose chase.

    That said, I am still curious why this happens and how everything ends up working out. Cause I feel like it shouldn't. If you want to see what I am talking about regarding the rectangle parameter, you can just copy Unity's example PropertyDrawer on that page I linked to (the PropertyDrawer for the Ingredient class) and just put a breakpoint on the first line of OnGUI and observe the value of the Rect parameter over the course of several frames.