Hello Guest

Author Topic: Tilemap update  (Read 13182 times)

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Tilemap update
« on: March 25, 2013, 08:35:17 pm »
The Build() takes around 2000ms (around 46k objects)
the Tileset has 5 tiles, one of them is a prefab
/*
Grass
->Transition StaticSpriteBatch containing
-->UpLeft
-->UpRight
-->DownLeft
-->DownRight
*/

and when i do
            WorldClass.TileMap.Layers[Layer].SetTile(Mathf.RoundToInt(Index.x),Mathf.RoundToInt(Index.y),4);
            WorldClass.TileMap.Build();
it rebuilds then entire tilemap and resets all the gameobjects
The chunks are 16x16
Is there a way not to make it rebuild everything?
Tried BeginEditMode/EndEditmode, Build(editmode), Build(default), Build(Forcebuild) none of them seem to work faster then 2000ms.

//Thanks
Logon

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #1 on: March 25, 2013, 08:46:44 pm »
Build should only build changed partitions.
Any idea why the tilemap thinks the tiles have changed?

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #2 on: March 25, 2013, 09:22:35 pm »
It's the chuck itself it seems like it is reinstantiateing all the prefabs each time i change the tile to prefab(4) and then in turn runs Awake
« Last Edit: March 25, 2013, 09:28:28 pm by Logon »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #3 on: March 26, 2013, 01:12:09 am »
The prefab thing looks like a bug. There isn't a quick and dirty fix for it, but you can disable building prefabs in tk2dTilemap.Build() - add a new build flag, eg. DisablePrefabs and use this to conditionally call BuilderUtil.SpawnPrefabs.

This will be addressed properly in the next tilemap update.

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #4 on: March 26, 2013, 06:43:20 am »
But does this not stop the prefab from spawning the first time?
So say it is a (0)Noprefab and i change it to 4(prefab) and run the modified build will it spawn the new prefab at all?

Any ETA on when you will fix this?
A feature tip, you could pool deleted prefabs from the chunkmap

Edit:
You already have the function SpawnPrefabsForChunk(tk2dTileMap tileMap, SpriteChunk chunk, int baseX, int baseY) ;

tileMap.Layers[layerId]; 
int baseY = cellY * layer.divY;(What do you do here?)
int baseX = cellX * layer.divX;(And here?)
var chunk = layer.GetChunk(cellX, cellY);
SpawnPrefabsForChunk(tileMap, chunk, baseX, baseY);


 Object prefab = tilePrefabs[tile];                                         
 if (prefab != null)    Shouldn't this be prefab == null?                           
{
                                                  prefabCounts[tile]++;
                                                   #if UNITY_EDITOR && !(UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4)
                                                  GameObject go = UnityEditor.PrefabUtility.InstantiatePrefab(prefab) as GameObject;
  #else
                                                  GameObject go = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; 
#endif
                                                ETC...
}
« Last Edit: March 26, 2013, 08:45:06 am by Logon »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #5 on: March 27, 2013, 12:17:28 am »
That conditional is correct. It should only instantiate when prefab != null.

Doing what I suggested won't stop prefabs from spawning the first time. This will be the behaviour in the corrected implementation - prefabs are spawned on startup but never when the tilemap is modified at runtime.

Things could go really weird - say you have an enemy as a prefab, and its moved off to another cell, changing the source cell will make it disappear wherever it may be at the time.

What are you using this for specifically?

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #6 on: March 27, 2013, 06:43:59 am »
To do transition tiles between tiles
so each tile 32x32 contains 4 16x16 tiles which can make up any combination of transition to a nearby tile so i use a GO with a StaticSpritebatcher which in turn means that the tilemap could change at any time.

While you are racking your head with this is there anyway to mux together 4 16x16 sprites into one big 32x32 sprite and put it in a spritesheet at runtime?
Because i don't need the gameobject logic i just want a 32x32 sprite basiclly only this was the solution i could come up with.

Thanks
Logon
ps Registered my copy last night awesome pack!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #7 on: March 27, 2013, 10:14:27 am »
I still don't understand why you would use instances for those - if they were separate sprites in the tilemap, couldn't you just change the tilemap? Maybe a screenshot of what you're trying to do will help.

