- Home /
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!
I'd probably solve this by uploading two different builds and differentiating them using Play Store filters.
@JohnnySunshine I didn't know the Play Store could do that. That sounds like a great idea. Does Apple do this too? and Windows?
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.
@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.
Your answer
Follow this Question
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