Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Correct way to generate map from xml tile

Discussion in '2D' started by cruzlutor, Jan 19, 2015.

  1. cruzlutor

    cruzlutor

    Joined:
    Jan 14, 2015
    Posts:
    13
    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
     
    deanbad likes this.
  2. Souri

    Souri

    Joined:
    May 30, 2013
    Posts:
    12
    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 - https://bitbucket.org/Chaoseiro/x-unitmx/wiki/Home
    Plans - https://trello.com/b/aIFN7aTk/x-unitmx
    Support forum - http://forum.x-unitmx.org
     
    DustyMcp likes this.
  3. DustyMcp

    DustyMcp

    Joined:
    May 23, 2013
    Posts:
    25
    http://studentguru.gr/b/dt008/archive/2014/09/04/a-tower-defense-game-in-unity-part-1

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

    Code (CSharp):
    1. for(x = 0, x < 100,x++) {
    2. for(y = 0, y < 100, y++){
    3. instantiate(object,new vector3(x,y,0f));
    4. }
    5. }
    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
     
  4. DustyMcp

    DustyMcp

    Joined:
    May 23, 2013
    Posts:
    25
    I did not know this, thanks for saving me alot of time converting values ^^
     
  5. cruzlutor

    cruzlutor

    Joined:
    Jan 14, 2015
    Posts:
    13
    X-UniTMX works fine

    thanks :)
     
  6. Rutu_g

    Rutu_g

    Joined:
    Jul 30, 2015
    Posts:
    1
    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. Screen Shot 2015-07-30 at 7.16.04 pm.png
     
  7. Cr3m4t0rY

    Cr3m4t0rY

    Joined:
    Sep 4, 2015
    Posts:
    1
    I got deprecated warnings when I try to run it.