Hello Guest

Author Topic: How to make sprite from tilemap?  (Read 9804 times)

TDippingSauce

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
How to make sprite from tilemap?
« on: July 06, 2014, 01:32:29 pm »
Hi, I want to move some part of my tilemap tiles smoothly, in all 4 directions.

It's not something like moving platform - Players decide which tiles to move, and also direction. So please don't tell me something like 'Just create a gameobject that moves!'.

For the smooth moving, I don't want to use getTile() and setTile() method only because in that way tiles just disappears and appears suddenly.

So My solution is little bit tricky :
1)Create a Sprite from part of the tilemap.
2)Overlap the sprite over the tiles which I originally wanted to move.
3)Make original tiles disappear, using setTile(originalX,originalY,-1).
4)Sprite starts to move.(Smoothly!)
5)If sprite reaches its destination, destroy the sprite and reappear tiles using setTile(destX,destY,originalTileType)

Then player can never notice how things work, but they'll only see tiles moving smoothly.

But the problem is I don't know how to create sprite from tilemap... I can get integer tileType data, but I don't know how to create sprite from it.

Can anyone help me please? :)

*oh and sry if my english is poor, it's not my native language:(
« Last Edit: July 06, 2014, 01:34:11 pm by TDippingSauce »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to make sprite from tilemap?
« Reply #1 on: July 06, 2014, 04:37:08 pm »
You can't create a sprite from a tilemap directly. This is a pretty specific use case, but its doable :)
I'm assuming you want to create a sprite from a selection of tiles at a time, x0..x1, y0..y1 in tilemap coordinates

1. Create a static sprite batcher with position = 0, 0, 0. http://2dtoolkit.com/docs/latest/advanced/scripting_static_sprite_batcher.html
2. You need to know which sprites to spawn, and where. So for each tile in x0..x1, y0..y1, call tilemap.GetTilePosition(x, y) to get the position of the tile.
3. As above, call tilemap.GetTileIdAtPosition(x, y) to get the tile id. If its -1 it means theres no tile there. Otherwise, the tile id = the sprite id, which is nice and convenient.
4. Sprite collection = tilemap.Editor__SpriteCollection (yeah... its meant for the editor, but you can use it!)

When you call batcher.Build(), you'll have 1 gameobject with all the sprites at that potision, do with it what you wish, then Destroy it to get rid of it when youre done.

TDippingSauce

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to make sprite from tilemap?
« Reply #2 on: July 07, 2014, 04:57:38 am »
Wow I could use static sprite batcher for this?? AWESOME! I'll try this way when I go back to home. I'll post the result then :)

TDippingSauce

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to make sprite from tilemap?
« Reply #3 on: July 07, 2014, 03:08:19 pm »
Works Like a charm! I didn't implement the 'Smooth Moving' yet, but I succeeded to make a GameObject which is created from tilemap.

Here is code, if any other guys need help in the future:)


Code: [Select]
using UnityEngine;
using System.Collections;
using tk2dRuntime;

public class ClickManager : MonoBehaviour {
public tk2dTileMap tilemap;

private GameObject createdSpriteGO = null;

// Use this for initialization
void Start () { }

// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)){
if(createdSpriteGO == null){
createdSpriteGO = createSpriteFromTilemap(3, 0, 3, 1);
}else{
Destroy (createdSpriteGO);
createdSpriteGO = null;
}
}
}

GameObject createSpriteFromTilemap(int originalX, int originalY, int destX, int destY){
GameObject tmpGO = new GameObject();
tk2dStaticSpriteBatcher batcher = tmpGO.AddComponent<tk2dStaticSpriteBatcher>();
int tileNum = (destX - originalX + 1) * (destY - originalY + 1);
Debug.Log("tileNum to batch: " + tileNum);

batcher.batchedSprites = new tk2dBatchedSprite[tileNum];

int idxCounter = 0;
for (int x = originalX; x <= destX; x++) {
for(int y = originalY; y <= destY; y++){
tk2dBatchedSprite bs = new tk2dBatchedSprite();

// assign sprite collection and sprite Id for this batched sprite
bs.spriteCollection = tilemap.Editor__SpriteCollection;
bs.spriteId = tilemap.GetTile(x, y, 0);

Vector3 pos = new Vector3(x, y , 0);

// Assign the relative matrix. Use this in place of bs.position
bs.relativeMatrix.SetTRS(pos, Quaternion.identity, Vector3.one);

batcher.batchedSprites[idxCounter] = bs;

idxCounter++;
}
}

// Don't create colliders when you don't need them. It is very expensive to
// generate colliders at runtime.
batcher.SetFlag( tk2dStaticSpriteBatcher.Flags.GenerateCollider, false );

batcher.Build();
return tmpGO;
}
}


fallingbrickwork

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
    • Montague's Mount
Re: How to make sprite from tilemap?
« Reply #4 on: July 07, 2014, 10:29:35 pm »
Many thanks for posting this code TDippingSauce , it will certainly come in handy with what i am currently working on!  :)

Regards,
Matt.

fsadeq

  • 2D Toolkit
  • Sr. Member
  • *
  • Posts: 353
    • View Profile
Re: How to make sprite from tilemap?
« Reply #5 on: July 08, 2014, 06:08:46 pm »
I'm actually looking to something similar, except in the editor and not at runtime. Is such a thing possible? Essentially what I'd like to do is extract certain groups of tiles from the tilemap and turn them into their own gameObjects. This is for elements that aren't going to be static, like the original poster. Right now I am manually building the objects sprite by sprite and it is extremely tedious. Is there some way to hack this to somehow mark a group of tiles to "pull" them out of the renderdata and in to their own objects?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to make sprite from tilemap?
« Reply #6 on: July 08, 2014, 07:39:38 pm »
If you run the code above in the editor from some menu, it should just work. You can integrate into the tilemap editor too if you really need that, but that will mean messing about with the tilemap editor codebase, and it will need patching in a few places for that. If you really want to do that, let me know and I can give you some idea of what needs to be done.