Hello Guest

Author Topic: How to change a tilemap layer's sorting layer and number at runtime?  (Read 4374 times)

Quells122

  • Newbie
  • *
  • Posts: 18
    • View Profile
Is there a way to expose the sorting layer and sorting number for tilemap layers so they can be changed via script?

I create my levels at runtime. Some levels have more layers of background tiles than foreground tiles and vice versa, so it would be really helpful if I could change a layer's orientation to BG or FG depending on where the need is.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to change a tilemap layer's sorting layer and number at runtime?
« Reply #1 on: November 28, 2014, 12:38:51 pm »
You can't change that at runtime directly. However, you can write a script that finds all the MeshRenderers on each layer gameobject and change those rather than through the tilemap. Should be pretty straightforward.

Quells122

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to change a tilemap layer's sorting layer and number at runtime?
« Reply #2 on: November 29, 2014, 05:46:13 am »
Thanks for the idea! I was eventually able to find the relevant chunks and change the sorting order after some trial and testing.

One thing that threw me off for a while, was the number of "Chunks". Each layer strangely had 80 chunks, many of which were duplicates. The tilemap is 128x128 tiles and each chunk is 32x32, so there should only be 16 chunks per layer. These chunks didn't seem to be used at all by the game. Initially I was grabbing these chunks and changing their mesh renderer's sort properties, and nothing would happen. Eventually I realized that when I started the game, new chunks were created and these "junk" chunks really weren't used at all. Is this a known issue? What causes these unused chunks to show up?

The other issue that concerns me is if I need to worry about refinding chunks? It seems like it would be possible to store the chunks in an array, and then go through them and change their MeshRenderer properties when I need to change their layer order. However, I'm not sure how these chunks are managed. Are new ones continually created so I should go and refind the chunks? Or are they reused?
« Last Edit: November 29, 2014, 05:55:25 am by Quells122 »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to change a tilemap layer's sorting layer and number at runtime?
« Reply #3 on: November 29, 2014, 11:25:52 am »
Chunks should be reused. Delete the renderdata and commit again to generate the chunks. Unused chunks only show up because the link is lost, sometimes - only thing I know that has caused issues in the past is some undo related issues. Try recreating it and see if it happens again?

Quells122

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to change a tilemap layer's sorting layer and number at runtime?
« Reply #4 on: December 02, 2014, 12:00:20 am »
Thanks for the advice! I was able to get it working :)