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.
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 :
using UnityEngine;
using System.IO;
using System.Linq;
using System.Collections.Generic;
public class MyScript : MonoBehaviour
{
public int[,] myarray;
List<string> fileLines = new List<string>();
private void Awake()
{
ReadFile();
for (int y = 0; y < fileLines.Count; y++)
{
for (int x = 0; x < fileLines[y].Length; x++)
{
myarray[y, x] = int.Parse(fileLines[y].Substring(x));
}
}
}
private void ReadFile()
{
using (StreamReader sr = new StreamReader("whatever.txt"))
{
fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
}
}
}
Obviously you’ll need to change “whatever.txt” to the path of your txt file.