Yes, use runtime sprite collections. There is a sample in 1.92. You are limited to using one sprite collection per tilemap right now though, so you'll need all your other tiles in there too.

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #8 on: March 27, 2013, 10:26:55 am »
No i don't neccesarilly need to use instances to do what i am trying to accomplish

can i do something like this
SS1 Transitions Spritesheet
SS2 Tilemap Spritesheet

texture2d t = new texture(32,32)

t.setpixelsinrectangle(ss1.rectangle(0,0,16,16)(This gets a tex2d from 0,0 size 16,16))
t.setpixelsinrectangle(ss1.rectangle(16,0,16,16))
t.setpixelsinrectangle(ss1.rectangle(0,16,16,16))
t.setpixelsinrectangle(ss1.rectangle(16,16,16,16)

if(ss2.exists(name: "transID-transID-transID-transID")) else
 SS2.add(texture: t, name: "transID-transID-transID-transID");
 tile.setid(GetIdFromname("transID-transID-transID-transID"));
endif;

this way i get sourcerectangles from the first spritesheet(Would be awesome if i could get sourcerectangle from the spritecollection instead of doing the "math" myself)
and mux them together to form a 32x32 tile and i add that to the tilemap spritesheet. The problem i can see doing this is that a 2048x2048 spritesheet only holds 128 tiles(or can the spritesheet be as big as i want? i guess not because that requires dx11 right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #9 on: March 27, 2013, 10:40:38 am »
Runtime sprite collections could work just fine, or you can simply set up the relationships in the main sprite collection too. Ultimately, all you're doing is matching up
names for transitions, so you could add the logic in the tilemap editor, or an editor script that is invoked at editor time.

You can use 4096 textures in pretty much all common PC graphics hardware.

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #10 on: March 27, 2013, 10:56:10 am »
The good thing about runtime is that i don't manually have to sit and do around 21 sprites and if the sprites are never used they don't take up any space either.
I don't really get what you are saying i should do but i'll dig some when i get home from work and see if i can figure it out.

What i in essence want to do is draw 4 (out of 14) sprites on 1 quad and possibly all the variations of the 14 sprites in these 4 slots. if you can give me a tip on how to do that i can probably figure out the rest by myself.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #11 on: March 27, 2013, 11:03:52 am »
Yup look at the runtime sprite collection sample - it shows you how to set up a sprite collection at runtime and use it. You can use it to build tiles from anything.

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #12 on: March 27, 2013, 05:31:49 pm »
tk2dSpriteCollectionData.CreateFromTexture you have to recreate the entire sheet each time you want to add something is there a way to just add one?


Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tilemap update
« Reply #13 on: March 27, 2013, 05:41:56 pm »
No, but you could modify the code to append to the sprite collection. All the data is pretty transparent in there.

Logon

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Tilemap update
« Reply #14 on: April 20, 2013, 10:28:05 pm »
Btw i guess the tilemap update for prefabs this is the foe

/// Spawns all prefabs for a given tilemap
      /// Expects populated chunks to have valid GameObjects
      public static void SpawnPrefabs(tk2dTileMap tileMap)
      {
         int numLayers = tileMap.Layers.Length;
         for (int layerId = 0; layerId < numLayers; ++layerId)
         {
            var layer = tileMap.Layers[layerId];
            if (layer.IsEmpty || tileMap.data.Layers[layerId].skipMeshGeneration)
               continue;
            
            for (int cellY = 0; cellY < layer.numRows; ++cellY)
            {
               int baseY = cellY * layer.divY;
               for (int cellX = 0; cellX < layer.numColumns; ++cellX)
               {
                  int baseX = cellX * layer.divX;
                  SpriteChunk chunk = layer.GetChunk(cellX, cellY);
                  if (chunk.IsEmpty)
                     continue;
                        if (chunk.Dirty)
                        {
                            SpawnPrefabsForChunk(tileMap, chunk, baseX, baseY);
                        }
               }
            }
         }
      }

you don't check if the chunk is dirty in your code.