Quantcast
Channel: Questions in topic: "ios"
Viewing all articles
Browse latest Browse all 4709

Changing view based on device

$
0
0
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("currentActivity")) { mWindowManager = activity.Call("getSystemService","window"); mConfig = activity.Call("getResources").Call("getConfiguration"); } } int lRotation = mWindowManager.Call("getDefaultDisplay").Call("getRotation"); int dOrientation = mConfig.Get("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!

Viewing all articles
Browse latest Browse all 4709

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>