- Home /
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 :)
you can create a custom type with multiple values and have an array of that ins$$anonymous$$d of the float variable. and then you create another class, a CustomInspector for that type which defines how that type looks.
if that's what you're looking for
I did some research about the things you said and found this:
http://answers.unity3d.com/questions/456515/create-a-custom-variable-type-in-c.html
I managed to do what I wanted, thanks!
Answer by hrgchris · Dec 04, 2016 at 08:28 AM
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: http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/
There are many more, and if that doesn't help at all, my apologies!
Chris
Hey! Ideally I want something like this: where you have some sort of array with several elements, index 0,1,2,etc and for every index, you have several input fields as shown in the picture, and then I want to access the variables of every element in the array.
Answer by Bunny83 · Dec 02, 2016 at 10:58 PM
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.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to Create a list with initial capacity 5 Answers
Inventory System wont work 2 Answers
how to assign mesh.vertices ToList linq extension 1 Answer
How to properly create a 2 dimensional array of an object. [C#] 1 Answer