Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Double Array (Custom Editor)

Discussion in 'Getting Started' started by Aliph-Null, Apr 13, 2021.

  1. Aliph-Null

    Aliph-Null

    Joined:
    Jan 9, 2020
    Posts:
    6
    Basicly, what I am trying to do, is to display a double array as one.
    Let me explain better.
    --> First I have a list of strings
    Code (CSharp):
    1. [HideInInspector]
    2. public List<string> ThemesList = new List<string>();
    -->Then in the custom Editor I display it as a Drop Down Menu
    Code (CSharp):
    1. GUIContent themeList = new GUIContent("Theme Mode");
    2. code.SelecteedThemeIndex = EditorGUILayout.Popup(themeList, code.SelecteedThemeIndex, code.ThemesList.ToArray());
    (code.SelecteedThemeIndex being my choice, (everything works just fine here))
    --> Now I have an array of Images
    Code (CSharp):
    1. public Image[][] ThemeArray = new Image[5][];
    ' 5 ' being the maximum allowed number.
    --> to display it, In custom Editor I use
    Code (CSharp):
    1. Image[][] array = code.ThemeArray;
    2.  
    3.             code.showImages = EditorGUILayout.Foldout(code.showImages, "Themes List");
    4.             if (code.showImages)
    5.             {
    6.                 EditorGUI.indentLevel++;
    7.                 code.ObjectSize = Mathf.Max(0, EditorGUILayout.IntField("Size", code.ObjectSize));
    8.  
    9.                 for (int i = 0; i < code.ObjectSize; i++)
    10.                 {
    11.                     array[code.SelecteedThemeIndex][i] = (Image)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex][i], typeof(Image), true);
    12.                 }
    13.  
    14.                 EditorGUI.indentLevel--;
    15.             }
    I am trying to show in inspector an array of images, based on the selected Index.
    (ex. on Index 0, there will be 3 images (img 1, img 2, img 3)
    on Index 1, there will be also 3 images (img 1, img 4, img 5)
    and I can switch the Index through the drop down menu, and the images(aka Arrays) will be swaped)

    Until now, if I set the ObjectSize to anything but ' 0 ' it gives me this error
    2021-04-13 10_09_58-.png

    In Visual Studio (2019) there are no errors.

    If you need the full code there it is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEngine.UI;
    6.  
    7. public class ThemesCoder : MonoBehaviour
    8. {
    9.     [HideInInspector]
    10.     public List<string> ThemesList = new List<string>();
    11.     [HideInInspector]
    12.     public List<Image> Theme = new List<Image>();
    13.     [HideInInspector]
    14.     public int SelecteedThemeIndex = 0;
    15.     [HideInInspector]
    16.     public bool showList = false;
    17.     [HideInInspector]
    18.     public bool showImages = false;
    19.     [HideInInspector]
    20.     public Image[][] ThemeArray = new Image[5][];
    21.     [HideInInspector]
    22.     public int ObjectSize = 0;
    23.     [HideInInspector]
    24.     public int size = 0;
    25.  
    26.     [CustomEditor(typeof(ThemesCoder))]
    27.     //[CanEditMultipleObjects]
    28.     public class ThemeCoderEditor : Editor
    29.     {
    30.         public override void OnInspectorGUI()
    31.         {
    32.             base.OnInspectorGUI();
    33.  
    34.             DrawInspector();
    35.         }
    36.  
    37.         public void DrawInspector()
    38.         {
    39.             ThemesCoder code = (ThemesCoder)target;
    40.  
    41.             #region Themes Modes List
    42.          
    43.            
    44.             code.showList = EditorGUILayout.Foldout(code.showList, "Themes List");
    45.             if (code.showList)
    46.             {
    47.                 EditorGUI.indentLevel++;
    48.                 List<string> list = code.ThemesList;
    49.                 code.size = Mathf.Max(0, EditorGUILayout.IntField("Size", list.Count));
    50.  
    51.                 while (code.size > list.Count)
    52.                 {
    53.                     list.Add(null);
    54.                 }
    55.  
    56.                 while (code.size < list.Count)
    57.                 {
    58.                     list.RemoveAt(list.Count - 1);
    59.                 }
    60.  
    61.                 for (int i = 0; i < list.Count; i++)
    62.                 {
    63.                     list[i] = EditorGUILayout.TextField("Theme " + i, list[i]);
    64.                 }
    65.                 EditorGUI.indentLevel--;
    66.             }
    67.             #endregion
    68.  
    69.             #region Curent Mode
    70.             GUIContent themeList = new GUIContent("Theme Mode");
    71.             code.SelecteedThemeIndex = EditorGUILayout.Popup(themeList, code.SelecteedThemeIndex, code.ThemesList.ToArray());
    72.             #endregion
    73.  
    74.             EditorGUILayout.Space();
    75.  
    76.             #region Theme Game Object
    77.            
    78.             Image[][] array = code.ThemeArray;
    79.  
    80.             code.showImages = EditorGUILayout.Foldout(code.showImages, "Themes List");
    81.             if (code.showImages)
    82.             {
    83.                 EditorGUI.indentLevel++;
    84.                 code.ObjectSize = Mathf.Max(0, EditorGUILayout.IntField("Size", code.ObjectSize));
    85.  
    86.                 for (int i = 0; i < code.ObjectSize; i++)
    87.                 {
    88.                     array[code.SelecteedThemeIndex][i] = (Image)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex][i], typeof(Image), true);
    89.                 }
    90.  
    91.                 EditorGUI.indentLevel--;
    92.             }
    93.             #endregion
    94.         }
    95.     }
    96. }
    Hope you can help me.
    Thank You anticiped.
     
  2. Aliph-Null

    Aliph-Null

    Joined:
    Jan 9, 2020
    Posts:
    6
    So I resolved this problem, looks like when declaring the Array you must make it to null as default.
    The code as for an example is here
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEngine.UI;
    6.  
    7. public class ThemesCoder : MonoBehaviour
    8. {
    9.     [HideInInspector]
    10.     public SpriteArray[] ThemeArray = null;
    11.     [HideInInspector]
    12.     public List<string> ThemesList = new List<string>();
    13.     [HideInInspector]
    14.     public List<Image> Theme = new List<Image>();
    15.     [HideInInspector]
    16.     public int SelecteedThemeIndex = 0;
    17.     [HideInInspector]
    18.     public bool showList = false;
    19.     [HideInInspector]
    20.     public bool showImages = false;
    21.    
    22.     [HideInInspector]
    23.     public int ObjectSize = 0;
    24.     [HideInInspector]
    25.     public int size = 0;
    26.  
    27.     [CustomEditor(typeof(ThemesCoder))]
    28.     //[CanEditMultipleObjects]
    29.     public class ThemeCoderEditor : Editor
    30.     {
    31.         public override void OnInspectorGUI()
    32.         {
    33.             base.OnInspectorGUI();
    34.  
    35.             DrawInspector();
    36.         }
    37.  
    38.         public void DrawInspector()
    39.         {
    40.             ThemesCoder code = (ThemesCoder)target;
    41.  
    42.             #region Themes Modes List
    43.          
    44.            
    45.             code.showList = EditorGUILayout.Foldout(code.showList, "Themes List");
    46.             if (code.showList)
    47.             {
    48.                 EditorGUI.indentLevel++;
    49.                 List<string> list = code.ThemesList;
    50.                 code.size = Mathf.Max(0, EditorGUILayout.IntField("Size", list.Count));
    51.  
    52.                 while (code.size > list.Count)
    53.                 {
    54.                     list.Add(null);
    55.                 }
    56.  
    57.                 while (code.size < list.Count)
    58.                 {
    59.                     list.RemoveAt(list.Count - 1);
    60.                 }
    61.  
    62.                 for (int i = 0; i < list.Count; i++)
    63.                 {
    64.                     list[i] = EditorGUILayout.TextField("Theme " + i, list[i]);
    65.                 }
    66.                 EditorGUI.indentLevel--;
    67.             }
    68.             #endregion
    69.  
    70.             #region Curent Mode
    71.             GUIContent themeList = new GUIContent("Theme Mode");
    72.             code.SelecteedThemeIndex = EditorGUILayout.Popup(themeList, code.SelecteedThemeIndex, code.ThemesList.ToArray());
    73.             #endregion
    74.  
    75.             EditorGUILayout.Space();
    76.  
    77.             #region Theme Game Object
    78.            
    79.             SpriteArray[] array = code.ThemeArray;
    80.  
    81.             code.showImages = EditorGUILayout.Foldout(code.showImages, "Themes List");
    82.             if (code.showImages)
    83.             {
    84.                 EditorGUI.indentLevel++;
    85.                 code.ObjectSize = Mathf.Max(0, EditorGUILayout.IntField("Size", code.ObjectSize));
    86.  
    87.                 for (int i = 0; i < code.ObjectSize; i++)
    88.                 {
    89.                    
    90.                     array[code.SelecteedThemeIndex].Sprites[i] = (Sprite)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex].Sprites[i], typeof(Sprite), true);
    91.  
    92.                 }
    93.                 EditorGUI.indentLevel--;
    94.             }
    95.             #endregion
    96.         }
    97.     }
    98. }
    99.  
    100. [System.Serializable]
    101. public struct SpriteArray
    102. {
    103.     public Sprite[] Sprites;
    104. }