Search Unity

How do Property Drawer?

Discussion in 'Scripting' started by leegod, Aug 24, 2013.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    http://blogs.unity3d.com/2012/09/07/property-drawers-in-unity-4/

    in above example,

    Code (csharp):
    1.  
    2. [Popup ("Warrior", "Mage", "Archer", "Ninja")]
    3.     public string @class = "Warrior";  
    4.  [Popup ("Human/Local", "Human/Network", "AI/Easy", "AI/Normal", "AI/Hard")]
    5. public string controller;
    6.  
    [Popup(....)] doesn't work.

    says, namespace doesn't have popup.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,406
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    So eventually what scripts should be made and where put? Editor folder? what script?

    And how it can possible simple [ ] mechanic as unity official blog?

    Why unity so suck at document there is no such example like unity blog and there is none c# example?
     
  4. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
  5. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I am not sure what you mean by "none of script input position and structure, name."

    The C# version is almost the same as the UnityScript which is demonstrated ;)

    I have not tested the following, but I have converted one of the simpler examples from UnityScript to C# for you. I hope that this helps :)

    Code (csharp):
    1.  
    2. // Assets/Scripts/RangeAttribute.cs
    3. using UnityEngine;
    4.  
    5. public class RangeAttribute : PropertyAttribute {
    6.     public float min;
    7.     public float max;
    8.    
    9.     public RangeAttribute (float min, float max) {
    10.         this.min = min;
    11.         this.max = max;
    12.     }
    13. }
    14.  
    15.  
    16. // Assets/Scripts/Editor/RangeDrawer.cs
    17. using UnityEngine;
    18. using UnityEditor;
    19.  
    20. // The property drawer class should be placed in an editor script, inside a folder called Editor.
    21. // Tell the RangeDrawer that it is a drawer for properties with the RangeAttribute.
    22. [CustomPropertyDrawer (typeof(RangeAttribute))]
    23. public class RangeDrawer : PropertyDrawer {
    24.    
    25.     // Draw the property inside the given rect
    26.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    27.        
    28.         // First get the attribute since it contains the range for the slider
    29.         RangeAttribute range = attribute as RangeAttribute;
    30.        
    31.         // Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
    32.         if (property.propertyType == SerializedPropertyType.Float)
    33.             EditorGUI.Slider (position, property, range.min, range.max, label);
    34.         else if (property.propertyType == SerializedPropertyType.Integer)
    35.             EditorGUI.IntSlider (position, property, range.min, range.max, label);
    36.         else
    37.             EditorGUI.LabelField (position, label.text, "Use Range with float or int.");
    38.     }
    39. }
    40.  
    41.  
    42. // Assets/Scripts/CustomBehaviour.cs
    43. using UnityEngine;
    44.  
    45. public class CustomBehaviour : MonoBehaviour {
    46.     [Range(0f, 10f)]
    47.     public float someRange;
    48. }
    49.  
     
  7. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    I never thought it is possible that class name can be without derived from : Monobehavior

    well, thanks and this works well,

    (except revise like this)
    EditorGUI.InsSlider(position, property, (int)range.min, (int)range.max, label);

    then, how about other examples like [[Multiline]], [Compact]?

    So this feature is not internally already done, should I input code all for [Multiline], etc...?

    Then where can I find other example?

     
    Last edited: Aug 24, 2013