Correct way to generate map from xml tile

Hi guys
I’m working in a 2D RPG game, I’m trying to render a XML tile map (Generated by Tiled Map Editor) in my game.
I have Tileset.png, Map.xml and Map.cs.

At the moment i’m doing the following

  1. Drag and drop my Tileset.png to Unity
  2. Open the sprite editor and slice 32x32
  3. Drop the Map.xml in resources folder
  4. In Map.cs load and parse the Map.xml and store tiles id in Array
  5. In Map.cs load all Sprites in Tileset.png using Resources.LoadAll()
    So far so good

Now, i have a couple of doubts

About tile object in Unity
What i need do next with my sprites, i read about gameobject with sprite inside, prefabs, materials, sprite packer, ¿What is the correct way?

About dynamic tile generation
I need to create my tiles with Instantiate?
If i have a lot of elements in my game (ground tiles), it doesn’t affect the game performance?

i have read about 2d toolkits, but i want to learn how to do it

1 Like

If you want to save yourself the trouble of learning how to do it, you can get X-UniTMX (a TMX importer for Unity) to do it for you. It’s free and regularly updated by Chaoseiro.

  • If i have a lot of elements in my game (ground tiles), it doesn’t affect the game performance?
    X-UniTMX will convert your tile layers from TILED into single meshes so it’ll run faster. There’s an example scene which has something like a 512 x 512 tiled map and it runs incredibly smooth.

About tile object in Unity

  • It’ll import objects you’ve drawn and created in TILED and import them as collisions, prefabs etc… There’s a lot of options you can set in TILED for easy importing into Unity like marking it’s “type” as a trigger, or inputing it’s layer name, tag etc in the object’s “Custom Properties”. You can see how much you can set there here (there’s quite a lot!): https://bitbucket.org/Chaoseiro/x-unitmx/wiki/Properties

On dynamic tile generation:
“I need to create my tiles with Instantiate?”
X-UniTMX offers a few ways. You can import your TMX file in your scene and save it like that, or you can load/stream it through code. There’s tonnes of example code and scenes in the package, and it’s well worth checking out thoroughly.

X-UniTMX and Tiled are essential parts of my workflow now. After an initial period of setting up things in Tiled (entering the type and custom properties of your objects so they can be imported as collisions or prefabs with their proper tags, layer names etc), as well as some simple import options on Unity’s side, I can do all my map editing and level design from then on in Tiled. And after every save, all changes are imported and reflected perfectly in Unity.

The wiki - Chaoseiro / X-UniTMX / wiki / Home — Bitbucket
Plans - Trello
Support forum - http://forum.x-unitmx.org

1 Like

This article explains it fairly well, however to instantiate your objects you just need to make 2 forloops like this:

for(x = 0, x < 100,x++) {
for(y = 0, y < 100, y++){
instantiate(object,new vector3(x,y,0f));
}
}

This will go 100 through the x axis and 100 through the y axis modify to your liking enjoy.

i made my tower defence game with this formula mapping all the pieces which needed to be a path, by just saying if my xml position == targetposition instantiate(tileprefab)

Reference:
http://puu.sh/eqnxP/a8967c4ee3.jpg

I did not know this, thanks for saving me alot of time converting values ^^

X-UniTMX works fine

thanks :slight_smile:

i am also using X-UniTMX and Tiled Map Editor.I added grassmap.xml and grass.png in same folder. but the Path Map row in Tiled Map Component*Script) is empty after selecting the xml file as map.xml and the default material does not take the png file. i am new in unity 3d. 2228604--148460--Screen Shot 2015-07-30 at 7.16.04 pm.png

I got deprecated warnings when I try to run it.