Hello Guest

Author Topic: tk2dCamera Preview does not match game view  (Read 11606 times)

etoreo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
tk2dCamera Preview does not match game view
« on: May 03, 2012, 10:12:34 pm »
I have set up a few background sprites and am trying to get my camera to behave correctly.  The problem I am running into is that the preview that the tk2dCamera is giving me is not what I see when I hit the Unity "play" button to test out my scene.  When I am in play mode, I see a blue background and tiny little sprites in the lower left had side of the screen.  These sprites should take up the entire screen and then some.  Why does my game differ from the "Camera Preview" shown in Scene mode?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #1 on: May 04, 2012, 09:36:04 am »
This is a bug in Unity. The camera preview window simply uses the camera parameters (orthosize, etc) to draw the preview and as such will incorrectly draw any custom projection matrices - the tk2dCamera uses a custom projection matrix. I'll be sending Unity a repro case, in the hope that they will be able to fix it.

The only way right now to correctly view the output is in the Game window.


Note: I was going to issue a fix which overwrite this preview window with a custom one, but it requires rendertargets and will only work on Unity Pro - as such I've scrapped that option for now.

etoreo

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #2 on: May 04, 2012, 07:30:33 pm »
Thanks for the reply on this.  I would be interested in any updates you get from Unity.

outtoplay

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #3 on: May 05, 2012, 12:44:08 am »
So... I can't see my Sprite at all in the Game view, only the Scene View with the tk2D camera.  that can't possibly be right, right?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #4 on: May 05, 2012, 01:04:23 am »
No, it means that the camera preview and the camera bounds in scene view are wrong. If you move your sprite to 0,0,0 you should see it in game view. Only game view shows you what you're going to see on your target.

Check demo 11 (I think). It should make a bit more sense after you've looked at how it works in there.

outtoplay

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #5 on: May 05, 2012, 02:14:16 am »
Looked at Demo 11, surprisingly little help (it is nice looking though).

This makes no sense:
I create a cube at 0,0,0, then create a regular Unity camera(Ortho set the Size to 1 beneath Projection)...poof the cube is in the Scene view, Game view and the little camera preview.

I create a tk2D camera (Ortho by default), set the Size to 1 (from it's default of 480) and the cube is there (but only in the Scene view and little Camera preview.  How/why would this work like this?

I did get the Animated Sprite bird to show up (I turned on the 'use tk2d camera' in his settings under the collection), but he shows up HUGE in the Game view, unless I turn on 'Force Resolution in Editor',(see attached) but then he shows up in the lower left hand corner, where this thread began.  (I can send the file if you like).

Here's what I want to do... Create a game, that works in Landscape on iPad 1&2 at 1024x768  and iPad3 at 2048x1536. If it worked with iPhone4 as well at 960x640 that would be terrific. If you could tell me how to achieve this, I'd be a very happy customer.  I love the 2d toolkit, I'm just not sure how to achieve the above.

Thanks.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #6 on: May 05, 2012, 10:05:09 am »
OK, perhaps the documentation isn't as clear as it could be on these things.

First things first, you don't need tk2dCamera to get a game to work in Landscape on iPad like you wanted. You don't even need it for the iPhone4 scenario. A simple quick walkthrough to get you started.
1. Create a (normal) camera, pick an ortho size, and select the correct resolution in the Game window drop down. Make sure the camera is at 0, 0, -10 or something like that.
2. Edit your sprite collection, untick "Use tk2dCamera" in settings. Now, enter Target height = 768, Target Ortho Size = the ortho size you selected above.
3. Commit.
4. Create a sprite from this collection in the scene and set the position to 0,0,0. It should appear at the correct size.

Now if you ran this on iPad3, everything would be the correct sizes, albeit scaled up 2x. So if you worked the other way, and started off with target height = 1536, and ortho size = 13.578, everything would still work fine as long as the sprites in #2 had the same values in there. Hope that makes sense so far, and thats pretty much all there is to how to get correctly sized sprites in 2D Toolkit.


Now that thats out of the way, I'll explain how the tk2dCamera works.

tk2dCamera is meant to solve a totally different problem - in this case, it is ideal for UIs where you'd like to position things by pixel offsets rather than arbitrary units as you would get in the method above. tk2dCamera creates a custom projection matrix, and the origin of the camera (0,0) is at the bottom left of the screen. Each world unit here is 1 pixel, so if you'd like to position a sprite 30,70 pixels in, you simply set the position to 30, 70. (Thats why everything is so big in the viewport)

Unfortunately, there is a bug in the current version of Unity, where the "Camera Preview" box in scene view is wrong and completely ignores the custom projection matrix. As a result of this, what you see in there is wrong. Unity also ignores the custom matrix when drawing the "Camera bounds" box. This is where/how most of the confusion arises.

Anyway, what the tk2dCamera always tries to to, is resize itself correctly to make 1 unit = 1 pixel. This happens on any platform, so if you ran the game at 960x640, you'll see that the sprites are still pixel perfect. So if your game window is really small, you'll end up with hideous huge sprites. If you're targetting a particular resolution, your best bet is to click "Force resolution in Editor", and type that resolution in. This will override the logic so it assumes you know what you're doing and displays the sprites as though they would appear in that resolution, regardless of the size of the game window. 1 world unit is still 1 pixel on the target, but may not be 1 pixel in the game view if its stretched / squished.

Hope that makes sense so far, if you have any further questions, dont hesitate to ask.

Cheers,
unikron

outtoplay

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: tk2dCamera Preview does not match game view
« Reply #7 on: May 06, 2012, 12:29:52 am »
Thanks Unikron... very helpful.  I will experiment with this and am sure with this great explanation, it should be pretty clear.

Best,
B.