Hello Guest

Author Topic: Dynamically set tk2dCamera resolutionOverride in code  (Read 8438 times)

theremin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Dynamically set tk2dCamera resolutionOverride in code
« on: November 24, 2012, 06:16:28 pm »
I haven't updated 2D Toolkit in a while, and I recently decided to update to 1.80 patch 3. I noticed that camera scaling (resolution override) seems to work quite differently now.

I used to handle camera scaling dynamically in code, and that worked really well for me, but my old script doesn't work anymore. This is the script that I used to put on my tk2dCamera object, in the Start() method:

Code: [Select]
tk2dCamera.inst.resolutionOverride = new tk2dCameraResolutionOverride[1];
tk2dCamera.inst.resolutionOverride[0] = new tk2dCameraResolutionOverride();
tk2dCamera.inst.resolutionOverride[0].width = Screen.width;
tk2dCamera.inst.resolutionOverride[0].height = Screen.height;

if (Screen.height >= 1280)
    tk2dCamera.inst.resolutionOverride[0].scale = 4f;
else if (Screen.height >= 640)
    tk2dCamera.inst.resolutionOverride[0].scale = 2f;
else
    tk2dCamera.inst.resolutionOverride[0].scale = 1f;

tk2dCamera.inst.UpdateCameraMatrix();

I'm not interested in using 2D Toolkit's new automatic scaling ? it doesn't allow me to fine tune my scaling enough, and I want pixel perfect scaling that's always a factor of 2 ? but, again, my old script isn't working anymore.

How can I manually set the resolutionOverride scale value with this new version of 2D Toolkit?

theremin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Dynamically set tk2dCamera resolutionOverride in code
« Reply #1 on: November 24, 2012, 08:16:22 pm »
Update: I just discovered that if I enable the Resolution Overrides checkbox in the Inspector, then I can use the code above and my dynamic scaling seems to work as intended. I think that solves it, but let me know if there's anything else I should know. Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamically set tk2dCamera resolutionOverride in code
« Reply #2 on: November 24, 2012, 08:19:14 pm »
Thats more or less it. The tk2dCameraResolutionOverride class initializers set it up like it used to behave.
Is all you're doing clamping it to power of 2 scales?
I could just put in a mode for that in there, should only be a couple of lines.

theremin

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Dynamically set tk2dCamera resolutionOverride in code
« Reply #3 on: November 25, 2012, 11:02:40 pm »
Yeah, I'm clamping to power of 2 scales ? it's the only way to do the scaling since I want uniform pixel perfect sprites. But I have very specific scale thresholds in mind so that my game looks the way I want it to on phones, tablets, and monitors. I imagine not everyone would want to use the same scaling ranges that I'm using, so it might not be as easy as adding a simple power of 2 scaling option. If the power of 2 scaling option was automatic, how would you determine the scale thresholds?

Ultimately, I think the Resolution Override system might be a bit more powerful if it used resolution ranges instead of specific resolutions. That way we could do stuff like I'm doing in my code, e.g., less than 640px height, then scale=1; less than 1280px height, then scale=2, etc. But that might just be best left to individuals to add whatever code they want, so they can scale their games however they want. For me, as long as code snippets like the one I'm using above continue to work, I'm totally happy :)

Thanks for your help!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dynamically set tk2dCamera resolutionOverride in code
« Reply #4 on: November 26, 2012, 12:27:42 am »
Hello again!

So what I did was I added a power of 2 scale directly, and a new auto scale mode called "Pixel Perfect Fit". This will scale the source image to the closest power of two scale to fit the screen.

With an override width & height of -1 each, which will match any resolution - you should always get pixel perfect scaling. I.e. 2x, 4x, 0.5x, and so on. Not quite sure about 0.5x but there might be situations it could be useful in... Will be in the next version.