Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KBCreater · Jun 14, 2017 at 12:10 AM · tilesborders

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;
             }
         }
 
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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]);
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ShadyProductions · Jul 20, 2017 at 06:48 AM 0
Share

Why don't you use a multi-dimensional array ins$$anonymous$$d of an array of arrays like so: int[,]

avatar image EnokV ShadyProductions · Jul 20, 2017 at 09:53 AM 1
Share

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;
             }
         }
     }

avatar image agencebrunel EnokV · Jul 20, 2017 at 04:04 PM 0
Share

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 ?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges