Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

PropertyDrawer - Compact for Vector2/Vector3.

Discussion in 'Editor & General Support' started by jocyf, Nov 16, 2012.

  1. jocyf

    jocyf

    Joined:
    Jan 30, 2007
    Posts:
    282
    Hi,

    Trying to make a property Drawer to work. It does it right, but can't make space in the inspector.
    Here is the code:

    CompactAttribute.cs
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. /// <summary>
    5. /// Prepares variables to be used by PopupDrawer.
    6. /// </summary>
    7.  
    8. // This is not an editor script. The property attribute class should be placed in a regular script file.
    9. public class CompactAttribute : PropertyAttribute {
    10.    
    11.     public CompactAttribute () {
    12.         return;
    13.     }
    14. }
    CompactDrawer.cs
    Code (csharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4. using System;
    5.  
    6. /// <summary>
    7. /// Creates a compact list with the provided values (Vectors).
    8. /// </summary>
    9.  
    10. [CustomPropertyDrawer(typeof(CompactAttribute))]
    11. class CompactDrawer : PropertyDrawer {
    12.     // Draw the property inside the given rect
    13.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    14.         EditorGUIUtility.LookLikeControls ();
    15.         position.xMin += 4;
    16.         position.xMax -= 4;
    17.         //position.yMax += 50;
    18.         // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
    19.         if (property.propertyType == SerializedPropertyType.Vector3){
    20.             property.vector3Value = EditorGUI.Vector3Field(position, label.text, property.vector3Value);
    21.         }
    22.         else
    23.         if (property.propertyType == SerializedPropertyType.Vector2){
    24.             property.vector2Value = EditorGUI.Vector2Field(position, label.text, property.vector2Value);
    25.         }
    26.         else
    27.             EditorGUI.LabelField (position, label.text, "Use Compact with Vector3 or Vector2.");
    28.         //EditorGUILayout.Space();
    29.     }
    30. }
    31.  
    Testing the new PropertyDrawer:

    Test.js
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. @Compact
    5. var Pos : Vector3 = Vector3(0,0,0);
    6.  
    7. function Update () {
    8.     this.transform.position = Pos;
    9. }
    10.  
    ¿Any tip for getting the EditorGUILayout.Space(); in the drawer to work? it do it right, but with one console error.
     
  2. KeirMeikle

    KeirMeikle

    Joined:
    Oct 16, 2012
    Posts:
    4
    The proper way to correct the vertical spacing is to override GetPropertyHeight, like so:

    Code (csharp):
    1.  
    2. public override float GetPropertyHeight (SerializedProperty prop, GUIContent label)
    3. {
    4.    float extraHeight = 20.0f;  
    5.    return base.GetPropertyHeight(prop, label) + extraHeight;
    6. }
    7.  
     
  3. termway

    termway

    Joined:
    Jul 5, 2012
    Posts:
    7
unityunity