• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by revraptor · Dec 09, 2020 at 02:33 PM · editor-scriptingeditorguicustom-inspectorcustomeditor

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: alt text

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();
         }


untitled.png (23.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

224 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom inspector Vs. Custom window 2 Answers

Can't update ObjectField after object is selected 1 Answer

Trying to add EditorGUILabels and am getting a couple errors. 0 Answers

OnPreviewGUI for Canvas and UI Elements 0 Answers

How to Instantiate Selected GameObject From Hierarchy in Editor mode? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges