Hi, I wanted to create a custom GUI List but it went wrong.
I want it like this
But instead it become like this
Please Help!!!
Here’s the script:
using System.Text.RegularExpressions;
using NaughtyAttributes;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(SequenceDialogueMultiLanguage))]
public class SequenceDialogueMultiLanguageEditor : Editor
{
private SerializedProperty languageProp;
private SerializedProperty dialogueListProp;
private SerializedProperty dialogueIndonesianProp;
private SerializedProperty dialogueEnglishProp;
private void OnEnable()
{
languageProp = serializedObject.FindProperty("language");
dialogueListProp = serializedObject.FindProperty("dialogueList");
dialogueIndonesianProp = serializedObject.FindProperty("dialogueIndonesian");
dialogueEnglishProp = serializedObject.FindProperty("dialogueEnglish");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(languageProp);
Languages selectedLanguage = (Languages)languageProp.enumValueIndex;
EditorGUILayout.LabelField("Dialogue List:");
EditorGUI.indentLevel++;
if (selectedLanguage == Languages.Indonesian)
{
EditorGUILayout.PropertyField(dialogueIndonesianProp, true);
if (dialogueIndonesianProp != null && dialogueIndonesianProp.isArray)
{
// Iterate through the dialogue list and draw individual elements
for (int i = 0; i < dialogueIndonesianProp.arraySize; i++)
{
SerializedProperty dialogueElementProp = dialogueIndonesianProp.GetArrayElementAtIndex(i);
SerializedProperty characterSpriteProp = dialogueElementProp.FindPropertyRelative("characterSprite");
SerializedProperty dialogueProp = dialogueElementProp.FindPropertyRelative("dialogue");
SerializedProperty dialogueSpriteProp = dialogueElementProp.FindPropertyRelative("dialogueSprite");
EditorGUILayout.PropertyField(characterSpriteProp, true);
EditorGUILayout.PropertyField(dialogueProp, true);
EditorGUILayout.PropertyField(dialogueSpriteProp, true);
}
}
}
else if (selectedLanguage == Languages.English)
{
EditorGUILayout.PropertyField(dialogueEnglishProp, true);
}
EditorGUI.indentLevel--;
EditorGUILayout.Space();
// Display the previous inspector GUI
DrawDefaultInspectorExcept(dialogueListProp, dialogueIndonesianProp, dialogueEnglishProp);
// Display the [Button] attributes from SequenceBaseModel
DrawButtonAttributes();
serializedObject.ApplyModifiedProperties();
}
private void DrawButtonAttributes()
{
// Access the target object to get the actual instance of SequenceBaseModel
SequenceBaseModel sequenceBaseModel = (SequenceBaseModel)target;
// Use reflection to find all methods with the [Button] attribute
var methods = sequenceBaseModel.GetType().GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
foreach (var method in methods)
{
var buttonAttributes = method.GetCustomAttributes(typeof(ButtonAttribute), true);
if (buttonAttributes.Length > 0)
{
string formattedName = FormatMethodName(method.Name);
if (GUILayout.Button(formattedName))
{
method.Invoke(sequenceBaseModel, null);
}
}
}
}
private string FormatMethodName(string methodName)
{
// Use regular expression to split method name by capital letters and keep acronyms together
return Regex.Replace(methodName, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])", " $1");
}
// Draw the default inspector GUI except for specified properties
private void DrawDefaultInspectorExcept(params SerializedProperty[] exceptions)
{
SerializedProperty iterator = serializedObject.GetIterator();
bool enterChildren = true;
while (iterator.NextVisible(enterChildren))
{
enterChildren = false;
if (!ShouldExcludeProperty(iterator, exceptions))
{
EditorGUILayout.PropertyField(iterator, true);
}
}
}
// Check if a property should be excluded from the default inspector GUI
private bool ShouldExcludeProperty(SerializedProperty property, SerializedProperty[] exceptions)
{
foreach (SerializedProperty exception in exceptions)
{
if (property == exception)
{
return true;
}
}
return false;
}
}