Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by andrew-lukasik · Oct 04, 2021 at 06:22 PM · serializationmultidimensional array

How do you work with multi-dimentional arrays


How do you work with multi-dimentional arrays? Any best practices you can share?


I ask this question because it is rather common dilemma among unity programmers and there is plenty of confusion around this topic. The whole issue starts when one day you write code like this...

 [SerializeField] int[] arrayA;
 [SerializeField] int[,] arrayB;
 [SerializeField] int[][] arrayC;

...and you notice that arrayA will serialize fine, but arrayB and arrayC wont (wont show up in Inspector). So how you deal with that?


Tragicomical anecdote: UE4 has this problem as well. TArray<TArray<int32>> wont serialize nor show up in Details panel. Made me chuckle, won't lie : )


Comment
Add comment · Show 1
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 Eno-Khaon · Oct 05, 2021 at 03:28 AM 1
Share

I'm not certain how far this will be able to take you in terms of multi-dimensional arrays, but this Github ArrayDrawer project might be a viable starting point for experimenting.

Comparing a few different resources, it looks like that uses a similar (if not the same) baseline implementation for arrays that Unity does by default. It would be interesting to see if parts of it can be expanded on (like, as an alternative, divergent array support) to provide support for multi-dimensional arrays.

The part I'm least sure about, however, if it's it something that *can* reasonably be expanded on as-is, since I don't know enough about how data is read from SerializedProperties.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by andrew-lukasik · Oct 04, 2021 at 07:18 PM

My approach is to keep your data as basic and serializable 1-dimentional array, but create some kind of facade object that provides n-dimensional indexer accessors ( [ x , y , ... ]) so the the index conversion issue disappears almost completely.


Implementation example: https://gist.github.com/andrew-raphael-lukasik/b1cba7b10407c7540e33f1c0a9930746


Comment
Add comment · 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
1

Answer by zORg_alex · Oct 09, 2021 at 10:13 AM

I've thrown this together when ran into this problem. You almost can use it as an array and convert to it. Not so invisible as I hoped though. You can implicitly cast ArrayElement and T[] back and forth unless you want to cast array to ArrayElements[], then you're out of luck it seems, only an extension method can do that, no casting to array of type is available in C#.

     //Example of use
     int[][] zzz = new int[][] { new int[]{ 1, 2, 3 }, new int[]{ 4, 5, 6 } };
     ArrayElement<int[]> xxx = zzz;
     ArrayElement<int>[] ccc = zzz.ToArrayElemets();

Here's the implementation:

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 
 namespace MultidimensionalArrayUtils {
     [Serializable]
     public class ArrayElement<T> : IEnumerable, IEnumerable<T> {
         [SerializeField]
         private T[] _array;
         public T[] Array { get => _array; }
         public T this[int i] => _array[i];
 
         public IEnumerator GetEnumerator() => _array.GetEnumerator();
 
         IEnumerator<T> IEnumerable<T>.GetEnumerator() {
             foreach (var item in _array) {
                 yield return item;
             }
         }
 
         public ArrayElement(T[] array) {
             _array = array;
         }
         public ArrayElement(IEnumerable<T> collection) {
             _array = collection.ToArray();
         }
 
         public static implicit operator T[](ArrayElement<T> arr) => arr == null ? new T[0] : arr.Array;
         public static implicit operator ArrayElement<T>(T[] arr) => new ArrayElement<T>(arr == null ? new T[0] : arr);
     }
     public static class ArrayElementExtensions {
     public static  ArrayElement<T>[] ToArrayElemets<T>(this IEnumerable<IEnumerable<T>> collectionOfCollections) => 
         collectionOfCollections.Select(t => (ArrayElement<T>)t.ToArray()).ToArray();
     public static ArrayElement<T>[] ToArrayElemets<T>(this IEnumerable<T[]> collectionOfCollections) =>
         collectionOfCollections.Select(t => (ArrayElement<T>)t).ToArray();
     }
 }
 
Comment
Add comment · 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

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

134 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make custom 2 dimensional enum array class 1 Answer

Best way to match up 3 bits of data 2 Answers

Serialize custom multidimensional array from Inspector 3 Answers

Unity - Serializing a single property or UnityEngine.Object reference 0 Answers

When attempting to serialize and save a jagged array it gives me an error message 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