Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexanderameye · Dec 02, 2016 at 08:35 PM · arraylistcustom-inspectoreditorguilayout

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.

alt text

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 :)

screenshot-12.png (4.6 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Dec 02, 2016 at 10:32 PM 1
Share

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

avatar image alexanderameye · Dec 02, 2016 at 10:41 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexanderameye · Dec 04, 2016 at 01:55 PM 0
Share

Hey! Ideally I want something like this:alt text 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.

8629-custom-editor.jpg (134.2 kB)
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexanderameye · Dec 04, 2016 at 01:53 PM 0
Share

I'll look into this, thanks!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges