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

Custom Editor Inspector side-distance

Discussion in 'Scripting' started by Jacky2611, Oct 9, 2015.

  1. Jacky2611

    Jacky2611

    Joined:
    Mar 2, 2013
    Posts:
    12
    Ok, so I am just developing my first real game in unity and to make my room-preset editor easier to understand I decided to replace two ints (width & length) with two custom sliders.
    I created a custom Vector2Int class and a drawer. Worked fine. After a bit of struggeling I even maaged to stop the slider from lagging.

    Then I resized my window. Big mistake. It looks like my slider is not always lined up with the unity fields.
    Can someone provide my with the correct values to fix this?

    Problem

    Code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomPropertyDrawer(typeof(Vector2Int))]
    6. public class Vector2IntDrawer : PropertyDrawer
    7. {
    8.         float fx =3;
    9.     int ix = 3;
    10.  
    11.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    12.     {
    13.  
    14.         SerializedProperty x = property.FindPropertyRelative("x");
    15.         SerializedProperty z = property.FindPropertyRelative("z");
    16.  
    17.  
    18.         EditorGUI.LabelField(new Rect(position.xMin, position.yMin, position.width, position.height), label);
    19.  
    20.         // Check if modified, to avoid overwriting the property constantly
    21.     EditorGUI.BeginChangeCheck();
    22.         int newX = EditorGUI.IntField(new Rect(position.x + position.width * 0.4f, position.y, position.width, position.height), "X", x.intValue);
    23.         if (newX < 0) newX = 0;
    24.         if (EditorGUI.EndChangeCheck())
    25.         {
    26.             x.intValue = newX;
    27.         }
    28.  
    29.  
    30.     }
    31.  
    32.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    33.     {
    34.         return base.GetPropertyHeight(property, label);
    35.     }
    36. }
    37.  
    38.  
    39.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Theres this function MultiFloatField
    http://docs.unity3d.com/ScriptReference/EditorGUI.MultiFloatField.html
    which might help
    Or just use the defualt vector2field, and rounds the values to ints in changeCheck

    as for why your code is doing that, you have told it to start the field at 40% the width of position
    presumably the default v2field doesn;t do this
     
  3. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    By using GUILayout.Width you can edit the module's width in order to make it fit as you want.
     
  4. Jacky2611

    Jacky2611

    Joined:
    Mar 2, 2013
    Posts:
    12
    Yeah, the 40% are just one of my closest attempts. I have tried different numbers for hours now. I was hoping that maybe someone with access to the unity source Code could tell me the exact values. :D