Home > Blockchain >  My custom editor doesn't find the fields I'm looking for
My custom editor doesn't find the fields I'm looking for

Time:11-17

void OnEnable()
{
    iteminfo = (ItemInfo)target;
    itemName = serializedObject.FindProperty(iteminfo.Name);
}

public override void OnInspectorGUI()
{
    serializedObject.Update();
    **EditorGUILayout.PropertyField(itemName);**
    serializedObject.ApplyModifiedProperties();
}

It says there is a null reference and i cant find where to change it.

Here are the other Scripts

   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;
    }

and then heres the item script is

public class Item
{
    public string Name, Description;
    public bool stackable;
    public int Value, weight;
    public string ModelPath;
}

Im trying to grab the "Name" in

public class Item

CodePudding user response:

Here is what you want to do:

First of all if not anyway the case make the Item class [Serializable].

Then have e.g. this editor

[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();
    }
}

enter image description here

  • Related