- Home /
Store data for tile based system
Hello, I am making a tile based spaceship simulation game like FTL.
This model has been created by script.
(This is a flat map. Some tiles has different sizes or shapes).
Here is my Code; using System.Collections.Generic; using UnityEngine;
public class TestData : MonoBehaviour {
int[,] tile;
public int mapSizeX;
public int mapSizeY;
public List<TileType> tileTypes;
void Start()
{
tileTypes = GameObject.Find("Database").GetComponent<Database>().tileType;
tile = new int[mapSizeX, mapSizeY];
for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
tile[x, y] = 0; //Fill the map with non-prefab tile(This is for another thing, will be not generated)
}
}
GenerateTileData();
GenerateTiles(mapSizeX,mapSizeY);
}
void GenerateTileData()
{
int x, y;
//Room_1
for (x = 3; x <= 6; x++)
{
for (y = 9; y <= 14; y++)
{
tile[x, y] = 7;
}
}
tile[18, 10] = 3;
tile[19, 10] = 3;
tile[18, 13] = 3;
tile[19, 13] = 3;
tile[15, 4] = 3;
tile[15, 19] = 3;
//Room_2
for (x = 8; x <= 10; x++)
{
for (y = 9; y <= 14; y++)
{
tile[x, y] = 8;
}
}
//....so much more like this
}
void GenerateTiles(int sizeX, int sizeY)
{
for (int x = 0; x < sizeX; x++)
{
for (int y = 0; y < sizeY; y++)
{
if (tile[x, y] != 0)
{
GameObject go = Instantiate(tileTypes[tile[x, y]].tileObj, transform);
go.transform.localPosition = new Vector3(x, y, 0);
TileData td = go.GetComponent<TileData>();
td.testmapper = this;
}
}
}
}
}
I want to make different ships. More ships means need thousands of "tile[x,y] = a"
I want to store this data in somewhere and script should access when creating ship models. I tried jsonMapper for this but i could not Anybody have an idea for this ? Sorry for my english if i wrote something wrong.
Answer by MaxGuernseyIII · Aug 31, 2017 at 06:38 PM
It sounds like what your want is a tile editor. There's a lot to be said for just buying a tile editor asset, I think.
However, if you really want to do it yourself, you could just use plain text. Basically, just map each character you might use to one of your tile codes. Iterate through all the lines in the file, using the line number as the y coordinate, then iterate through each character using the one as the coordinate.
For instance:
var lines = File.ReadAllLines(pathToYourTextFile);
for (var y = 0; y < lines.length; y++)
{
var currentLine = lines[y];
for (var x = 0; x < currentLine.Length; x++)
{
map[x, y] = TranslateCharToTileCode(currentLine[x]);
}
}
Where pathToYourTextFile is the path to your text file and TranslateCharToTileCode is a method that takes a char, returns an int, and maps between symbolic characters and tile codes using something like a switch statement.
If this isn't exact, my apologies, I'm writing it in my phone.
Your answer
Follow this Question
Related Questions
Finding the location of the tile in a tilemap nearest to the player 0 Answers
Get the tilemap that the tile is sitting on - Scriptable Tiles (runtime and editor) 1 Answer
Auto-tiling - adding tiles which are nearby the added one 0 Answers
Help with scriptable tiles PLEASE HELP URGENT!!! 1 Answer
Efficient way of placing multi-tile sprites onto tilemap via script 0 Answers