Hello Guest

Author Topic: Change TileMap graphics dynamically  (Read 6784 times)

Mary674

  • Newbie
  • *
  • Posts: 2
    • View Profile
Change TileMap graphics dynamically
« on: December 31, 2013, 12:55:29 am »
Hello,
I would like to be able to change a Tilemap's graphics dynamically via script.
Is it feasible, and if so, what would be the simplest method?

I thought about changing the Sprite Collection it uses, but there doesn't seem to be an easy way to do it and I am not familiar enough with the code to figure it out by myself. I don't even know if it would be the right way to do it.

To be more specific, I want to change the seasons in my game depending on a variable. I would like the graphics of my Tilemap to change accordingly. For that purpose, I have 4 identically placed TileSheets, with only the colors being different depending on the season. The graphics would change without having to reload.

Thank you very much!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #1 on: December 31, 2013, 10:18:48 am »
By far the easiest and most efficient way is to make sure your sprite collections are interchangeable i.e. the sprites are in the same order and are the same size = the atlas textures will be interchangeable.
At this point you can simply swap the material textures to change the graphics.

Mary674

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #2 on: January 01, 2014, 12:30:58 am »
Thank you, but how would I go about that?
I've tried this:

public GameObject summer; (this is the sprite collection)
public Texture fallTexture;    (this is the texture in the data folder)
summer.GetComponent<tk2dSpriteCollectionData>().textures[0] = fallTexture;

This doesn't do anything... Sorry for being a complete newbie. :P

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #3 on: January 01, 2014, 11:54:30 pm »
Code: [Select]
public tk2dTileMap tileMap;
public tk2dSpriteCollectionData thingYouWantToChangeItTo;

tileMap.SpriteCollectionInst.materialInst[0].mainTexture = thingYouWantToChangeItTo.material.mainTexture;


Something like that... I don't have my main dev machine at the moment so I'm typing a lot of that from memory / source repo so it could be wrong.... thats the general idea anyway. Hope it helps, post back if it doest - I should have my dev machine back in a day or two.

tarcisiotm

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #4 on: February 27, 2014, 03:18:32 am »
Code: [Select]
public tk2dTileMap tileMap;
public tk2dSpriteCollectionData thingYouWantToChangeItTo;

tileMap.SpriteCollectionInst.materialInst[0].mainTexture = thingYouWantToChangeItTo.material.mainTexture;

Hi! I tried doing the above to change the tilemap at runtime but couldn't succeed. I keep getting this message:

Code: [Select]
UnassignedReferenceException: The variable material of 'tk2dSpriteCollectionData' has not been assigned.
You probably need to assign the material variable of the tk2dSpriteCollectionData script in the inspector.
UnityEngine.Material.GetTexture (System.String propertyName) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/ShaderBindings.cs:220)
UnityEngine.Material.get_mainTexture () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/ShaderBindings.cs:166)
...

When the code calls this line:

Code: [Select]
        tileMap.SpriteCollectionInst.materialInsts[0].mainTexture = black.material.mainTexture;

What can I do? I also need to change the tiles at runtime.

While in the topic, is it possible to apply shaders to one layer?

Thanks in advance!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #5 on: February 27, 2014, 01:36:06 pm »
It sounds like material is null? Shouldn't you be getting it the same way - black.inst.materialInst[0]..?

tarcisiotm

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #6 on: February 27, 2014, 03:19:25 pm »
Doing the suggested I could load tiles but they were loaded in the wrong order.

If I change the sprite collection in the tilemap via the editor, I "get" the effect I want, the tiles match. Any suggestions?

Thanks again.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #7 on: February 27, 2014, 03:47:14 pm »
It looks like the tiles are being atlases differently. If you disable trimming in both, the likelihood is the textures will be the same, and as such will be interchangeable.

There is always the option of rebuilding the tile map entirely but thats slow.

tarcisiotm

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Change TileMap graphics dynamically
« Reply #8 on: February 27, 2014, 07:51:23 pm »
It looks like the tiles are being atlases differently. If you disable trimming in both, the likelihood is the textures will be the same, and as such will be interchangeable.

There is always the option of rebuilding the tile map entirely but thats slow.

That was it! And it makes perfect sense. Since my tilemap was not "similar", but a version of entirely black, probably some tiles got cut by the trimming algorithm. Now everything is perfect. Only care I had to take was not to lose the reference to the original texture.

Also noteworthy, didn't need to rebuild!

Thanks a lot!