Property Drawer for lookup property.

Hi,

I have a ScriptableObject that records strings as names, and are referred to throughout the code as indexes. integer indexes (see DamageTableDB below). In my attempt to make this more user-friendly. So I created the ArmorType class as a way to reference the armor while being able to make a property drawer for it. The idea is a drop down to select an index, and then back-feed that index into the object.

But

The example I found to work off of might not be the way to go, or it might be if I can get some help. I want the drop down to show the list of armor types (this is working), but also to assign the value back when one is selected. Below are the classes involved. The goal is a drop down that I can collapse into a property drawer. I am just stuck at being able to put the value back in the serialized property. Any suggestions?

P.S. I also get a ton of exceptions operating this editor. type is not a valid int type. What do I need to implement (IEquatable?) for unity to be able to fetch the int value?

using System;
using System.Collections;
using UnityEngine;

[Serializable]
public class ArmorType
{
    public int reference;

    public string name
    {
        get
        {
            DamageTableDB db = DamageTableDB.LoadDB();
            return db.armorTypeList[reference].name;
        }
    }

    public static bool operator ==(ArmorType a, int b)
    {
        return a.reference == b;
    }
    public static bool operator !=(ArmorType a, int b)
    {
        return a.reference != b;
    }


}
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(ArmorType))]
public class ArmorTypeDrawerUGUI : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        DamageTable.Init();
        var armorTypes = DamageTable.AllArmorTypes();

        label.text += " - " + armorTypes[property.intValue].name;
        if (!EditorGUI.DropdownButton(position, label, FocusType.Passive))
        {
            return;
        }

        GenericMenu menu = new GenericMenu();

        for (int armorID = 0; armorID < armorTypes.Count; armorID++)
        {
            menu.AddItem(new GUIContent(armorTypes[armorID].name), false, handleItemClicked, armorID);
        }

        menu.DropDown(position);
    }

    private void handleItemClicked(object parameter)
    {
        Debug.Log(parameter);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[CreateAssetMenu(fileName ="DamageTableDB", menuName ="DBs/DamageTable", order = 1)]
public class DamageTableDB : ScriptableObject
{
    public List<ArmorData> armorTypeList = new List<ArmorData>();
    public List<DamageData> damageTypeList = new List<DamageData>();

    public static DamageTableDB LoadDB()
    {
        return Resources.Load("DB/DamageTableDB", typeof(DamageTableDB)) as DamageTableDB;

    }

    public static DamageTableDB instance;
    public static DamageTableDB Init()
    {
        if (instance != null) return instance;
        instance = LoadDB();
        return instance;
    }
}

I think what you’re trying to do is almost the exact same as what Vault does. Link.

I made an attribute [AssetDropdown(typeof(MyType)] that can go on anything inheriting from a base type I use (to avoid noise). It makes a searchable drop-down menu appear when clicked and whatever is selected gets stored as a reference (not index) directly into the field.