[Unity] Declaring multidimensional arrays in Unity C#

2018. 8. 29. 11:23English translation


When developing with Unity, you may have to use multidimensional arrays.

Unity uses the C# language when coding some script, so you can use [, ] like a regular multidimensional array, but we can't use this way in the Inspector window.


This is typically a one-dimensional array that can be represented in Inspector window.


If you declare a variable with the same array as above,

One-dimensional array declaration is possible in the Inspector window like above picture.


But if you need declare a multidimensional array like this picture,

You need another declaration, not increasing number of commas.


Just display the Inspector in class, not MonoBehaviour class.

In other words, you declare a GameObject in another script and call that variable in your main script.


The source code is shown below.

 [System.Serializable]  //Displaying non-MonoBehaviour classes in the Inspector.
public class MapArray
{
    public GameObject[] Map;
}

public class Run_World : MonoBehaviour {

    static public float Speed = 4;

    public GameObject[] StartPrefabs;
    public MapArray[] JunglePrefabs; //0 = Start, 5~10 = End

If you declare a one-dimensional array in other class and call it,


You can use the multidimensional array in the Inspector window.


반응형