- Home /
Double Array (Custom Editor)
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
[HideInInspector]
public List<string> ThemesList = new List<string>();
-->Then in the custom Editor I display it as a Drop Down Menu
GUIContent themeList = new GUIContent("Theme Mode");
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
public Image[][] ThemeArray = new Image[5][];
' 5 ' being the maximum allowed number, it may change to any other
--> to display it, In custom Editor I use
Image[][] array = code.ThemeArray;
code.showImages = EditorGUILayout.Foldout(code.showImages, "Themes List");
if (code.showImages)
{
EditorGUI.indentLevel++;
code.ObjectSize = Mathf.Max(0, EditorGUILayout.IntField("Size", code.ObjectSize));
for (int i = 0; i < code.ObjectSize; i++)
{
array[code.SelecteedThemeIndex][i] = (Image)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex][i], typeof(Image), true);
}
EditorGUI.indentLevel--;
}
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
In Visual Studio (2019) there are no errors.
If you need the full code there it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class ThemesCoder : MonoBehaviour
{
[HideInInspector]
public List<string> ThemesList = new List<string>();
[HideInInspector]
public List<Image> Theme = new List<Image>();
[HideInInspector]
public int SelecteedThemeIndex = 0;
[HideInInspector]
public bool showList = false;
[HideInInspector]
public bool showImages = false;
[HideInInspector]
public Image[][] ThemeArray = new Image[5][];
[HideInInspector]
public int ObjectSize = 0;
[HideInInspector]
public int size = 0;
[CustomEditor(typeof(ThemesCoder))]
//[CanEditMultipleObjects]
public class ThemeCoderEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
DrawInspector();
}
public void DrawInspector()
{
ThemesCoder code = (ThemesCoder)target;
#region Themes Modes List
code.showList = EditorGUILayout.Foldout(code.showList, "Themes List");
if (code.showList)
{
EditorGUI.indentLevel++;
List<string> list = code.ThemesList;
code.size = Mathf.Max(0, EditorGUILayout.IntField("Size", list.Count));
while (code.size > list.Count)
{
list.Add(null);
}
while (code.size < list.Count)
{
list.RemoveAt(list.Count - 1);
}
for (int i = 0; i < list.Count; i++)
{
list[i] = EditorGUILayout.TextField("Theme " + i, list[i]);
}
EditorGUI.indentLevel--;
}
#endregion
#region Curent Mode
GUIContent themeList = new GUIContent("Theme Mode");
code.SelecteedThemeIndex = EditorGUILayout.Popup(themeList, code.SelecteedThemeIndex, code.ThemesList.ToArray());
#endregion
EditorGUILayout.Space();
#region Theme Game Object
Image[][] array = code.ThemeArray;
code.showImages = EditorGUILayout.Foldout(code.showImages, "Themes List");
if (code.showImages)
{
EditorGUI.indentLevel++;
code.ObjectSize = Mathf.Max(0, EditorGUILayout.IntField("Size", code.ObjectSize));
for (int i = 0; i < code.ObjectSize; i++)
{
array[code.SelecteedThemeIndex][i] = (Image)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex][i], typeof(Image), true);
}
EditorGUI.indentLevel--;
}
#endregion
}
}
}
Edit: I will use sprites, not Images as arrays, shall be no difference in the code.
I tried aproaching this problem with a struct:
[System.Serializable]
public struct SpriteArray
{
public Sprite[] Sprites;
}
Then declaring it like this
[HideInInspector]
public SpriteArray[] ThemeArray;
Using the next loop to show it
for (int i = 0; i < code.ObjectSize; i++)
{
array[code.SelecteedThemeIndex].Sprites[i] = (Sprite)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex].Sprites[i], typeof(Sprite), true);
}
Still I get no errors in VS, only when I change the ObjectSize to anything but 0 I get the same error as above.
Answer by denisbodrug05 · Apr 15, 2021 at 05:53 AM
Solved, Just when declaring the array make it to null. exmple code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
public class ThemesCoder : MonoBehaviour
{
[HideInInspector]
public SpriteArray[] ThemeArray = null;
[HideInInspector]
public List<string> ThemesList = new List<string>();
[HideInInspector]
public List<Image> Theme = new List<Image>();
[HideInInspector]
public int SelecteedThemeIndex = 0;
[HideInInspector]
public bool showList = false;
[HideInInspector]
public bool showImages = false;
[HideInInspector]
public int ObjectSize = 0;
[HideInInspector]
public int size = 0;
[CustomEditor(typeof(ThemesCoder))]
//[CanEditMultipleObjects]
public class ThemeCoderEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
DrawInspector();
}
public void DrawInspector()
{
ThemesCoder code = (ThemesCoder)target;
#region Themes Modes List
code.showList = EditorGUILayout.Foldout(code.showList, "Themes List");
if (code.showList)
{
EditorGUI.indentLevel++;
List<string> list = code.ThemesList;
code.size = Mathf.Max(0, EditorGUILayout.IntField("Size", list.Count));
while (code.size > list.Count)
{
list.Add(null);
}
while (code.size < list.Count)
{
list.RemoveAt(list.Count - 1);
}
for (int i = 0; i < list.Count; i++)
{
list[i] = EditorGUILayout.TextField("Theme " + i, list[i]);
}
EditorGUI.indentLevel--;
}
#endregion
#region Curent Mode
GUIContent themeList = new GUIContent("Theme Mode");
code.SelecteedThemeIndex = EditorGUILayout.Popup(themeList, code.SelecteedThemeIndex, code.ThemesList.ToArray());
#endregion
EditorGUILayout.Space();
#region Theme Game Object
SpriteArray[] array = code.ThemeArray;
code.showImages = EditorGUILayout.Foldout(code.showImages, "Themes List");
if (code.showImages)
{
EditorGUI.indentLevel++;
code.ObjectSize = Mathf.Max(0, EditorGUILayout.IntField("Size", code.ObjectSize));
for (int i = 0; i < code.ObjectSize; i++)
{
array[code.SelecteedThemeIndex].Sprites[i] = (Sprite)EditorGUILayout.ObjectField("Theme " + i, array[code.SelecteedThemeIndex].Sprites[i], typeof(Sprite), true);
}
EditorGUI.indentLevel--;
}
#endregion
}
}
}
[System.Serializable]
public struct SpriteArray
{
public Sprite[] Sprites;
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
new Mesh() works outside of array, but not in array. Huh? 3 Answers
What is wrong with my code? (Unity 2018, C#) 2 Answers
Working with Unity Editor 1 Answer