generating a 4x5 tile map using unity c#

Im trying to create a 4x5 tile map using a text.txt file but i cant figure it out i have tried everything and legit nothing has worked. i found many examples but they dont explain how to do it its more just a solution . i really need help with this if somone can give me some information . maybe even a sample script just so i can fully understand

what i want to do is have a txt file with

11111

32134

11132

21413

something like that and using that it creates the prefab assigned to that number and they are right next to each square.
please help

well the first thing to do here is to read that file in so that its easily readable.
First thing I would suggest is splitting each number with a comma or other delimiter character (for example i often see the pipe | used in place of comas for delimeters).
So your file would look like this.

1,1,1,1,1
3,2,1,3,4
1,1,1,3,2
2,1,4,1,3

You could then read this into a string using System.IO.File.ReadAllLines() so now you have each line of the file as a string.
You can then iterate over these strings and with each one do the following.

  • Split the string with your delimeter (in this case ‘,’) using string.split()
  • Iterate over that resulting array.
  • For each item in THAT array, parse the value to an int (if necessary) and use it to load the appropriate prefab.

If your text file is an asset in the build then you can even connect it to your script in the inspector as a TextAsset and use TextAsset.text to read it in (if you do this, you will need to split with the ’
’ character which should give you the same result as reading each line with the ReadAllLines method.

Hope this helps a bit.