- Home /
TileMap inside a Tilemap
How do I make a TileMap inside another Tilemap the correct way? The main tile map has cubes which are 10x1x10 and some of the tiles have another 10x10 tile map inside them, but not 1 cube. The script I have right now, but it doesn't work as I want it to:
public class TileMapGenerator : MonoBehaviour {
public static TileMapGenerator instance;
public GameObject[,][,] tiles;
public GameObject[,] fields;
public GameObject fieldPrefab;
public GameObject tilePrefab;
public int worldMapSize;
public int tileMapSize;
public int fieldSize;
public int tileSize;
public int centerPos = 5;
public Material[] materials;
void Awake() {
if (instance == null) {
instance = this;
}
GenerateTileMap ();
}
private void GenerateTileMap() {
fields = new GameObject[worldMapSize, worldMapSize];
tiles = new GameObject[worldMapSize, worldMapSize][,];
for (int x = 0; x < worldMapSize; x++) {
for (int y = 0; y < worldMapSize; y++) {
fields [x, y] = new GameObject ("Field");
fields [x, y].transform.localPosition = new Vector3 (x * fieldSize + fieldSize/2, 0f, y * fieldSize + fieldSize/2);
fields [x, y].transform.SetParent (transform);
if (x == 5 && y == 5) {
GenerateTiles (x, y);
}
GameObject newField = BuildTile (10f, "Tile");
//newField.transform.SetParent (fields[x, y].transform);
newField.transform.position = new Vector3(x * fieldSize, 0f, y * fieldSize);
newField.transform.SetParent (fields[x, y].transform);
}
}
}
private GameObject BuildTile(float size, string name) {
GameObject newTile = GameObject.CreatePrimitive (PrimitiveType.Cube);
newTile.name = name;
newTile.transform.localScale = new Vector3 (size, 1f, size);
MeshRenderer rend = newTile.GetComponent<MeshRenderer> ();
return newTile;
}
private void GenerateTiles(int k, int j) {
tiles[k, j] = new GameObject[tileMapSize, tileMapSize];
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
tiles [k, j][x, y] = BuildTile (1f, "Tile");
tiles [k, j] [x, y].name += "_" + x + y;
float posX = x - fieldSize / 2f;
float posY = y - fieldSize / 2f;
Vector3 pos = new Vector3 (posX, 0f, posY);
tiles [k, j][x, y].transform.position = pos;
tiles [k, j] [x, y].transform.SetParent (fields[k, j].transform);
}
}
}
}
Comment
Could you show us what you mean with tilemap in a tilemap with like paintings or something or some way to illustrate to us what exactly you mean?
Your answer
Follow this Question
Related Questions
Weird blue lines in scene and game view. 0 Answers
Getting cell position of a Tile from a reference to that Tile at runtime 0 Answers
Ugly line in Tilemap 0 Answers
How to check tile flag in IF statement? 0 Answers