- Home /
Using TextAsset to create a grid.
Hello, I need to be pointed in the right direction with Unity's TextAsset component.
I will have a .txt file with 400 numbers, without spaces. ex: "010100111000101010" I want to have unity read each indivdual character, if it detects a "0" it will move up a value(position) and if it detects a "1", it will add a prefab (in this case a cube) before moving up a value. The end result will be a 20x20 grid of blocks in pre-determined positions.
I want to integrate it into this script somehow:
var cube : Transform;
function Start () {
for (var y = 0; y < 5; y++) {
for (var x = 0; x < 5; x++) {
var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity);
}
}
}
Can anyone help me out with this?
Answer by duck · Oct 26, 2010 at 04:01 PM
Place your text file into Unity's assets folder. Then make the script changes below, and drag a reference to the text asset into the "dataFile" variable below.
var dataFile : TextAsset; var cube : Transform;
function Start () { for (var y = 0; y < 5; y++) { for (var x = 0; x < 5; x++) { var charNum = (y*5)+x; if (dataFile.text[charNum] == "1") { var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity); } } } }
That should do it! (untested though... give me a shout in the comments if it doesn't work!)
Your answer