Hello Guest

Author Topic: Screen Rotation Problem  (Read 6920 times)

wiley.a

  • Newbie
  • *
  • Posts: 14
    • View Profile
Screen Rotation Problem
« on: November 01, 2012, 03:10:49 pm »
Hello

In the game I am working on we have both horizontal and vertical based levels. I want to be able to switch the camera's setup so that it displays horizontally on iOS when on a horizontal level but set it in portrait mode when on a vertical level. I am sure its simple to do this but I am obviously missing something. The horizontal setup works fine but that could be because most of the game is in horizontal. When the vertical code runs everything is rendered vertically (so portrait on the device) but it has massive pillar boxing going on. I'd post a screenshot but I can't, sorry. Hope I have explained it well enough.

Code: [Select]
public static void SwitchCameraTo(Orientation orientation)
{
// If the orientation is already correct, return early
if (orientation == GameController.CameraOrientation)
return;

GameController.CameraOrientation = orientation;

if (orientation == Orientation.Horizontal)
{
GameController.Camera.nativeResolutionWidth = 960;
GameController.Camera.nativeResolutionHeight = 640;

GameController.Camera.forceResolution = new Vector2(960, 640);

Screen.orientation = ScreenOrientation.LandscapeLeft;
}
else
{
GameController.Camera.nativeResolutionWidth = 640;
GameController.Camera.nativeResolutionHeight = 960;

GameController.Camera.forceResolution = new Vector2(640, 960);

Screen.orientation = ScreenOrientation.Portrait;
}
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Screen Rotation Problem
« Reply #1 on: November 02, 2012, 12:17:41 pm »
What do you want it to do exactly? Do you want to rotate the scene, or just change resolution?
Also forceResolution only works in the editor, so you'll be getting different behaviour on a real device for sure.

wiley.a

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Screen Rotation Problem
« Reply #2 on: November 02, 2012, 12:29:57 pm »
I am after changing the resolution with the device orientation, like the image below.



However, with the supplied code I am actually getting this:


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Screen Rotation Problem
« Reply #3 on: November 02, 2012, 12:38:41 pm »
In tk2dCamera.UpdateCameraMatrix

add these lines after
Code: [Select]
float pixelWidth = mainCamera.pixelWidth;
float pixelHeight = mainCamera.pixelHeight;
                  Debug.Log(pixelWidth.ToString() + " " + pixelHeight.ToString());

Those will be the resolutions its dealing with there.
What kind of overrides do you have set up?

wiley.a

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Screen Rotation Problem
« Reply #4 on: November 02, 2012, 12:46:44 pm »
On the main setup which is horizontal I am getting 796.5x530.6667 which when upscaled comes to 960x640 whcih is right. When I switch it to vertical using the supplied code I get 354.2224x531 which doesn't seem right to me despsite looking right in the editor (but not on the iOS device).

I have one override setup which is:

Name: Wildercard Override
Width: -1
Height: -1
Auto Scale: Fit Visible
Fit Mode: Center

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Screen Rotation Problem
« Reply #5 on: November 02, 2012, 12:57:24 pm »
Ahaa.. Thats one of those wonderful "features" in Unity, where if your window isn't big enough to display the full resolution it will scale it down. Fine for 3d, not so fine for pixel perfect 2D. The resolution stuff will be correct on the device.

I'll look into this for you.

wiley.a

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Screen Rotation Problem
« Reply #6 on: November 02, 2012, 01:06:39 pm »
The resolution stuff will be correct on the device.

The problem is it isn't. Its fine in the editor though.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Screen Rotation Problem
« Reply #7 on: November 02, 2012, 01:08:27 pm »
You could make the force resolution work on the device to see if that fixes it for you. In tk2dCamera.cs, UpdateCameraMatrix() there is the line:

#if UNITY_EDITOR
  if (forceResolutionInEditor)
...
#endif

define that in and it should do it on the device too.

wiley.a

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Screen Rotation Problem
« Reply #8 on: November 02, 2012, 05:37:39 pm »
I tried that and while the height is now right, the width appears wrong. I've swapped the values so they should be right but I must be missing something. I now get the image below but without the sections labelled "Other stuff".


wiley.a

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Screen Rotation Problem
« Reply #9 on: November 02, 2012, 05:50:52 pm »
I think the black boarders are now my problem with some pillarboxing/letterboxing I am doing. Completely forgot about that. Thanks for your help