I have an editor window for editing items in an inventory. The area I am working on is the portion for building recipes for craftable items. The class for craftable items contains a List for itemID’s of each material needed to make the desired item.
public List<int> materialIDs;
If I show this in the editor window using a propertyfield:
SerializedProperty prop1 = serObj.FindProperty("craftItem").FindPropertyRelative("materialIDs");
EditorGUILayout.PropertyField (prop1, new GUIContent("Required material IDs"), true);
I get a result like this in the editor window:
What I am trying to do… is replace the textboxes shown in the default rendering of this list, with a dropdown selection of all of the items currently existing in the item database so that instead of having to go look up itemIDs for each material, the user can simply select the material item from the dropdown list and behind the scenes in the editor window it will assign the selected items itemID as that element in the materialIDs list.
ie this is the intended result:
I have the initial rendered materialID list… I have a list pre-populated with all of the items currently existing in the database, is it possible for me to “stuff” the values of the existing items list into/in place of, the textboxes for the materialID list elements so that the user can select the desired item, instead of having to go look up its assigned ID?
Yes, but it’s not straightforward. You need to make your own drop down control. Here’s some sample code that does what you need.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{
// ---------------------------------------------------
// this is just a placeholder, you need to change this to fit your project
// by taking the data from your database
class ItemData
{
public int id;
public string name;
}
List<ItemData> database = new List<ItemData>()
{
new ItemData() { id = -1, name = "(Unset)" }, // might want to keep an invalid id for unset items
new ItemData() { id = 1, name = "Small Healing Potion" },
new ItemData() { id = 2, name = "Medium Healing Potion" },
new ItemData() { id = 3, name = "Large Healing Potion" },
};
// ---------------------------------------------------
// prepare a list of item names to use with Popup();
// Might be more efficient to do this once somewhere than
// rebuild it each time the component gets selected
string[] itemNames;
void OnEnable()
{
itemNames = new string[database.Count];
for (int i = 0; i < database.Count; i++)
itemNames _= database*.name;*_
}
// --------------------------------------------------- // Do the actual drawing bool listExpanded = true; public override void OnInspectorGUI() {
listExpanded = EditorGUILayout.Foldout(listExpanded, “Required material IDs”); if (listExpanded) { materialIDs.arraySize = EditorGUILayout.IntField(“Size”, materialIDs.arraySize); for (int i = 0; i < materialIDs.arraySize; i++) { // get serialized item id from component int itemID = materialIDs.GetArrayElementAtIndex(i).intValue;
// assuming the array itemNames is in the same order as database, we can do this int index = database.FindIndex(item => item.id == itemID);
// draw the combo box / popup / pulldown / whatchamacallit index = EditorGUILayout.Popup("Element " + i, index, itemNames);
// now work backwards to update the property itemID = database[index].id; materialIDs.GetArrayElementAtIndex(i).intValue = itemID; } }