Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom Editor Lists From Many Classes

Discussion in 'Scripting' started by DDeathlonger, Dec 19, 2017.

  1. DDeathlonger

    DDeathlonger

    Joined:
    May 9, 2014
    Posts:
    73
    How do I reference properties from more than one class, when the properties of the other classes are contained by references to other scripts held in the parent class's list?

    I have 5 scripts.. each one has its own list(s).. the first script begins by adding new instances of script 2 to its list, and each instance of script 2 adds new instances of script 3 to script its list.. etc.

    The custom editor currently is able to rename the first layer.. the label in the inspector, for each instance of script 2 that it has in its list.

    I need to be able to rename each instance of script 3 that is held by each instance of script 2's personal list.

    The editor I have set up right now is here:

    Code (CSharp):
    1.     [UnityEditor.CustomEditor(typeof(ParentClass/Class1))]
    2.     [UnityEditor.CanEditMultipleObjects]
    3.     public class CustomEditor : UnityEditor.Editor
    4.     {
    5.         UnityEditor.SerializedProperty ParentsList;
    6.         UnityEditor.SerializedProperty ChildsList;
    7.  
    8.         private void OnEnable()
    9.         {
    10.             ParentsList = serializedObject.FindProperty("ListInParent");
    11.         }
    12.  
    13.         public override void OnInspectorGUI()
    14.         {
    15.             serializedObject.Update();
    16.  
    17.             ShowCustomLists(ParentsList, "Parent");
    18.             //Be Able to put loops in loops until the List contained in Script5 is accessed and customized.
    19.             foreach (Child child in ParentList)
    20.             {
    21.                 print("Successful loop");
    22.                 ChildList = serializedObject.FindProperty("ListInChild");
    23.                 ShowCustomLists(ChildList, "Child");
    24.             }
    25.  
    26.             serializedObject.ApplyModifiedProperties();
    27.         }
    28.  
    29.         public void ShowCustomLists(UnityEditor.SerializedProperty list, string label)
    30.         {
    31.             UnityEditor.EditorGUI.indentLevel += 1;
    32.  
    33.             for (int i = 0; i < list.arraySize; i++)
    34.             {
    35.                 UnityEditor.EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i), new UnityEngine.GUIContent(label + "_" + (i + 1).ToString()), true);
    36.             }
    37.  
    38.             UnityEditor.EditorGUI.indentLevel -= 1;
    39.         }
    40.     }
    I've spent many hours now searching into this.. and I found workarounds for nested lists.. which this is not. Many tutorials on displaying custom lists in the inspector... which i have done so far... but not yet been able to find out how to access lists of particular instance of classes containing lists...

    The other thing I've tried is doing it all in the "OnInspectorGUI()" and also setting the child to the element at the index of the ParentList as such:

    Code (CSharp):
    1.     [UnityEditor.CustomEditor(typeof(ParentClass/Class1))]
    2.     [UnityEditor.CanEditMultipleObjects]
    3.     public class CustomEditor : UnityEditor.Editor
    4.     {
    5.         UnityEditor.SerializedProperty ParentList;
    6.  
    7.         UnityEditor.SerializedObject ParentScript;
    8.         UnityEditor.SerializedProperty ChildList;
    9.  
    10.  
    11.         private void OnEnable()
    12.         {
    13.             NodeList = serializedObject.FindProperty("ParentList");
    14.             Node = new UnityEditor.SerializedObject(targets); //not sure about the proper way of assigning.. but I'm working from here: https://answers.unity.com/questions/274002/using-serializedproperty-on-custom-classes.html
    15.         }
    16.  
    17.         public override void OnInspectorGUI()
    18.         {
    19.             serializedObject.Update();
    20.  
    21.             for (int i = 0; i < ParentList.arraySize; i++)
    22.             {
    23.                 UnityEditor.EditorGUILayout.PropertyField(ParentList.GetArrayElementAtIndex(i), new UnityEngine.GUIContent("Parent" + "_" + (i + 1).ToString()), true);
    24.                 ChildList = ParentList.GetArrayElementAtIndex(i);
    25.                 for (int j = 0; j < ChildList.arraySize; j++)
    26.                 {
    27.                     UnityEngine.MonoBehaviour.print("Successful Loop.");
    28.                     UnityEditor.EditorGUILayout.PropertyField(ChildList.GetArrayElementAtIndex(j), new UnityEngine.GUIContent("Child" + "_" + (j + 1).ToString()), true);
    29.                 }
    30.             }
    31.  
    32.             serializedObject.ApplyModifiedProperties();
    33.         }
    34.     }
    But this does not work either, I just get the error:
    Retrieving array size but no array was provided
    UnityEditor.SerializedProperty:get_arraySize()

    Ultimately, I need to find a way to pull a reference to the (instance of "ChildScript") element of the list array as a "ChildScript" and then access "ChildScript's" list(s), and finally edit the list(s) contained in that instance of the "ChildScript".
    Any help is immensely appreciated.. I think I'm just gonna sleep on this one.. :/
     
    Last edited: Dec 19, 2017
  2. DDeathlonger

    DDeathlonger

    Joined:
    May 9, 2014
    Posts:
    73
    BumpedeeBumpBump, still need some help plz.
     
  3. DDeathlonger

    DDeathlonger

    Joined:
    May 9, 2014
    Posts:
    73
    Helloooooooooo am I sooo alone all the sudden?? *cries*
     
    Davon_Allen_AofL likes this.