PropertyDrawer



Ingredient.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public enum IngredientUnit { Spoon, Cup, Bowl, Piece }

// Custom serializable class
[System.Serializable]
public class Ingredient 
{
    public string name;
    public int amount = 1;
    public IngredientUnit unit;
    
    //public int list;
    public List list = new List();
}

 
  

IngredientDrawer.cs

// C# example.
using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer
{

    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        int indent = EditorGUI.indentLevel;
        EditorGUI.indentLevel = 0;

        // Calculate rects
        Rect amountRect = new Rect(position.x, position.y, 30, position.height);
        Rect unitRect = new Rect(position.x + 35, position.y, 50, position.height);
        Rect nameRect = new Rect(position.x + 90, position.y, position.width - 90, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("amount"), GUIContent.none);
        EditorGUI.PropertyField(unitRect, property.FindPropertyRelative("unit"), GUIContent.none);
        EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("name"), GUIContent.none);


        Debug.Assert(property.FindPropertyRelative("list") != null, "list");

        //Debug.Assert(property.FindPropertyRelative("table") != null, "table");


        // Set indent back to what it was
        EditorGUI.indentLevel = indent;

        EditorGUI.EndProperty();
    }
}

Recipe.cs

using UnityEngine;
using System.Collections;
public class Recipe : MonoBehaviour
{
    public Ingredient potionResult;
    public Ingredient[] potionIngredients;
      
}


注意:

Note that for performance reasons, EditorGUILayout functions are not usable with Property Drawers.


扩展:

IngredientDrawer.cs

using UnityEditor;
using System.Collections;

[CustomPropertyDrawer(typeof(Ingredient))]
public class IngredientDrawer : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        int propertCount = property.CountInProperty();
        return propertCount * 40;
    }

    // Draw the property inside the given rect
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        Debug.Log(position);
        
        label = EditorGUI.BeginProperty(position, label, property);

        Rect contentPosition = EditorGUI.PrefixLabel(position, label);
        contentPosition.height = 20;
        EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("amount"), GUIContent.none);
        contentPosition.y += 40f;
        EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("unit"), GUIContent.none);

        EditorGUI.EndProperty();

    }
}


参考网址:

http://catlikecoding.com/unity/tutorials/editor/custom-data/





你可能感兴趣的:(unity3d,扩展编辑器)