Detect device and resize GUI accordingly?

Hi everyone,

So recently I started upgrading some textures for my game that was originally targeted for the low resolution iPads (1024 X 768) but now with all the new models (2048 X 1536) and the retina display I want to make my game work with both resolutions.
First I though I could simply resize all my textures to fit the new devices (2048 X 1536) but when I run the game on my iPad Mini First Gen, everything looks huge. I thought that enabling native device resolution would do the trick and crunch the Gui textures like I read on the forums but for some reason it doesn’t. So I decided to write a script that checks for device generation and sets the desired resolution, I checked some posts and found some code and combined it with screen.resolution, the problem is when I run the game now it just stays in loading screen.

//Ipad Generations
 var iOSGen = iPhone.generation;

function Start () {
 
    if (Debug.isDebugBuild) {
        Debug.Log("iPhone.generation     : " + iPhone.generation);
        Debug.Log("SystemInfo.deviceType : " + SystemInfo.deviceType);
        Debug.Log("SystemInfo.deviceModel: " + SystemInfo.deviceModel);
    }
   
   	//Ipad 1/2/Mini1 - No retina
    	
    if (iOSGen == iPhoneGeneration.iPad1Gen) { 
        Screen.SetResolution (1024, 768, true);
    } else if (iOSGen == iPhoneGeneration.iPad2Gen) {  
       Screen.SetResolution (1024, 768, true);
    } else if (iOSGen == iPhoneGeneration.iPadMini1Gen) {  
       Screen.SetResolution (1024, 768, true);
       
    //Ipad 3/4/Mini2/Air - Retina
       
    } else if (iOSGen == iPhoneGeneration.iPad3Gen) { 
        Screen.SetResolution (2048, 1536, true);
    } else if (iOSGen == iPhoneGeneration.iPad4Gen) { 
       Screen.SetResolution (2048, 1536, true);
    } else if (iOSGen == iPhoneGeneration.iPadMini2Gen) { 
       Screen.SetResolution (2048, 1536, true);   
    } else if (iOSGen == iPhoneGeneration.iPad5Gen) { 
       Screen.SetResolution (2048, 1536, true);
       
       } else {
   
        if (Debug.isDebugBuild) { Debug.LogWarning("Can't detect iOS device type."); }

}
}

Any idea why this is not working? or a proper approach to make the new textures fit the screen on older iPad generations?

Thanks in advance!

At least for mobile platforms, it’s better to detect the current screen width and height in order to adjust the size of items so they look good. Using Screen.SetResolution could cause some issues on mobile games and is more for use with desktop platforms.

In order to resize a GUITexture that is being drawn in OnGUI, it’s common to use the GUI.matrix property. There is a good answer I’ve linked here that has a more detailed explanation:
Script gui units

Based on the target screen size of 1024x768, the code will look something like this…

void OnGUI ()
{
	float horizScale = Screen.width / 1024f;
	float vertScale = Screen.height / 768f;
	Matrix4x4 origMat = GUI.matrix;
	GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, new Vector3 (horizScale, vertScale, 1));
	/* Your GUI code here */
	GUI.matrix = origMat;
}

Another option is to attach a GUITexture to a GameObject, then it’s unnecessary to use GUI.matrix as it will automatically resize. In this case, the GUITexture size is defined as a percentage of the view area.

It’s worth mentioning Unity’s new UI while on the topic. Unity 4.6 is now out of beta and along with additional features the new UI tries to play nice with multiple resolutions. Not perfect, but there are a number of benefits over the legacy GUI. Here is something from the manual: Designing UI for Multiple Resolutions

Update: In regards to your answer… in case you decide to enlarge the graphics to target the Retina resolution, you could try the code below. So long as you keep the same screen orientation and only use iPad, then it will scale down the GUI to half size so it fits non-Retina displays.

void Start ()
{
	if (Screen.dpi < 250) {
		GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, new Vector3 (0.5f, 0.5f, 1));
	}
}

Ok so for anyone struggling with something similar, I ended up solving the problem in a much simpler way, I resized the textures back to the non retina size (1024 X 768) then used this very simple script to check if the device is retina or non retina:

function Start () {

//If we have a retina device, change the screen resolution to match 
if(Screen.dpi > 250){ 

Screen.SetResolution (1024,768,true);

}
}

It simply checks for the device dpi and resizes according to it. A non retina iPad has 132 dots per inch, a
new iPad with retina display has 264. This also can be done the other way around.

Hi Noah1!

About the textures I can’t really help you… But to detect the size of any display, use: Screen.width and Screen.height.

Don’t forget they are “read only” and they will tell you the absolute dimension off the screen wich runs the game.

Math