Multiple variables in one array element + access them

Hey

I have a script where I declare an array:

public float[] angles;

Then I have an editorscript linked to the first script where I have this:

serializedObject.Update();
EditorList.Show(serializedObject.FindProperty("angles"), EditorListOption.ListLabel | EditorListOption.Buttons);
serializedObject.ApplyModifiedProperties();

And then I have another script:

using UnityEditor;
using UnityEngine;
using System;

[Flags]
public enum EditorListOption {
	None = 0,
	ListSize = 1,
	ListLabel = 2,
	ElementLabels = 4,
	Buttons = 8,
	Default = ListSize | ListLabel | ElementLabels,
	NoElementLabels = ListSize | ListLabel,
	All = Default | Buttons
}

public static class EditorList {

	private static GUIContent
		MoveRotationBlockUp = new GUIContent("\u25B2", "Move Up"),
		MoveRotationBlockDown = new GUIContent("\u25BC", "Move Down"),
		duplicateButtonContent = new GUIContent("+", "Add Rotation Block"),
		deleteButtonContent = new GUIContent("-", "Delete"),
		AddRotationBlocks = new GUIContent("Add Rotation Blocks", "Add Rotation Blocks"),
		RemoveAllBlocks = new GUIContent("Remove All Blocks", "Remove All Blocks");

	private static GUILayoutOption miniButtonWidth = GUILayout.Width(20f);

	public static void Show (SerializedProperty list, EditorListOption options = EditorListOption.Default)
	{

		bool
			showListLabel = (options & EditorListOption.ListLabel) != 0,
			showListSize = (options & EditorListOption.ListSize) != 0;

		if(showListLabel)
		{
			EditorGUILayout.PropertyField(list);
			EditorGUI.indentLevel++; //Indentlevel of the blocks themselves
		}

		if (!showListLabel || list.isExpanded)
		{
			SerializedProperty size = list.FindPropertyRelative("Array.size");

			if(showListSize)
			{
			EditorGUILayout.PropertyField(size);
			}

			if (size.hasMultipleDifferentValues)
			{
				EditorGUILayout.HelpBox("Not showing lists with different sizes.", MessageType.Info);
			}

			else ShowElements(list, options);

		}

		if (showListLabel) EditorGUI.indentLevel -= 1;
	}

	private static void ShowElements (SerializedProperty list, EditorListOption options)
	{
		bool
			showElementLabels = (options & EditorListOption.ElementLabels) != 0,
			showButtons = (options & EditorListOption.Buttons) != 0;

		for (int i = 0; i < list.arraySize; i++) {
			if (showButtons) {
				EditorGUILayout.BeginHorizontal();
			}
			if (showElementLabels) {
				EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i));
			}
			else {
				EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i), GUIContent.none);
			}
			if (showButtons) {
				ShowButtons(list, i);
				EditorGUILayout.EndHorizontal();
			}
		}

		//IF
		GUI.color = Color.green;
		if (showButtons && list.arraySize == 0 && GUILayout.Button(AddRotationBlocks))
		{
			list.arraySize++;
		}

		GUI.color = Color.red;

	if(showButtons && list.arraySize != 0 && GUILayout.Button(RemoveAllBlocks))
		{
			list.arraySize = 0;
		}
	}

	//BUTTONS
	private static void ShowButtons (SerializedProperty list, int index)
	{
		//Move block up
		if (GUILayout.Button(MoveRotationBlockUp, EditorStyles.miniButtonLeft, miniButtonWidth)) list.MoveArrayElement(index, index - 1);

		//Move block down
		if (GUILayout.Button(MoveRotationBlockDown, EditorStyles.miniButtonLeft, miniButtonWidth)) list.MoveArrayElement(index, index + 1);

		//Add Block
		if (GUILayout.Button(duplicateButtonContent, EditorStyles.miniButtonMid, miniButtonWidth)) list.InsertArrayElementAtIndex(index);

		//RemoveBlock
		if (GUILayout.Button(deleteButtonContent, EditorStyles.miniButtonRight, miniButtonWidth))
		{
			int oldSize = list.arraySize;
			list.DeleteArrayElementAtIndex(index);
			if (list.arraySize == oldSize) list.DeleteArrayElementAtIndex(index);
		}
	}

}

Now if I add the first script where I declare the array to a gameobject, I am able to fill in several floats.

Now my question is, how do I make it so that instead of having the ability to fill in one variable in each element of the list/array, I can declare multiple variables I have to fill in for each element of the list.

So for example, when I click the little plus icon and a new array element appears, I have 2 boxes where I can put variables in.

Any help would be truly great :slight_smile:

Hi there

I’ll confess I don’t entirely understand the question. However it seems you might just be asking how you create an inspector with a more dynamic way of modifying / adding deleting elements. This has been going around for a while, and it’s a pain but can be achieved using the unity internal reorderable list.

This site gives an example: Unity: make your lists functional with ReorderableList

There are many more, and if that doesn’t help at all, my apologies!

  • Chris

It’s really not clear what you mean. Your array is a one dimensional array of float. So it’s simply a list of floats values. You already have your + button and it already adds a new element to the array. It sounds like you want something like a “two dimensional array” or a jagged array but those can’t be serialized by Unity. A common workaround if something is needed is to use an intermediate data class for that:

[System.Serializable]
public class SubArray
{
    public float[] values;
}

// in your MonoBehaviour you would declare an array of your SubArray class:

public SubArray[] angles;

Now each element of the “angles” array can have as many float values as you want. Though you might want to look at implementing a PropertyDrawer for that SubArray class, that would simplify everything. So you would still be able to draw the angles array just as you have it right now and display the sub values horizontally. Though drawing them horizontally limits you in the number of sub values you can have as you simply would run out of space.

That’s why Unity displays such sub arrays as actual sub items below the element.