Hello Guest

Author Topic: Creating a scalable overlay sprite  (Read 16138 times)

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Creating a scalable overlay sprite
« on: October 13, 2012, 09:51:45 am »
Hello everybody,

I would like to create a semi transparent overlay, that I can use (for example) when the game is paused.
So, I am wondering what would be the best approach for me to have such an overlay in a way that I can easily scale to different resolutions.

One approach I am considering, is to have a 1x1 semi transparent pixel sprite, and then scale it by changing its transform scale. This way I get a direct ratio, where a scale a 1024x768 will cover the entire screen.

I also noticed there is a scale property in the inspector of a tk2dSprite, along with Bake Scale that I never used. I never touch this area, as I am using the tk2dCamera.

Can anyone share some thoughts on that? I mean, does my approach make sense? Is there a better / more efficient / easier way?

Thanks in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #1 on: October 13, 2012, 03:43:33 pm »
I think the EASIEST way to do this is to create one sprite and change the scale property in the sprite collection editor - for example, if its 1x1, set the scale to 4096x4096 and the sprite will fill your screen. You won't need to change any scale at runtime. The pixels that get clipped won't really cost anything, so that could be a quick way to do this.




DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #2 on: October 13, 2012, 03:50:37 pm »
Oh - out of screen pixels do not cost anything? That's new to me.

Actually, what is the difference between changing the scale in the transform and changing it in the tk2dSprite inspector?

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #3 on: October 13, 2012, 04:05:44 pm »
Out of screen pixels get clipped before it gets to the rasterizer - and clipping 4 vertices should not cost anything at all - you clip 4 vertices all the time with half visible sprites, etc.

There are 3 places you can change scale.

1. In the sprite collection editor, select the sprite - there's a scale parameter there. This will scale the mesh geometry, the rest of the system is oblivious to this. Use case: you have a gradient sprite which should be 256x256, but you can get away with a 32x32 one scaled 8x. Think of it as the prefab for the sprite.

2. In the sprite inspector - this changes the geometry too, but it happens at runtime. This is here so you can always have a transform.localScale of 1,1,1. This changes geometry, so the cost of this is similar to animating a sprite. Changing this will never dynamic batching.
 
3. Transform.localScale - this changes the transformation matrix in Unity. The problem with this is you have to be very very careful not to break dynamic batching when using this. The rules in Unity are pretty bizzare (for good reason). Different parameters make it run through different codebases with different performance characteristics.

If you need a lot of sprites which need to scale dynamically, and option #2 is too slow, make sure ALL your sprites have non uniform scales always. Something like 1, 1, 1.0001 will do. If you have a hierarchy of sprites, just changing scale on the root object will suffice.


DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #4 on: October 13, 2012, 04:15:54 pm »
Wow, thanks a lot for the details.

I started losing you at "dynamic batching", but up until there you were golden. :)
So since I only need this for my semi transparent overlay, I will try method #1 or #2. 
Good to know I can use gradients as well.

When I used a single pixel, it seemed like somewhere along the way, someone converted it to be 2x2.

Also, I noticed that unless I tick the "Point Sampled" checkbox, the stretched result has a (nicely gradiented-) border - which is nice if it is the desired effect. So I am using point sampled, correct me if I am wrong.


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #5 on: October 13, 2012, 05:12:48 pm »
Always keep an eye on batch count. In game window, click on the little stats toolbar button. Lots of draw calls = bad. Lots batched = good :) The batched stuff is how many you're saving through dynamic batching.

Oh yeah, smallest texture size in Unity is 2x2, so you'll need to work with that.

To fix border, select sprite in sprite collection editor, set pad method to Extend.

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #6 on: October 13, 2012, 05:50:08 pm »
Thanks for the tips.

I see I have ~20 draw calls in my level, 0 batching no matter what I do.
Is there some Unity / tk2d setting I am missing?

Is 20 considered a lot?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #7 on: October 13, 2012, 06:11:40 pm »
20 draw calls is fine. Try creating 2 of the same sprite and position them next to each other. You should get the batched count = 2... Also open up the 1st sample scene - you should get a lot of batching there.

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #8 on: October 13, 2012, 06:14:04 pm »
Thanks.
Im doing just this test now, batching at 0
The draw calls number is not increased, but batching is definitely stubborn at 0.

Will try the test scene, but even if I see it does work, I do not know what is the difference between it and my setup.

EDIT:
Started a new project, imported tk2d and all test scenes show 0 batching.
I am testing this on Windows, in the editor, using Unity Free.


EDIT 3:
Well - if I go to player settings and disable Dynamic Batching, draw calls number jumps from 2 to 10 - so I guess it DOES do some batching, it just doesnt tell me about it in the "batching" value in the stats window.
Any clue why?
« Last Edit: October 13, 2012, 06:40:54 pm by DannyB »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #9 on: October 13, 2012, 07:01:03 pm »
What version of Unity are you using? That scene is DEFINITELY batching if its only showing 2 draw calls.
Also what platform are you targetting?

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #10 on: October 13, 2012, 07:39:11 pm »
Unity 3.5.6f4
Targeting either desktop or web player.
Yeah - it seems like it does batch, there are 2 draw calls instead of 10, but it doesnt show it in stats.

Should I take this to Unity Answers?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #11 on: October 13, 2012, 07:42:34 pm »
This is really bizzare. I've got the same version (tested on Windows, I'll check mac next time I'm on there). I get Draw Calls: 2 batched: 10 in both Desktop and web player.

I suggest testing on a clean new project to see what you get there...

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #12 on: October 13, 2012, 07:45:09 pm »
This is a clean new project, only with imported tk2d (the public version, not beta).
I am on Windows as well, so if you see something different, it is bizarre.

If you think of anything else, let me know.

Thanks for all the tips and help - I will post a Unity Answer thread and will cross-link it with this thread.

EDIT:
Seems like someone else started a thread on the same subject earlier today, so I am joining his thread.

EDIT 2:
When running on a Mac, I do see "batched" above zero, so the problem is not in the project but in Unity for Windows.
« Last Edit: October 14, 2012, 01:21:17 pm by DannyB »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Creating a scalable overlay sprite
« Reply #13 on: October 14, 2012, 05:01:13 pm »
Except it isn't broken for me, on the same version on Windows :)

DannyB

  • 2D Toolkit
  • Hero Member
  • *
  • Posts: 609
    • View Profile
    • Chicks Ahead
Re: Creating a scalable overlay sprite
« Reply #14 on: October 14, 2012, 05:23:13 pm »
Well, I have that animated GIF to prove I am not crazy.... :)