- Home /
Map generation. How to name my tiles based on their position
I am trying to make a grid system and I finally found a grid generator using tiles. But, my next step is to make a system that creates allows me to create boundaries between the different "counties". I thought maybe I could define each individual tile and then define the tile to wich county it belonged to (for starters neutral, red blue, green, yellow). Since I am a noob at C# I need help integrating this into what I believe is the correct place to put it in.
So far my code is as fallows
using UnityEngine;
using System.Collections;
public class mapGenerator : MonoBehaviour {
public Transform tilePrefab;
public Vector2 mapSize;
[Range(0,1)]
public float outlinePercent;
void Start() {
genMap ();
}
public void genMap(){
string holderName = "Generated map";
if (transform.FindChild (holderName)) {
DestroyImmediate (transform.FindChild (holderName).gameObject);
}
Transform mapHolder = new GameObject (holderName).transform;
mapHolder.parent = transform;
for (int x = 0; x < mapSize.x; x++) {
for (int y = 0; y < mapSize.y; y++) {
Vector3 tilePos = new Vector3 (-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y/2 + 0.5f + y);
Transform newTile = Instantiate (tilePrefab, tilePos, Quaternion.Euler(Vector3.right*90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent);
newTile.parent = mapHolder;
}
}
}
}
Answer by toddisarockstar · Jun 17, 2017 at 05:34 AM
you should create an array to respresent all the tiles in your map to store information about the tiles. which acually is an array of arrays so that you can access or assign info about them with x and y coordinates.
if you wanted to assign countries i would just assign them with int numbers for faster checking later. you allready have a height and width in your mapsize variable. so here is how you would create what i'm talking about to assign an int to every "tile".
// create an empty array of arrays for two dementions
public int[][] land;
void Start() {
// initialize the first part of the array according to your mapsize.x
land= new int[(int)mapsize.x][];
// now for loop through to initialize to represent your y
int i = land.Length;
while (i>0) {i--;
land[i]=new int[(int)mapsize.y];
}
// now yer set up!!!!!
//this is how to assign a number to a map coordinate
// lets use an example of 2 accross and 4 down on your map
land [2] [4] = 13;
print (land [2] [4]);
}
Why don't you use a multi-dimensional array ins$$anonymous$$d of an array of arrays like so: int[,]
Jagged arrays are faster to iterate than multi-dimensional ones(in C#) where the latter induces a bunch of unessacary invocations. Even if Jagged arrays are faster, they're not really suitable for large tilemaps because each element is a reference type(and so is the array itself).
The best way to save up on allocations is to use a 1D array. Thinking about chunks from the start is also a pretty good idea.
1 world contains many chunks 1 chunk contains many tiles
Here's an example on setting up a X/Y grid in a 1D array
private static void CreateChunks()
{
for(int i = 0, y = 0; y < chunkResolution; y++) {
for(int x = 0; x < chunkResolution; x++, i++) {
Chunk newChunk = new Chunk();
newChunk.Initialize(chunkResolution, size);
newChunk.Create(x, y);
chunks[i] = newChunk;
}
}
}
hello that interesting me but i'm beginer, can you comment this code ? Chunk is a smal size of area that compose a map right ? Is a chunk a gameobject prefab instantiate for exemple ?
Answer by Glurth · Jul 20, 2017 at 04:27 PM
" I thought maybe I could define each individual tile and then define the tile to wich county it belonged to (for starters neutral, red blue, green, yellow). "
This sounds exactly right. I see in your code that you create your tile gameobjects dynamically.
Transform newTile = Instantiate (tilePrefab, tilePos, Quaternion.Euler(Vector3.right*90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent);
So, I would suggest you add a new COMPONENT to your tile prefab. Something like:
class Tile:MonoBehavior //defines the Tile class as a component
{
public int provinceID; //use a number to specify which provience this tile belongs to. This number is visible in the editor
}
While the above technically answers the question, by itself, it's not very useful.
You will probably want to make the Tile a particular color, based on the province. (I'll leave HOW to do that up to you.) And then make sure the tile's will be drawn with that particular color.
class Tile:MonoBehavior //defines the Tile class as a component
{
public int provinceID; //use a number to specify which provience this tile belongs to.
Color ProvinceColor(){....} //this function cenverts the provience ID into a color (not unity specific)
private void Start()// when this compoennt starts, it will set the color of the MeshRender's material to the provinceColor
{
MeshRenderer mr = GetComponent<MeshRenderer>();//this is the compoent that does the drawing
if (mr)//confirm drawing component exists
mr.material.color = ProvinceColor();
}
}
Keep in mind, the prefab will have a particular vaule for the provide member, you will probabaly want to adjust this while building the map, (just like when you modify the localScale and position of the prefab).
Importantly, this is just a beginning for your "Tile" class- eventually, it should handle everything you need an individual tile to do.
Your answer
Follow this Question
Related Questions
How to draw a border around tiles in Unity's Tilemaps? 0 Answers
Terrain Tile Seams and Terrain.SetNeighbor 1 Answer
Do I need to destroy objects I'm not using? 4 Answers
How can I find adjacent 2D tiles without using colliders? 3 Answers
How do i make large tilemaps that i can individually select and edit each tile? 0 Answers