- Home /
Question by
CoreyLmao · Feb 05, 2020 at 08:07 AM ·
editor-scriptingeditorguieditorscriptlist of listsarray list
How to create an editor script that contains an array of a class within a list
Hey, been struggling to put together an editor script for my dialogue event class.
I want to have my dialogue reorderable, which would make things infinitely easier when developing my game. However I have ran into an issue where I need to display an array of another class in the inspector. Basically this is what I have so far (I'm new to this so excuse the code if it's bad): Editor Script
[CustomEditor(typeof(DialogueTrigger))]
public class _DialogueTriggerEditor : Editor
{
private ReorderableList dialogueList;
public void OnEnable(){
dialogueList = new ReorderableList(serializedObject,
serializedObject.FindProperty("dialogue"),
true, true, true, true);
dialogueList.drawHeaderCallback = (Rect rect) => {
EditorGUI.LabelField(rect, "Dialogue Events");
};
dialogueList.onRemoveCallback = (ReorderableList l) => {
if (EditorUtility.DisplayDialog("Warning!",
"Are you sure you want to delete this Dialogue Event?", "Yes", "No")) {
ReorderableList.defaultBehaviours.DoRemoveButton(l);
}
};
dialogueList.drawElementCallback =
(Rect rect, int index, bool isActive, bool isFocused) => {
var element = dialogueList.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
GUIStyle myHeaderStyle = new GUIStyle(EditorStyles.label);
myHeaderStyle.fontStyle = FontStyle.BoldAndItalic;
myHeaderStyle.fontSize = 12;
Color myHeaderColor = Color.blue;
myHeaderStyle.normal.textColor = myHeaderColor;
EditorGUI.LabelField (new Rect(rect.width / 2 - 40, rect.y, rect.width, EditorGUIUtility.singleLineHeight), "Dialogue Event " + index, myHeaderStyle);
EditorGUI.PropertyField(
new Rect(rect.x, rect.y + 20, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("charName"), new GUIContent("Speaker Name:"));
EditorGUI.PropertyField(
new Rect(rect.x, rect.y + 35, rect.width, 75),
element.FindPropertyRelative("sentence"), new GUIContent("Sentence:"));
EditorGUI.PropertyField(
new Rect(rect.x, rect.y + 115, rect.width, 75),
element.FindPropertyRelative("characters"), new GUIContent("Characters"));
EditorGUI.LabelField (new Rect(rect.x, rect.y + 140, rect.width, EditorGUIUtility.singleLineHeight), "Audio Settings", EditorStyles.boldLabel);
EditorGUI.PropertyField(
new Rect(rect.x, rect.y + 155, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("sceneMusic"), new GUIContent("Music:"));
EditorGUI.PropertyField(
new Rect(rect.x, rect.y + 173, rect.width, EditorGUIUtility.singleLineHeight),
element.FindPropertyRelative("dialogueSFX"), new GUIContent("Additional SFX:"));
};
dialogueList.elementHeightCallback = (int index) => {
var elementHeight = 4;
if (elementHeight >= 1)
{
elementHeight--;
}
return (EditorGUIUtility.singleLineHeight * 5) + elementHeight * (EditorGUIUtility.singleLineHeight * 2.7f);
};
}
public override void OnInspectorGUI() {
serializedObject.Update();
dialogueList.DoLayoutList();
serializedObject.ApplyModifiedProperties();
}
}
Basically the "characters" property field is what I want displayed, however the most I've managed to get in the inspector is a foldout GUI that shows nothing.
While it doesn't work with my editor script, it works perfectly fine without it
charactersnotworking.png
(22.3 kB)
charactersworking.png
(27.3 kB)
Comment