Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ben.aten · Nov 11, 2015 at 08:01 AM · screenorientationlandscapeportraitautorotation

screen auto rotation forth and back

Hi,

I am developing a story application with games & story in it. In that one of the game developed as portrait and others are in landscape orientation. So when i select that particular game screen needs to auto rotated to portrait even we hold the device as landscape and when exit fro that game it needs to be autorotated to landscape even the device held as portrait. I did scripting for this, but its not automatically rotating, if i rotate the device to those desired orientation, its working.

//To Portrait from Landscape

void Start () {

   if (Input.deviceOrientation == DeviceOrientation.LandscapeRight)
   {
          Screen.orientation = ScreenOrientation.AutoRotation;
          Screen.autorotateToPortrait = true;
          Screen.orientation = ScreenOrientation.Portrait;
   }
   else if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
   {
         Screen.orientation = ScreenOrientation.AutoRotation;
         Screen.autorotateToPortraitUpsideDown = true;
         Screen.orientation = ScreenOrientation.PortraitUpsideDown;
   }
   else
   {
        Screen.orientation = ScreenOrientation.AutoRotation;
        Screen.autorotateToPortrait = true;
        Screen.orientation = ScreenOrientation.Portrait;
   }

    Screen.autorotateToLandscapeLeft = false;
     Screen.autorotateToLandscapeRight = false;

}

//To Landscape from Portrait

void Start () {

   if (Input.deviceOrientation == DeviceOrientation.Portrait)
   {
          Screen.orientation = ScreenOrientation.AutoRotation;
          Screen.autorotateToLandscapeRight = true;
          Screen.orientation = ScreenOrientation.LandscapeRight;
   }
   else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)
   {
         Screen.orientation = ScreenOrientation.AutoRotation;
         Screen.autorotateToLandscapeLeft = true;
         Screen.orientation = ScreenOrientation.LandscapeLeft;
   }
   else 
   {
        Screen.orientation = ScreenOrientation.AutoRotation;
        Screen.autorotateToLandscapeRight = true;
        Screen.orientation = ScreenOrientation.LandscapeRight;
   }

    Screen.autorotateToPortrait = false;
    Screen.autorotateToPortraitUpsideDown = false;

}

Please let me know is any other way or any scripting changes i have to made to make things possible.

Regards, Ben

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AhadKhan98 · Nov 11, 2015 at 08:55 AM

If you want it to lock it to a only portrait mode then add a gameobject, attach a script and write the following code in the start function.

 Screen.orientation = ScreenOrientation.Portrait;
         Screen.autorotateToLandscapeLeft = false;
         Screen.autorotateToLandscapeRight = false;
         Screen.autorotateToPortrait = true;
         Screen.autorotateToPortraitUpsideDown = true;
         Screen.orientation = ScreenOrientation.AutoRotation;

And if you want to set a specific scene to only landscape then use this code on the other scene.

 Screen.orientation = ScreenOrientation.LandscapeLeft;
         Screen.autorotateToLandscapeLeft = true;
         Screen.autorotateToLandscapeRight = true;
         Screen.autorotateToPortrait = false;
         Screen.autorotateToPortraitUpsideDown = false;
         Screen.orientation = ScreenOrientation.AutoRotation;


Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AhadKhan98 · Nov 11, 2015 at 08:57 AM 0
Share

Just create two separate scripts for example, ScreenAdjustmentPotrait and ScreenAdjustmentLandscape and use them accordingly.

avatar image ben.aten AhadKhan98 · Nov 11, 2015 at 09:41 AM 0
Share

Thanks for the reply. I will check and share the details. Is there any way to set these things in unity editor widow, means when i open the scene with portrait, game window will be at Tall (768 x 1024) and when i play landscape game scene from there it should automatically switch to wide (1024 x 768)

avatar image AhadKhan98 ben.aten · Nov 11, 2015 at 04:20 PM 0
Share

I don't think you can set unity editor to switch automatically.

avatar image ben.aten AhadKhan98 · Nov 12, 2015 at 04:47 AM 0
Share

Hi, The script mentioned above is not working for the first time when we play portrait scene from landscape scene, we need to rotate the device to make it portrait. And when we switch back to landscape from portrait, its still in portrait mode again have to rotate the device to make it landscape. But for the second time when we play portrait scene from landscape its working, but back to landscape from portrait is not. Is any other solution for this. Is we need to specify for iOS & android.

avatar image Goa ben.aten · Jun 14, 2016 at 02:09 PM 0
Share

Hi, did you manage to fix this? I have the same exact problem (screen resolution not changing when I set screen.orientation, but it works I run the android app 2nd time)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObjects in same size for any orientation 0 Answers

ScreenOrientation.PortraitUpsideDown not working 1 Answer

How can I make only one scene landspace? other ones are portrait 0 Answers

Switch to Tall & Wide game view in editor 0 Answers

Force scene to be LandscapeLeft or LandscapeRight 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges