- Home /
IOS orientation loss on scene change
I have a menu scene that remains in landscape and another scene accessed from the menu scene using application.loadlevel() which can be all orientations.
I have attached the script below to a game object in both scenes with the flag set for landscape only for the menu and not set for the other scene and this works great on app start.
However if when in the second scene I am in portrait and return to the menu scene it displays the menu incorrectly as it remains in portrait rather than on menu load resetting to landscape only.
Am I doing this the wrong way?
Thanks
public class DeviceOrientation : MonoBehaviour {
public bool landscapeOnly;
void Awake () {
if (landscapeOnly) {
// scene remains in landscape only
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
}
else {
// scene allows auto rotation in all directions
Screen.orientation = ScreenOrientation.AutoRotation;
Screen.autorotateToLandscapeLeft = true;
Screen.autorotateToLandscapeRight = true;
Screen.autorotateToPortrait = true;
Screen.autorotateToPortraitUpsideDown = true;
}
}
}
Check out the solutions suggested in this post: http://blog.joachimbaur.de/?p=149
Thanks, but that is not the answer. I already have correct rotation and placement of background image and any gui elements when in the the menu scene. likewise the same is true for the second scene that allows full rotations.
The problem is navigating back to the menu scene the orientation code setting does not seem to apply
Answer by M-G-Production · Dec 04, 2014 at 07:04 PM
Hi elpuerco63!
Perhaps you could do so: this.DontDestroyOnLoad()
(you could change "this" with gameObject or whatever you want! "This" refers to the actual script)
(Read about it: http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html)
When a new scene is loaded, this script won't be destroyed and recreated.
Bests, Math
but surely if the menu scene is destroyed entirely when the next scene loads then is it not recreated anew when returning to it via application.loadlevel(menu) ?
And as such the orientation code should run?
I think you should give it a try! I'm pretty sure it will work.
And yes, of course it should work! Or perhaps try switching Awake to Start! This could do it!
Please try it and reply back because I need to know something about the Awake fonction.
Hi, I'm trying that but getting weird results where stuff from second scene remain alive and appear over the menu?
Continuing to test ....
ok no more weirdness, but the suggestion re dont destroy doesnt work...:-(
Answer by Owen-Reynolds · Dec 04, 2014 at 09:52 PM
This is from memory, from a "good enough" solution similar to this a year ago:
Setting orientation to AutoRotate only keys off of movement. It won't spin the screen now. I think you need to set orientation to a specific value, in order to force a change. Of course, then you need to set it back to autorotate, to enable the 180 flip.
Normally you don't have to worry about this, since the build settings force orientation to be legal on start-up, and most people never change autorotate. But, I think, for example, if you allowed any rotation in your build, and started the game end-up, the menu would be messed-up right away.
O$$anonymous$$ in Unity I have the project set to LandscapeLeft and this makes sure the menu scene appears only in landscape either left or right which is what I want.
When I load the second via the code runs to set rotation in all directions.
I have inserted this code in the menu scene start function which if second scene is portrait and returns to menu it does force orientation to landscapeLeft but it does not rotate to landscapeRight anymore and then if I then load second scene again the orientations are all to cock?
if (Input.deviceOrientation == UnityEngine.DeviceOrientation.Portrait ||
Input.deviceOrientation == UnityEngine.DeviceOrientation.PortraitUpsideDown) {
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
I think this is where I stopped in my tests -- the specs got changed so we would never want portrait anyway.
Seems like the sequence would be : set Screen.orient to Left, wait a little bit for iOS to notice that and start turning, set Screen.Orient back to autorotate (how long to wait? 1 frame? As long as it takes to spin?)
Your answer
Follow this Question
Related Questions
Game support for landscape and portrait 0 Answers
Xcode 6 Orientation Issue 0 Answers
Difference between Screen.orientation - Input.deviceOrientation 2 Answers
Wrong Orientation Detected After Application Pause 0 Answers
iOS8 Screen Orientation Bug?? 0 Answers