Hello Guest

Author Topic: Static sprite batcher not respecting layers properly?  (Read 6169 times)

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Static sprite batcher not respecting layers properly?
« on: February 06, 2014, 08:53:18 am »
Hi,

I am following the Whack a Mole tutorial (http://www.unikronsoftware.com/2dtoolkit/docs/latest/tutorial/whack_a_mole/game_objects.html)

I'm doing this in Unity 4.3.x with Sorting Layers, so I'm sorting the sprites using 'Order in layer' rather than using z-values. When I click Commit on the static sprite batcher, the Sky Burst sprite moved itself on top of the landscape sprite, but if i hit Edit, then the sprite is fine again.

Does the static sprite batcher have a problem respecting layers? or did I do something wrong


Before Commit:




And After Commit:

« Last Edit: February 06, 2014, 08:55:03 am by arvz »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #1 on: February 06, 2014, 09:09:05 am »
It doesn't respect layers and for good reason. Sorting layers in Unity work globally, and while the batcher could wort within itself, it wouldn't be able to sort with the rest of the scene as sorting layers are stored in the renderer and the entire static batcher only has one renderer.

arvz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #2 on: February 06, 2014, 09:11:46 am »
I see, thanks for that. Does this basically mean we must use z values?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #3 on: February 06, 2014, 09:12:44 am »
Yes

dav793

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #4 on: February 06, 2014, 11:36:46 pm »
Hi Unikron.

I am having the same problem, with the exception that I am already using z-values instead of 'order in layer'. Additionally I am setting up the sprite batcher via code at runtime, instead of setting it up beforehand in unity.

I got it working perfectly as long as there are no overlapping sprites, but in the case of overlapping sprites, the sprite batcher draws the first sprite encountered in the code on top, and every subsequent sprite is drawn under the last one, regardless of their z-value.

To position the batched sprites, i am using the function tk2dBatchedSprite.relativeMatrix.SetTRS() as indicated in the docs, in case it may have anything to do with it.

Been trying to solve this all day, with no success. Could you please help me out?


Thanks in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #5 on: February 06, 2014, 11:56:06 pm »
The sorting is only performed in the editor - it makes sense for the GUI related stuff to give the same results before and after committing. If you're adding sprites at runtime, just add them in the order you want them drawn in.

stilghar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #6 on: February 28, 2014, 10:13:44 pm »
What about having different static sprite batchers in different layers? Or in the same layer but in a different order? I don't see the option once I commit...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #7 on: March 01, 2014, 07:07:40 pm »
There isn't. Mainly because the sorting layer stuff isn't exposed properly in Unity. Easily added to this specific case though, as we've done the grunt work for the rest. How would you expect this to work when not committed though?

stilghar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #8 on: March 03, 2014, 10:26:01 am »
Ideally it should also respect layers when not committed. Is it doable? Once you assign a layer and layer order to a static sprite batcher all the children sprites should get this from the parent batcher when not commited.

Let me know if it can be done like this.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #9 on: March 03, 2014, 10:29:02 am »
No that can't be done, the batcher reduces everything to one renderer, and unity layers are set per renderer. That is one of the reasons I haven't done this - its just too messy due to how Unity works. Things in a hierarchy but can be drawn in a completely different order, etc. There is no way to inherit anything from parents without having a script constantly looking it up which would be intrusive and slow.

stilghar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #10 on: March 03, 2014, 10:51:38 am »
I don't understand...

What I'm proposing is to have in the inspector the possibility to set the layer and layer order of ONE SINGLE the static sprite batcher (everything in it will have that layer and layer order). As you say it is one renderer when committed and the layer setting is done per renderer, can't this be done simple using something like the following?

myStaticBatcher.renderer.sortingLayerName = "Background";

When you are in edit you will simply assign the layer and layer order to all the children sprites. Is there no possibility of only changing the layer and layer order of the children when you change the one of the batcher? Having something constantly checking I agree is very bad. Maybe in that case something manual can be implemented (similar to the commit)? A manual button saying "assign layers"?

Just throwing some ideas for a possible implementation.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #11 on: March 03, 2014, 11:04:08 am »
Yeah thats what I'm leaning towards. It would have to handle people setting the render layers on sprites internally while expanded, etc. Its just annoying that Unity doesn't let you inherit stuff like that. Eg. what happens when you create a new sprite internally? It will sort incorrectly until you assign the correct layer to it.

stilghar

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #12 on: March 03, 2014, 11:10:29 am »
Yeah thats what I'm leaning towards. It would have to handle people setting the render layers on sprites internally while expanded, etc. Its just annoying that Unity doesn't let you inherit stuff like that. Eg. what happens when you create a new sprite internally? It will sort incorrectly until you assign the correct layer to it.

Totally true, but it is at least better than not even having the option of using layers with the batchers as it is now...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Static sprite batcher not respecting layers properly?
« Reply #13 on: March 03, 2014, 11:28:32 am »
I will be adding the layer option probably in the next 2.4 beta. It really is quite simple to do that.

Having said that there isn't really a nice foolproof solution to this :( Maybe some obnoxious message boxes will do the job when someone tries to do something that isn't supported.
« Last Edit: March 03, 2014, 11:32:27 am by unikronsoftware »