Search Unity

[CustomEditor] Displaying an array in a struct list like the default inspector

Discussion in 'Scripting' started by HeavyMetalSnail, Jun 12, 2018.

  1. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    I am trying to display an array in a custom inspector the way the default inspector displays it.
    That array is contained in a serialized struct, which i have a list of.

    Here is where i am so far :


    Test script :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Test : MonoBehaviour {
    6.  
    7.     public List<TheStruct> theStructList = new List<TheStruct>();
    8.  
    9.     [System.Serializable]
    10.     public struct TheStruct
    11.     {      
    12.         public float[] theFloatArray;      
    13.     }
    14. }
    CustomEditor script :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(Test))]
    7. public class TestEditor : Editor {
    8.  
    9.     Test test;
    10.  
    11.     public override void OnInspectorGUI()
    12.     {
    13.         serializedObject.Update();
    14.  
    15.         foreach (Test.TheStruct myStruct in test.theStructList)
    16.         {
    17.             SerializedObject so = new SerializedObject((Object) myStruct);
    18.  
    19.             SerializedProperty sp = so.FindProperty("theFloatArray");
    20.  
    21.             EditorGUILayout.PropertyField(sp, true);
    22.         }
    23.      
    24.         serializedObject.ApplyModifiedProperties();
    25.     }
    26.  
    27.     void OnEnable()
    28.     {
    29.         test = target as Test;      
    30.     }
    31. }
    The problem i have currently is that i can't access the properties of the struct element from the foreach loop because the struct element cannot be converted into an Object

    Is there any way to make this work ?

    Thanks
     
  2. Held0fTheWelt

    Held0fTheWelt

    Joined:
    Sep 29, 2015
    Posts:
    173
    [SerializeField]
    public float[] theFloatArray;

    Looking for that to see the array ??
     
  3. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    It doesn't hurt to add it, but the issue is line 17 where i try to cast myStruct to an Object, it cannot be converted that way.

    But thank you anyway.
     
  4. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    So, small update on my issue, i managed to display the array, but any change i make is instantly reverted to the old value, any idea how to fix that ?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(Test))]
    7. public class TestEditor : Editor {
    8.  
    9.     Test test;
    10.  
    11.     public override void OnInspectorGUI()
    12.     {
    13.         serializedObject.Update();
    14.         base.DrawDefaultInspector();
    15.  
    16.         GUILayout.Label("----------------------Test----------------------");
    17.        
    18.         SerializedObject so = new SerializedObject(target);      
    19.         SerializedProperty listsp = so.FindProperty("theStructList");
    20.  
    21.         for (int i = 0; i < test.theStructList.Count; i++)
    22.         {
    23.             SerializedProperty arraysp = listsp.GetArrayElementAtIndex(i).FindPropertyRelative("theFloatArray");
    24.             EditorGUI.BeginChangeCheck();          
    25.  
    26.             EditorGUILayout.PropertyField(arraysp, true);
    27.          
    28.             if (EditorGUI.EndChangeCheck())
    29.             {              
    30.                 serializedObject.ApplyModifiedProperties();
    31.             }
    32.         }    
    33.     }
    34.  
    35.     void OnEnable()
    36.     {
    37.         test = target as Test;      
    38.     }
    39. }
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    That's because you're making a new SerializedObject, but applying the modified properties to the one from Editor.

    Code (csharp):
    1. SerializedObject so = new SerializedObject(target);  
    2. ...
    3.  
    4. if (EditorGUI.EndChangeCheck())
    5. {
    6.     serializedObject.ApplyModifiedProperties(); //this should be so, not serializedObject
    7. }
    In fact, you don't need to create so at all, serializedObject is the same thing.
     
  6. HeavyMetalSnail

    HeavyMetalSnail

    Joined:
    Jun 12, 2018
    Posts:
    13
    Thanks, it's working perfectly now