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 /
avatar image
0
Question by aman_jha · Aug 23, 2015 at 03:14 AM · c#androidjavascriptiosdevice

Changing view based on device

Hi Unitarians!

I've got a pretty classic problem for app makers, but its not often seen by game developers. My issue is to detect and respond to if the user is using a tablet or a phone for their Android device. If they have a phone, I want them to see my app in portrait mode. If they have a tablet, I want them to see a landscape mode. I want it locked in either landscape or portrait depending on their device.

I have seen ideas so far. First would be to let the user choose their own orientation through auto rotation, and I would display my view depending on what they have. My problem is that I want my landscape view ONLY for tablets and my portrait view ONLY for phones. My users can't choose.

My second solution is this script: using UnityEngine; using System.Collections;

 public class NaturalOrientation : MonoBehaviour {
     
     public static int ORIENTATION_UNDEFINED = 0x00000000;
     public static int ORIENTATION_PORTRAIT = 0x00000001;
     public static int ORIENTATION_LANDSCAPE = 0x00000002;
     
     public static int ROTATION_0     = 0x00000000;
     public static int ROTATION_180     = 0x00000002;
     public static int ROTATION_270     = 0x00000003;
     public static int ROTATION_90     = 0x00000001;
     
     public static int PORTRAIT = 0;
     public static int PORTRAIT_UPSIDEDOWN = 1;
     public static int LANDSCAPE = 2;
     public static int LANDSCAPE_LEFT = 3;
     
 #if UNITY_ANDROID
 
         AndroidJavaObject mConfig;
     AndroidJavaObject mWindowManager;
     
     //adapted from http://stackoverflow.com/questions/4553650/how-to-check-device-natural-default-orientation-on-android-i-e-get-landscape/4555528#4555528
     public int GetDeviceDefaultOrientation()
     {
         if ((mWindowManager == null) || (mConfig == null))
         {
             using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").
                             GetStatic<AndroidJavaObject>("currentActivity"))
             {
                 mWindowManager = activity.Call<AndroidJavaObject>("getSystemService","window");
                 mConfig = activity.Call<AndroidJavaObject>("getResources").Call<AndroidJavaObject>("getConfiguration");
             }
         }
         
         int lRotation = mWindowManager.Call<AndroidJavaObject>("getDefaultDisplay").Call<int>("getRotation");
         int dOrientation = mConfig.Get<int>("orientation");
 
         if( (((lRotation == ROTATION_0) || (lRotation == ROTATION_180)) && (dOrientation == ORIENTATION_LANDSCAPE)) ||
         (((lRotation == ROTATION_90) || (lRotation == ROTATION_270)) && (dOrientation == ORIENTATION_PORTRAIT))) {
               return(LANDSCAPE); //TABLET
           }     
           return (PORTRAIT); //PHONE
     } 
 #endif

Essentially the script detects if the user is using a tablet or a phone through their "natural orientation", which is conveniently landscape for tablet and portrait for phone. A comment on the page where I got the code from however, says "I've seen getRotation() and orientation go out of sync sometimes, so this isn't a perfect solution." This means that this isn't a solution either.

So, how would I detect if my user is using a tablet or phone, and how would I be able to change my UI based on that?

Thanks!

Comment
Add comment · Show 7
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 Umresh · Aug 24, 2015 at 12:19 PM 1
Share

http://forum.unity3d.com/threads/reliable-way-to-detect-tablet-on-android.127184/ Check this thread

avatar image JohnnySunshine · Aug 24, 2015 at 12:30 PM 1
Share

I'd probably solve this by uploading two different builds and differentiating them using Play Store filters.

avatar image aman_jha · Aug 24, 2015 at 04:18 PM 0
Share

@JohnnySunshine I didn't know the Play Store could do that. That sounds like a great idea. Does Apple do this too? and Windows?

avatar image JohnnySunshine · Aug 24, 2015 at 06:31 PM 0
Share

Sorry, I only have direct experience with the Play Store :( I vaguely remember that the App Store has some iPad-only apps, so you might have something there, but I have absolutely no idea about Windows apps. I assumed you were writing an Android app based on your code, but if you need a cross-platform solution, I would look into Screen.dpi and the device's resolution or something like that.

avatar image aman_jha · Aug 24, 2015 at 06:46 PM 0
Share

@JohnnySunshine no it's fine! For this singular Unity project, I am only using Android. I learned a while back that if I wanted to build for other platforms, I should simply copy paste the project and make separate projects for separate platforms. That being said, right now I only want android, so your idea works great. For other platforms, I'll use a different solution. If you want to post your idea as an answer, I'd be happy to accept it.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

28 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

Related Questions

How can I get the WiFis name? 1 Answer

C++ windows DLL and Android? 1 Answer

Multiple Tiled GPU warning: RenderTexture color surface errors? 0 Answers

Dim Screen but Don't Lock 0 Answers

How to create a shortcut automatically in android 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