Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Easy 2D Array Declaration

Discussion in 'Scripting' started by JosephMGS, Nov 23, 2019.

  1. JosephMGS

    JosephMGS

    Joined:
    Nov 5, 2019
    Posts:
    20
    Hi All,

    I have the below table on an Excel sheet and want to create 2D array. What I know so far is the below way of declaration. Is there any better practice in C#? Because the size might be larger in the future. Thanks.

    Code (CSharp):
    1.     public int[,] myarray = new int[9,9];
    2.  
    3.     private void Awake()
    4.     {
    5.         myarray[0, 0] = 1;
    6.         myarray[0, 1] = 9;
    7.         myarray[0, 2] = 2;
    8.         myarray[1, 0] = 8;
    9.         ...
    10.     }
    1 9 2 1 4 2 4 5 7
    8 9 8 6 1 5 8 5 3
    5 8 2 1 4 2 4 5 8
    8 6 2 8 1 9 4 6 6
    7 5 8 1 4 1 5 5 3
    7 2 2 6 7 3 1 2 5
    7 2 1 8 2 9 3 6 4
    5 2 1 2 2 5 6 2 8
    6 1 8 4 8 7 4 9 3
     
  2. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    If you could find a way to read from the Excel sheet (or convert the Excel sheet into a .txt file), you could loop over it and have it all done by code instead of manually inserting values into the array. Something like :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.IO;
    3. using System.Linq;
    4. using System.Collections.Generic;
    5.  
    6. public class MyScript : MonoBehaviour
    7. {
    8.     public int[,] myarray;
    9.  
    10.     List<string> fileLines = new List<string>();
    11.  
    12.     private void Awake()
    13.     {
    14.         ReadFile();
    15.  
    16.         for (int y = 0; y < fileLines.Count; y++)
    17.         {
    18.             for (int x = 0; x < fileLines[y].Length; x++)
    19.             {
    20.                 myarray[y, x] = int.Parse(fileLines[y].Substring(x));
    21.             }
    22.         }
    23.     }
    24.  
    25.     private void ReadFile()
    26.     {
    27.         using (StreamReader sr = new StreamReader("whatever.txt"))
    28.         {
    29.             fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
    30.         }
    31.     }
    32. }
    Obviously you'll need to change "whatever.txt" to the path of your txt file.

    Edit: fixed error.
     
    Last edited: Nov 23, 2019
    JosephMGS likes this.
  3. JosephMGS

    JosephMGS

    Joined:
    Nov 5, 2019
    Posts:
    20
    Seems very practical. Thanks a lot :)
     
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,135
    Code (CSharp):
    1. public int[,] myarray = new[,]
    2. {
    3.     { 1, 9, 2, 1, 4, 2, 4, 5, 7, },
    4.     { 8, 9, 8, 6, 1, 5, 8, 5, 3, },
    5.     { 5, 8, 2, 1, 4, 2, 4, 5, 8, },
    6.     ...
    7. };
     
    JosephMGS likes this.