Author Topic: Buttons become unresponsive when programmatically changing screen orientation  (Read 15865 times)

DZP

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 17
    • View Profile
Hey,

I'm on NGUI 3.5.5 with Unity 4.5.1 (EDITED FROM 5.1  ::) ) and I'm trying to programmatically change the screen orientation of my iOS device (iPad iOS 7.1) for different parts of my game. The buttons are responsive and work until I change the orientation using for example
  1. Screen.orientation = ScreenOrientation.Portrait;

The buttons work with the rotation on Android (tested on a Nexus 5 4.2.2).

Once I do that, the buttons can no longer be clicked. I see the code in UICamera for detecting screen changes gets executed, so the UIAnchors must be getting updated as well. What could be preventing my buttons from being responsive again?

Thanks.
« Last Edit: June 30, 2014, 02:32:55 AM by DZP »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Unity 5.1? Can I have a copy? I've always wanted to have something from 2 years in the future. :)

Your NGUI is quite a bit out of date, so I would start by updating to the latest version. After that, enable the Debug option on the UICamera so that you can see what the touch is interacting with. Beyond that, I'm not sure -- you will need to add some Debug.Logs there. I suggest covering your entire screen with a big collider and logging the UICamera.lastTouchPosition via NGUIDebug.Log in OnDrag.

DZP

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 17
    • View Profile
Unity 5.1? Can I have a copy? I've always wanted to have something from 2 years in the future. :)

Your NGUI is quite a bit out of date, so I would start by updating to the latest version. After that, enable the Debug option on the UICamera so that you can see what the touch is interacting with. Beyond that, I'm not sure -- you will need to add some Debug.Logs there. I suggest covering your entire screen with a big collider and logging the UICamera.lastTouchPosition via NGUIDebug.Log in OnDrag.

Ha! I wish :(

I did as you asked after updating and was able to see the touch locations prior to the orientation change, but as soon as the orientation changed then the huge collider I put in the background to receive the touches no longer did anything. That was on iOS. On Android the huge collider continued to spit out values for me after the orientation change.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Well, from NGUI's point of view, all it does is performs a raycast into the screen using physics (2D or 3D, depending on your UI type). Have you tried switching your UI colliders from 2D to 3D or vice versa via NGUI -> Extras menu? (make sure you have the latest NGUI first)

bfriedman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
FYI, I have this exact problem as well.  Unity 4.5.1p3,  NGUI just updated via asset store, so I guess 3.6.6?  In my case, NGUI works perfectly fine letting Unity auto-rotate the GUI.  But when I programmatically change to:

  1.                 Screen.orientation = ScreenOrientation.Landscape;

During normal user input handling, I get no more events from the UICamera when running in iOS.

So NGUI updates the layout to the new orientation.  But events stop flowing to it.  I do get them when running in Android.  Everything seems fine there.

Anecdotally, the gyro input that I'm running in parallel (outside of the NGUI system) continues to work fine.  So Unity itself isn't frozen.  It's just the UI events that are not flowing.

I'm about to dig into this.  But thought I'd confirm it as best I can.

bfriedman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
So upon doing some debugging on the device (iPad), I find that after the screen rotation, Unity is reporting touch positions as negative in x.  If I break inside UICamera.ProcessTouhces and look at the touch returned by calling Unity's "Input.GetTouch(i)", and watch it's position and rawPosition fields, I get negatives.  Which I believe is a Unity bug.  Though I suppose it could instead be intended behavior I don't understand.  I'll be moving investigation in that direction.

Anyhow, as far as NGUI goes:  Later on in NGUI's logic, it skips touches outside the 0-1 range.  Which is correct behavior I believe.

bfriedman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
I've isolated the problem as a unity issue and built a test project to show it.  I submitted it to Unity as a bug.  Will update if I get a tracking number or response.

-brad

bfriedman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 4
    • View Profile
FYI a workaround for me is to toggle between this two modes:

  1.                 Screen.orientation = Screen.orientation;
and
  1.                 Screen.orientation = ScreenOrientation.AutoRotation;

That moves between freezing the orientation, and setting back to auto.  That may not work for everyone.  My particular use-case simply requires temporarily disabling auto-rotation while my gyro mode is active.  I don't care if it's in portrait or landscape when that happens.  Then I restore it to auto when my gyro mode is deactivated.

Whatever is wrong with Unity's orientation changing logic, moving between these two modes doesn't cause errant behavior at least.

fanling3

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 29
    • View Profile
I can confirm that my game has the same problem on iOS (no problem on Android).
I felt strange that a number of people wrote reviews on app store reporting they are unable to play my game and gave my game 1 star, and my colleague accidently discover that it is caused by rotating the device to another orientation

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Thanks for investigating it bfriedman, and for submitting a bug to Unity to boot.

DZP

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 17
    • View Profile
Thanks bfriedman, your solution works for me. It sucks that I have to give the device a jiggle before the device auto rotates, but it is better than nothing until a solution is found!

morgue

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Apparently this has been fixed for Unity 4.3 (not yet available at the time of this posting).

I want it now :(

garing

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 1
    • View Profile
For those still struggling with this issue, the bug was fixed in 4.5.2p3, available here: http://forum.unity3d.com/threads/unity-patch-releases.246198/#post-1718700

huixiong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
FYI a workaround for me is to toggle between this two modes:

  1.                 Screen.orientation = Screen.orientation;
and
  1.                 Screen.orientation = ScreenOrientation.AutoRotation;

That moves between freezing the orientation, and setting back to auto.  That may not work for everyone.  My particular use-case simply requires temporarily disabling auto-rotation while my gyro mode is active.  I don't care if it's in portrait or landscape when that happens.  Then I restore it to auto when my gyro mode is deactivated.

Whatever is wrong with Unity's orientation changing logic, moving between these two modes doesn't cause errant behavior at least.

我是Unity4.5.5,当我在IOS平台调用相册,这时候游戏并未进入后台,切换回来后屏幕就不能点击了