Custom Editor showing twice?
Hi
I've been trying to make a custom editor to show lists in a different manner in my scriptable objects. I kept having an issue where it wasn't rendering right (It would show twice in the inspector even though it should only show once.)
While trying to find a fix, I found a tutorial about the same thing I was doing, so I tried following it. What's really confusing is when I followed it I had the same issue.
This is what the issue I'm getting looks like, since it's sort of hard to explain in words:
It should only be showing The Size and list of elements once, instead it's showing them twice. I tried googling for solutions and the only one I could find suggested setting the layout to default and closing/reopening Unity. I tried this, as well as uninstalling all versions of Unity/Unity Hub and reinstalling, and tried updating versions of unity to the latest stable release.
This is the ListEditor class: using UnityEditor; using UnityEngine; using System;
[Flags]
public enum ListEditorOptions
{
None = 0,
ListSize = 1,
ListLabel = 2,
ElementLabels = 4,
Buttons = 8,
Default = ListSize | ListLabel | ElementLabels,
NoElementLabels = ListSize | ListLabel,
All = Default | Buttons
}
public static class ListEditor
{
private static GUIContent
moveButtonContent = new GUIContent("\u21b4", "move down"),
duplicateButtonContent = new GUIContent("+", "duplicate"),
deleteButtonContent = new GUIContent("-", "delete"),
addButtonContent = new GUIContent("+", "add element");
private static GUILayoutOption miniButtonWidth = GUILayout.Width(20f);
public static void Show(SerializedProperty list, ListEditorOptions options = ListEditorOptions.Default)
{
if (!list.isArray)
{
EditorGUILayout.HelpBox(list.name + " is neither an array nor a list!", MessageType.Error);
return;
}
bool
showListLabel = (options & ListEditorOptions.ListLabel) != 0,
showListSize = (options & ListEditorOptions.ListSize) != 0;
if (showListLabel)
{
EditorGUILayout.PropertyField(list);
EditorGUI.indentLevel += 1;
}
if (!showListLabel || list.isExpanded)
{
SerializedProperty size = list.FindPropertyRelative("Array.size");
if (showListSize)
{
EditorGUILayout.PropertyField(size);
}
if (size.hasMultipleDifferentValues)
{
EditorGUILayout.HelpBox("Not showing lists with different sizes.", MessageType.Info);
}
else
{
ShowElements(list, options);
}
}
if (showListLabel)
{
EditorGUI.indentLevel -= 1;
}
}
private static void ShowElements(SerializedProperty list, ListEditorOptions options)
{
bool
showElementLabels = (options & ListEditorOptions.ElementLabels) != 0,
showButtons = (options & ListEditorOptions.Buttons) != 0;
for (int i = 0; i < list.arraySize; i++)
{
if (showButtons)
{
EditorGUILayout.BeginHorizontal();
}
if (showElementLabels)
{
EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i));
}
else
{
EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i), GUIContent.none);
}
if (showButtons)
{
ShowButtons(list, i);
EditorGUILayout.EndHorizontal();
}
}
if (showButtons && list.arraySize == 0 && GUILayout.Button(addButtonContent, EditorStyles.miniButton))
{
list.arraySize += 1;
}
}
private static void ShowButtons(SerializedProperty list, int index)
{
if (GUILayout.Button(moveButtonContent, EditorStyles.miniButtonLeft, miniButtonWidth))
{
list.MoveArrayElement(index, index + 1);
}
if (GUILayout.Button(duplicateButtonContent, EditorStyles.miniButtonMid, miniButtonWidth))
{
list.InsertArrayElementAtIndex(index);
}
if (GUILayout.Button(deleteButtonContent, EditorStyles.miniButtonRight, miniButtonWidth))
{
int oldSize = list.arraySize;
list.DeleteArrayElementAtIndex(index);
if (list.arraySize == oldSize)
{
list.DeleteArrayElementAtIndex(index);
}
}
}
}
And this is where I'm calling it from:
public override void OnInspectorGUI()
{
soTarget.Update();
EditorGUI.BeginChangeCheck();
ListEditor.Show(soTarget.FindProperty("possibleGenerators"));
if (EditorGUI.EndChangeCheck() || GUI.changed)
{
Undo.RecordObject(myTarget, "Save Cavern Parameters");
EditorUtility.SetDirty(myTarget);
soTarget.ApplyModifiedProperties();
AssetDatabase.SaveAssets();
}
EditorGUI.EndChangeCheck();
}
I am having this issue as well. In 2019 it was fine, but I imported into newer versions and it draws the same way as yours now without any code changes. Did you ever find out anything about it?
It seems to be anything within a top-level foldout. Not the top level itself. Also not sub-foldouts
Your answer
Follow this Question
Related Questions
Webview in custom Unity Editor Window? 0 Answers
EditorGUILayout.Popup with an array of GameObject? 0 Answers
Using CustomEditor caused error which was CS0246. 0 Answers
Making a custom editor window much like the Animator window? 0 Answers
GUI / Inspector Help,CustomEditor with A callback Function 0 Answers