Unity自定义检查器,允许选择的子对象

本鲁宾

我正在尝试为PlayerInventory课程创建自定义检查器PlayerInventory班由项目(这是一个列表ScriptableObjects),每个项目的数量,这样一起:

public class Item : ScriptableObject
{
   public string description;
   public Sprite picture;
}

public class InventoryItem : ScriptableObject
{
   // A reference to the Item object that contains the item's description, 
   // picture, stats, etc.
   public Item item;

   // The quantity of this item that the player has in his inventory
   public int quantity;
}

[CreateAssetMenu(menuName = "Player/Player Inventory")]
public class PlayerInventory : ScriptableObject
{
   // The list of each distinct item that the player has in his inventory,
   // along with the quantity of each item
   public List<InventoryItem> items;
}

我创建了一个PlayerInventory作为游戏资产的实例默认情况下,检查器显示的列表InventoryItem但是,我要的是使检查器显示每个InventoryItem元素,并Item为它选择一个字段,一个要输入数量的字段和一个“删除”按钮。

这是我要实现的视觉示例,以及下面的当前代码。该屏幕截图的问题在于Unity使我InventoryItem为每个元素选择一个对象。我希望能够Item为每个元素选择一个对象。我在第二个问题是我不知道如何将设置EditorGUILayout.TextFieldInventoryItem.quantity属性,因为我不知道如何转换SerializedProperty到一个InventoryItem对象。

在此处输入图片说明

[CustomEditor(typeof(PlayerInventory))]
public class PlayerInventoryEditor : Editor
{
   public override void OnInspectorGUI()
   {
       this.serializedObject.Update();

       SerializedProperty items = this.serializedObject.FindProperty("items");
       for (int i = 0; i < items.arraySize; i++)
       {
           EditorGUILayout.BeginHorizontal();
           EditorGUILayout.LabelField("Item", GUILayout.Width(50));

           // I don't know how to make this line reference the child "item"
           // field of the current InventoryItem
           EditorGUILayout.PropertyField(items.GetArrayElementAtIndex(i), GUIContent.none, GUILayout.Width(170));

           EditorGUILayout.LabelField("    Quantity", GUILayout.Width(80));

           // I don't know how to set the text field to the "quantity" field
           // of the current InventoryItem
           EditorGUILayout.TextField("0", GUILayout.Width(50));

           EditorGUILayout.LabelField("", GUILayout.Width(20));
           GUILayout.Button("Delete Item");               
           EditorGUILayout.EndHorizontal();
       }

       GUILayout.Button("Add Item");
   }
}
我飞

类必须可序列化才能出现在检查器中。请参阅可序列化

编辑在下面的评论中进行进一步的讨论之后,下面是完整的摘要;

标有标准类Serializable;

[System.Serializable]
public class InventoryItem
{
    public Item item;
    public int quantity;
}

使用PlayerInventoryEditor;

public override void OnInspectorGUI ()
{
    this.serializedObject.Update();

    SerializedProperty items = this.serializedObject.FindProperty("items");

    for (int i = 0; i < items.arraySize; i++)
    {
        SerializedProperty item = items.GetArrayElementAtIndex(i);

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Item", GUILayout.Width(50));
        EditorGUILayout.PropertyField(item.FindPropertyRelative("item"), GUIContent.none, GUILayout.Width(170));
        EditorGUILayout.LabelField("    Quantity", GUILayout.Width(80));
        EditorGUILayout.IntField(item.FindPropertyRelative("quantity").intValue, GUILayout.Width(50));

        EditorGUILayout.LabelField("", GUILayout.Width(20));
        GUILayout.Button("Delete Item");
        EditorGUILayout.EndHorizontal();
    }

    GUILayout.Button("Add Item");
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Unity中使自定义检查器添加对象引用

Unity-检查器中结构的自定义绘图

使用自定义检查器扩展Unity UI组件

在Unity自定义检查器中设置枚举控件

Unity:ScriptableObject的自定义检查器不起作用

如何创建可折叠的汉堡菜单图标自定义检查器/编辑器Unity

自定义对象检查清单

带有身份检查器的自定义Xcode选择器

Unity3D 自定义检查器未正确保存值

Unity-使用自定义检查器时可以访问OnValidate()吗?

Unity-基于结构文件的检查器中的自定义结构名称

是否可以在Unity的自定义检查器中显示Partial枚举?

自定义元素选择器

自定义号码选择器

自定义垫选择以允许嵌套值成角度

手动打开可序列化对象的自定义检查器

在对象检查器中显示自定义类型的值

目标C:身份检查器中的自定义类会创建该类的新对象?

如何使自定义验证器*检查Vuejs中*props*中对象的每个属性*

Unity 自定义编辑器 Raycast 在实例化对象后无法立即工作

如何正确键入自定义选择器函数,该函数从对象中选取属性

在自定义对象上使用jQuery之类的选择器

JPA,实体管理器,选择许多列并获取结果列表自定义对象

检查自定义对象数组是否包含特定的自定义对象

带有自定义阵列适配器的微调器不允许选择项目

使用文档选择器选择自定义元素

检查自定义列多项选择的默认值

如何检查复杂对象的自定义相等性?

Chrome开发控制台中的自定义对象检查