I am trying to work with displaying a list of options inside a collection element, initially I had an enum, a target object and an enum but as I realized I needed the enums to be dynamic (changing depending on which gameobject is in the 2nd element), I switched to string lists. This works great, I can query the object assigned to the target element and grab it's list and copy it to the one displayed in the mono behavior governing the master list.
My problem then became that I could find an example of displaying the list as a popup easy enough, but I wanted to use ReorderableLists in the custom inspector to neatly organize the list of Actions, Targets, and TargetAction. (Actions, and TargetActions are Lists).
Normally the examples I've found for displaying fields inside the ReorderableList use
EditorGUI.PropertyField(RectDimentions, element.FindPropertyRelative("propertyName"), GUIContent.none);
I found no way to display these lists as a popup since they're serialized and the popup wants a string[].
I did see a wonderful demo by DerHugo at: How to make an enum-like Unity inspector drop-down menu from a string array with C#?
But try as I may I am not sure how draw the data from my class element and display it the way he has.
This is what my class is doing:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
[System.Serializable]
public class ConnectorFields {
public List<string> eventList;
public Connector Target;
public List<string> actionList;
}
public class ConnectionController : MonoBehaviour {
[SerializeField] public List<ConnectorFields> connectors = new List<ConnectorFields>();
private IInteractable interactable;
private Connector target;
// Populates the sourceEvent and targetAction list for each item in 'connectors'
private void OnValidate() {
interactable = GetComponent<IInteractable>();
for (int i = 0; i < connectors.Count; i++) {
connectors[i].eventList = interactable.SourceEventList.ToList();
if (connectors[i].Target != null) {
target = connectors[i].Target.GetComponent<Connector>();
connectors[i].actionList = target.TargetActionList.ToList();
} else {
connectors[i].actionList.Clear();
}
}
And this is what I'm currently using to display the connectors collection:
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
[CustomEditor(typeof(ConnectionController))]
public class ConnectorListPropertyDrawer : Editor
{
private ReorderableList list;
[SerializeField] bool clickToSelect = false;
private void OnEnable()
{
list = new ReorderableList(serializedObject, serializedObject.FindProperty("connectors"), false, true, true, false);
list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
var element = list.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
EditorGUI.PropertyField(new Rect(rect.x, rect.y, 100, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("eventList"), GUIContent.none);
EditorGUI.PropertyField(new Rect(rect.x + 105, rect.y, rect.width - 105 - 155, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("Target"), GUIContent.none);
if (GUI.Button(new Rect(rect.x + rect.width - 150, rect.y, 20, EditorGUIUtility.singleLineHeight), "?")) { clickToSelect = true; }
EditorGUI.PropertyField(new Rect(rect.x + rect.width - 125, rect.y, 100, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("actionList"), GUIContent.none);
if (GUI.Button(new Rect(rect.x + rect.width - 20, rect.y, 20, EditorGUIUtility.singleLineHeight), "-")) { removeConnection(); }
};
list.drawHeaderCallback = (Rect rect) =>
{
EditorGUI.LabelField(rect, "Connectors List");
};
}
// more stuff after this unrelated to displaying the table
}
If anyone has examples or information that can help me understand the methodology to display a list as a popup within the ReorderableList I'd appreciate it.