Hello Guest

Author Topic: Sort Method - Rendering incorrectly when using Prefabs  (Read 3359 times)

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Sort Method - Rendering incorrectly when using Prefabs
« on: August 07, 2014, 11:03:51 pm »
Our tile set contains some perspective, so we use Sort Method: Bottom Right and are trying to swap an animated Tile in place of the static one using Prefab linkage.

It should looks like this:


But as soon as I commit on the TileMap, it renders like this:


Cab this be fixed? If using the SortMethod to layout tiles, and we swap those tiles with a Prefab, the Sort Method should continue to work.
« Last Edit: August 07, 2014, 11:05:51 pm by esdot »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #1 on: August 10, 2014, 12:31:37 pm »
That isnt really possible to do. The sort method sorts tiles but can't sort anything else with it correctly. If everything was a prefab it would sort correctly, but prefabs can't reliably sort with tilemaps as tilemaps get their performance by drawing entire chunks of them at a time.

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #2 on: August 11, 2014, 05:11:12 am »
Hmm would it be possible to disable batching then? We may not need the performance gains...

This essentially makes the Prefab feature useless for any tiles that have perspective which forces them to overlap.

Alternatively, if we had the ability to map an Animation to a Tile then we wouldn't need to use prefabs at all. Would that be possible?
Cheers,

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #3 on: August 11, 2014, 10:13:46 am »
You can disable batching by making everything a prefab. Otherwise tk2d will use the optimal path of batching everything.

You can't map an animation to a tile, but you can manually change tiles from code.

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #4 on: August 11, 2014, 05:23:02 pm »
Ah ok, thanks. I don't think that making everything a Prefab would be a very good workflow, but swapping tiles at runtime could work great.

Could you give a quick code snippet on looping through the tileMap, and swapping all instances of sprite0 with sprite1?

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #5 on: August 11, 2014, 07:18:18 pm »
Don't worry about the code snippet, I figured it out.

Unfortunately, I'm not sure if this will be viable, as it drops my desktop to 50fps, and on Nexus 7 it's basically unplayable.

I optimized it as best I could, caching a list of all animated frames, but the TimeMap.build() call just kills performance.

Here's the script if anyone is interested:
https://gist.github.com/treefortress/2c9b034e696953bd83fc

Gonna try now to make everything Prefabs, and see how performance is there.

It would really ease our workflow if there was a "Treat everything as prefabs" checkbox (aka "Don't batch"), because prefabbing every single tile kinda seems like a lot of busywork.

« Last Edit: August 11, 2014, 07:26:43 pm by esdot »

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #6 on: August 11, 2014, 09:06:50 pm »
You can disable batching by making everything a prefab.

I tried this, and it is not respecting the "Sorth Method: Bottom Right" at all :(



it should be rendering like this:
« Last Edit: August 11, 2014, 09:17:07 pm by esdot »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #7 on: August 11, 2014, 10:22:56 pm »
If the prefab performs well enough, you can patch the tk2d tilemap prefab spawning thing to assign renderer sort order values based on position, i.e. sorting similar to sort method. This only works when everything is a prefab, and will also work for dynamic objects.

You'll need to get a unique int value for each tile - which we can work out by  unique = y * width + (width - 1 - x), and then assign this as the renderer.sortingOrder value in SpawnPrefabsForChunk.

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #8 on: August 11, 2014, 10:29:10 pm »
Ahh ok, that should work, I can just loop through them all, and set the renderOrder myself from Bottom-Right, doh!

Definitely would be nice to have support for animations baked into the Toolkit. Or an option to force everything to be "sprites" so we don't have to manually create prefabs just to force TileMap to not batch them.

You could even add a "Exclude from Batch" or or "Render as Sprite" checkbox, on the Data setting for each tile. Then we'd just need to highlight the tile, check the checkbox and then run a simple loop to assign order position.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9708
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #9 on: August 11, 2014, 11:12:59 pm »
Animations in the toolkit are not straightforward to do efficiently in Unity otherwise they'd already be in. I've really wanted to revisit this once Unity have created a better codepath for dynamic meshes, but as of now this is still a bit rubbish, especially for the kind of volume we'd need.

Render as sprites is too easy to break, anything > a few thousand sprites (64x64 = 4k sprites...) and your perf will dip. Only way something like that could be practical is to limit the tilemap size massively in the docs, but no one reads the docs :)

esdot

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Sort Method - Rendering incorrectly when using Prefabs
« Reply #10 on: August 11, 2014, 11:45:53 pm »
Ok, thanks for the replies.

We can hack around this by having 4 versions of each animation and manually paint them so they look like they're properly sorted.

But because of the workflow issues with the above, we'll probably just try and find an alternative to animated tiles in general.
Cheers,