- Home /
Reading Screen Dimension in Android Immersive Mode
Been doing a lot of searching for this and yet to fix my issue. Basically when the phone is in immersive mode (which works fine) then I can't find a way of getting the true resolution and at a complete loss.
As an example I have an Xperia E1 that should be 480, 800 however all methods I know of reading the screen sizes return 480, 744 which is a huge problem for our game.
What I have tried:
Screen.currentResolution Screen.width and height Screen.resolutions (returns nothing as expected)
and even the fancier:
using(
AndroidJavaObject metricsInstance = new AndroidJavaObject("android.util.DisplayMetrics"),
activityInstance = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"),
windowManagerInstance = activityInstance.Call<AndroidJavaObject>("getWindowManager"),
displayInstance = windowManagerInstance.Call<AndroidJavaObject>("getDefaultDisplay")
)
{
displayInstance.Call("getMetrics", metricsInstance);
debugLabel.text += "Test: " + metricsInstance.Get<int>("heightPixels") + ", ";
debugLabel.text += metricsInstance.Get<int>("widthPixels");
}
But sadly all the methods return the 'wrong' resolution, 744 in this case. I have tried this on other 4.4 devices (at least 3) and they all have the same issue. If I manually set the resolution, in this case with height back to 800, everything works as I would like so I am extremely confused!
Anyone any ideas or suggestions? Suprised no one seems to be talking about this!
I am considering it but thought it best to post here first to see if anyone can shed light on this
Actually, I did some quick research and apparently the problem is with Android not Unity. Check this out:
http://stackoverflow.com/questions/20555083/android-gl-screen-size-in-immersive-mode
Your right this looks exactly like my issue thanks. When i was searching I skipped traditional Android stuff since I have never done natvie development and most of it means nothing to me :P
Still leaves me a bit stumped, anyone know if we can access things such as:
Display.getRealSize();
Guessing I would have to contact Unity for that though :(
Answer by LaireonGames · Mar 18, 2015 at 02:55 PM
Ok so figured out half of my issue. I can get the real size by calling this:
using(AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"), metricsClass = new AndroidJavaClass("android.util.DisplayMetrics"))
{
using(AndroidJavaObject metricsInstance = new AndroidJavaObject("android.util.DisplayMetrics"),
activityInstance = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity"),
windowManagerInstance = activityInstance.Call<AndroidJavaObject>("getWindowManager"),
displayInstance = windowManagerInstance.Call<AndroidJavaObject>("getDefaultDisplay"))
{
displayInstance.Call("getRealMetrics", metricsInstance);
currentAR = metricsInstance.Get<int>("widthPixels") / (float)metricsInstance.Get<int>("heightPixels");
}
}
And for 4.4 phone with immersive mode this works! :D But.... I have a phone (Xperia SP) thats 4.1.2 and it has an annoying bar at just the bottom so now need to figure out a way around that now..
P.S note that the change is to use "getRealMetrics" instead of just "getMetrics"
Edit Found how to distinguish between the resolutions now. Basically if the API is 19 or above emmersive mode is enabled. To find out the API you can run this:
string os = SystemInfo.operatingSystem;//this is the whole OS string with a big of info
int position = os.IndexOf("API-");//first grab the start of where we see this label
string numbers = os.Substring(position + 4, 2);//basically grab the next two characters which are the API numbers
int test = Int32.Parse(numbers);//now we have an API number
Android Immersive $$anonymous$$ode won't work on 4.1.2 because it's available since 4.4:
https://developer.android.com/training/system-ui/immersive.html
Yeah exactly so thats a separate issue now. Thus answer accepted to original problem :P