Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Can't edit list elements in CustomEditor or CustomWindow

Discussion in 'Editor & General Support' started by RA_Prion_exe, May 24, 2022.

  1. RA_Prion_exe

    RA_Prion_exe

    Joined:
    Aug 2, 2021
    Posts:
    5
    Been trying to create a Custom Editor for a scriptable object with lists. The new Unity system uses reorderable lists that, for some reason, don't allow to modify the list's element values in a custom editor.
    1. Can add a specific item to the list but by dragging it to the list name
    2. adding the [NoReorderable] Property to the list fix the issue but, well, you lose the reorderable property
    I've tried to make my own Reorderable List but had the same issue. Any clues? documentation is still lacking.

    The class, you can try changing Inheritance to Sxriptable Object and using the commented code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(menuName = "Sample/List Scriptable")]
    6. [SerializeField]
    7. public class MyScriptableList : MonoBehaviour
    8. {
    9.     public List<GameObject> objects;
    10. }
    11.  
    The Editor

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6.  
    7. [CustomEditor(typeof(MyScriptableList))]
    8. public class MyEditor : Editor
    9. {
    10.     private void OnEnable()
    11.     {
    12.        
    13.     }
    14.  
    15.     public override void OnInspectorGUI()
    16.     {
    17.         /*
    18.          // Can't change value of elements neither in Monobehaviour or ScriptableObjects
    19.          // Can add element by draging it to the list title in Monobehaviour but not in ScriptableObject
    20.         var e = CreateEditor(target);
    21.         e.DrawDefaultInspector();
    22.         */
    23.  
    24.         // Cant' interact with ScriptableObject
    25.         //Works fine in Monobehaviour
    26.         base.OnInspectorGUI();
    27.  
    28.  
    29.         GUILayout.Label("Drawing MyEditor");
    30.     }
    31. }
    32.  
    The Window

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class MyWindow : EditorWindow
    7. {
    8.     static MyScriptableList scriptable;
    9.  
    10.     [MenuItem("Window/MyWindow")]
    11.     public static void ShowWindow()
    12.     {
    13.         var window = EditorWindow.GetWindow<MyWindow>();
    14.         //scriptable = ScriptableObject.CreateInstance<MyScriptableList>(); //-> for Scritpable
    15.  
    16.        
    17.         //for Monobehaviour
    18.         var g = new GameObject();
    19.         g.AddComponent<MyScriptableList>();
    20.         scriptable = g.GetComponent<MyScriptableList>();
    21.        
    22.     }
    23.  
    24.     private void OnGUI()
    25.     {
    26.         var e = Editor.CreateEditor(scriptable);
    27.  
    28.  
    29.         // Can't change value of elements neither in Monobehaviour or ScriptableObjects
    30.         // Can add element by draging it to the list title in Monobehaviour and in ScriptableObject
    31.         e.OnInspectorGUI();
    32.  
    33.  
    34.         /*
    35.         // Can't change value of elements neither in Monobehaviour or ScriptableObjects
    36.         // Can add element by draging it to the list title in Monobehaviour and in ScriptableObject
    37.         e.DrawDefaultInspector();
    38.         */
    39.     }
    40. }
    41.  
     

    Attached Files:

  2. Fraktalia

    Fraktalia

    Joined:
    Apr 2, 2015
    Posts:
    16
    I also experience the same issue with an editor tool that allows merging materials into a texture array container for multi-material blending. It also is an editor window where the default inspector is drawn for a scriptable object containing lists.

    EDIT: Adding [NonReorderable] to the arrays and lists also fixes them but they lose the reordering feature. I think the scriptable objects are preventing the reordering as it was added with version 2021
     
    Last edited: Jun 28, 2022
  3. rainwizerd

    rainwizerd

    Joined:
    Feb 19, 2020
    Posts:
    7
    holy S*** thank you. this fixed a lot of broken inspectors for our project after upgrading to 2021