Hello Guest

Author Topic: How To Use Tilemaps to Create Prefabs??  (Read 11417 times)

NMD

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
How To Use Tilemaps to Create Prefabs??
« on: March 14, 2014, 01:14:55 am »
Hello there,

The tile map editor makes a prefect solution to building platforms for my 2D game.

Ideally, I need to build them in a separate scene and use them as prefabs in my spawn-manger in the game.

I found a few posts where you mention stuff like this but it does not seem to apply to the existing system.

This post from a while ago from you would be absolutely prefect for me if you could tell me how to do this...

Quote
1. You create a separate scene, and create a tilemap in there, ticking the Export To Prefab option.
2. Save a prefab into a directory using the newly revealed buttons. This will save the file in there, and create a Meshes directory with the prefab name.
3. In your other scenes, you simply drag this prefab into the scene and use it in whatever way you'd like.
http://2dtoolkit.com/forum/index.php/topic,215.msg848.html#msg848

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #1 on: March 14, 2014, 10:24:46 am »
The export to prefab option was retired at some point (2D Toolkit 2.0 I think). You can simply create a prefab of a tile map and instantiate that if you want - it should be fully supported in 2D Toolkit 2.3 and 2.4

NMD

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #2 on: March 14, 2014, 05:24:45 pm »
Wouldn't that be extremely expensive to have multiple entire Tilemap objects?

I need to build 100s of different 2d platforms out of the same set of tiles. If you can guide me where to start or give some pointers I can prob figure it out...

I know I'll need to somehow save out the "chunk" mesh and mesh collier, and somehow make sure the "chunk" references the right sprites from the atlas.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #3 on: March 14, 2014, 05:55:37 pm »
If you need to do that, you could probably write a script that will convert a tile map or tile map section to a static sprite batcher. Or you could save the chunk meshes and game objects recursively, but that won't work with platform collections (they need to be rebuilt when that changes).

You don't need any specific tk2d functions to save them out. Just instantiate the render object so you're not messing with the original, create a prefab from it, find all meshes in children and AddObjectToAsset. The last step is essential to be able to let this work elsewhere.

NMD

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #4 on: March 14, 2014, 08:30:23 pm »
Ok cool, thank you...

I've never really done any asset loading and saving outside of text/xml and bin read/write. I've never even created a prefab from code, hehe, I guess I'll have to look thru the docs some.

I'm not sure what you mean by saving the game objects recursively, you mean like recursing up a tree, where you call whatever function inside itself, moving to children and parents?

I think I mostly get it, just not totally clear. I think you are saying...

1. Created and edit the tilemap object, commit it into a chunk.
2. Instantiate a clone of the entire RenderData object(RenderData->Layer0->Chunk0)
     Don't need to do anything with the tilemap object or the asset data files?
3. Save this clone as a prefab"AssetDatabase.CreateAsset(clone, "path");" ???
    This creates a .prefab???
4. Find all meshes and mesh collider from the clone "GetComponentsinChildren<Mesh(Meshfilter??) / MeshCollider>", and add them to the new saved prefab with "AssetDatabase.AddObjectToAsset(mesh, "path");" ??

At that point how does the mesh know which sprite cords to pull from the collection atlas? Or is that part of the meshfilter or something that gets saved out?

Thank you so much btw!!!!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #5 on: March 15, 2014, 11:17:34 am »
You're gonna have to try this out :) Its been a while since I did this -
The recursing is necessary (as far as I remember), to create a copy of the meshes on each mesh filter in the children. About your steps
3 - yes thats right :) Might need to AssetDatabase.SavaeAssets() and Refresh(), can't remember.
4 - You need to find all mesh filters on the clone, make sure they are unique (filter.sharedMesh = Instantiate(filter.sharedMesh) as Mesh;) then AddObjectToAsset to the prefabPath.

The mesh already contains the sprite coordinates, etc. This will only work with non-png atlases and when you don't use platform collections. Its a lot more messy if you want to cope with those.

NMD

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #6 on: March 15, 2014, 05:15:38 pm »
Hey thanks,

Gonna start tackling this today, got like 5 things on my plate right now for this game...  Recent vid if you are interested: https://www.youtube.com/watch?v=O0BJLhio7_A

I still don't understand what you mean by recursion... To me recursion is something like;
DoFoo(node)
{
    ...
    DoFoo(node.nextNode);
}

As for as platform specifics. I'm guessing the most straight forward way is to create separate prefabs sets and at start-up load the proper set based on the tk2d platform setting or device res. This is not a problem for the spawnmanager, albeit a lil sloppy, hehe.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #7 on: March 15, 2014, 10:04:46 pm »
Yeah I mentioned recursion because my function was written in that way (i.e. foreach transform.getchild, does child have a mesh filter, then call function again). I needed that for specific reasons though, I don't think you will need to step through the hierarchy like that - a simple GetComponentsInChildren<MeshFilter> will suffice.

For platform specific stuff it isn't as simple as you mentioned - the materials are generated at runtime for platform sprites, so you will probably have to duplicate those and save them just like you do the meshes.

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #8 on: October 30, 2014, 08:50:12 pm »
I'm trying the steps listed here, but for some reason, when I instantiate the prefab, the tiles aren't being rendered.

What I'm trying to, it create small "Bonus Rooms" and them load them into the scene on the fly. When I load a Bonus Room into an existing scene, the Mesh's seem to get nulled out:
http://cl.ly/image/2X2w3T303x0Q

Any ideas that I'm doing wrong?

[UPDATE] I saw your other posts about MeshData not getting saved in prefabs. Calling tileMap.ForceBuild() after instantiation did the trick!
« Last Edit: October 30, 2014, 08:56:03 pm by esdot »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #9 on: October 30, 2014, 10:44:12 pm »
What version of 2D Toolkit are you using? Later versions should actually create render data automatically (equivalent of calling ForceBuild). In any case calling ForceBuild should do what you need.

Hale_88

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: How To Use Tilemaps to Create Prefabs??
« Reply #10 on: February 09, 2016, 10:04:15 pm »
I wanted to follow up on this as I am currently running into this issue. I am sure it is just something I am missing/not understanding.

I create a Room prefab, in this room prefab I have a Tilemap Render Data. My level is randomly generated by piecing several of these rooms together. When ever I instantiate or even bring in the prefab into a scene the Tilemap collision is there but no images. I think it has to do with the Mesh Filter as it is empty and that is the only difference between the original and a prefab that I can find.
« Last Edit: February 09, 2016, 10:12:44 pm by Hale_88 »