我的自定义编辑器找不到我要查找的字段

Posted

技术标签:

【中文标题】我的自定义编辑器找不到我要查找的字段【英文标题】:My custom editor doesn't find the fields I'm looking for 【发布时间】:2021-12-27 11:17:46 【问题描述】:
void OnEnable()

    iteminfo = (ItemInfo)target;
    itemName = serializedObject.FindProperty(iteminfo.Name);


public override void OnInspectorGUI()

    serializedObject.Update();
    **EditorGUILayout.PropertyField(itemName);**
    serializedObject.ApplyModifiedProperties();

它说有一个空引用,我找不到在哪里更改它。

这是其他脚本

   public class ItemInfo : MonoBehaviour
    
        public Item Item_Info;
        public enum ItemType  Weapon, Armor, Potion, Other ;
        public ItemType itemtype;

        public int damage;

        public int Defense;

        public int heal_value;
    
        private Inventory playerinventory;
    

然后这里的项目脚本是

public class Item

    public string Name, Description;
    public bool stackable;
    public int Value, weight;
    public string ModelPath;

我试图在

中获取“名称”
public class Item

【问题讨论】:

在哪一行?你确定target 有效吗? @RetiredNinja 我认为它在“EditorGUILayout.PropertyField(itemName)”的行上,我认为目标不是问题。 您能向我们展示您的ItemInfo 实现吗? ItemInfo 没有任何名为 Name 的字段或属性 .... @derHugo 对不起,我忘了添加其他 2 个脚本,但你去吧 【参考方案1】:

这是你想要做的:

首先,如果不是这样的话,请创建Item[Serializable]

然后有例如这个编辑器

[CustomEditor(typeof(ItemInfo))]
public class ItemInfoEditor : Editor

    private SerializedProperty item;
    private SerializedProperty itemType;
    private SerializedProperty damage;
    private SerializedProperty defense;
    private SerializedProperty heal;

    private ItemInfo itemInfo;
    
    void OnEnable()
    
        itemInfo = (ItemInfo)target;
        
        // Link up the properties via the according field names
        item = serializedObject.FindProperty(nameof(ItemInfo.Item_Info));
        itemType = serializedObject.FindProperty(nameof(ItemInfo.itemtype));
        damage = serializedObject.FindProperty(nameof(ItemInfo.damage));
        defense = serializedObject.FindProperty(nameof(ItemInfo.Defense));
        heal = serializedObject.FindProperty(nameof(ItemInfo.heal_value));
    

    public override void OnInspectorGUI()
    
        // just draw the default script field
        DrawScriptField();
        
        serializedObject.Update();
        
        // draw the Item field itself
        EditorGUILayout.PropertyField(item, true);
        // draw the item type dropdown
        EditorGUILayout.PropertyField(itemType);
        
        // according to the selected value draw the according field(s)
        switch ((ItemInfo.ItemType)itemType.intValue)
        
            case ItemInfo.ItemType.Weapon:
                EditorGUILayout.PropertyField(damage);
                break;
            
            case ItemInfo.ItemType.Armor:
                EditorGUILayout.PropertyField(defense);
                break;
            
            case ItemInfo.ItemType.Potion:
                EditorGUILayout.PropertyField(heal);
                break;

            case ItemInfo.ItemType.Other:
                EditorGUILayout.PropertyField(damage);
                EditorGUILayout.PropertyField(heal);
                EditorGUILayout.PropertyField(defense);
                break;
        
        
        serializedObject.ApplyModifiedProperties();
    

    private void DrawScriptField()
    
        EditorGUI.BeginDisabledGroup(true);
        EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour(itemInfo), typeof(ItemInfo), false);
        EditorGUI.EndDisabledGroup();
        
        EditorGUILayout.Space();
    

【讨论】:

以上是关于我的自定义编辑器找不到我要查找的字段的主要内容,如果未能解决你的问题,请参考以下文章

定义实体属性值

Detox 找不到已指定 testId 的自定义组件

查找在 LWUIT 资源编辑器中定义的元素

在Wordpress上使用ACF上的自定义字段时找不到“图库”字段类型

编辑器网格中的自定义类型存储字段未正确映射

高分 我要打开电脑的默认共享但是找不到LanmanServerParameters