Fit GUI to various android screen resolutions and densities

Is there a way to automatically get a game made with Unity to fit to the numerous Android screen resolutions and densities?
As for iOS we could just use a list with the various resolutions but as for Android there are just so many devices. It’s impossible to create a list like that.

Thanks for any help in advance.

GUI.Button(new Rect(Screen.width * (1f/6.55f),Screen.height * (0.1f/6.3f),Screen.width * (4.8f/6.55f), Screen.height * (0.85f/6.3f)),“Click”); //c#

 GUI.Button(Rect(Screen.width * (1f/6.55f),Screen.height * (0.1f/6.3f),Screen.width * (4.8f/6.55f), Screen.height * (0.85f/6.3f)),"Click"); //java script

Until 3.5b, you couldn’t get the physical size of the screen for choosing good layout options. Now you can. I think it best to have at least 2 layouts you can choose from, based on resolution and screen size (DPI). Meaning, a small device with a low res screen will look better if you gear it toward that, but a small device with a high-res screen will look unuasable where that same layout would be just fine on a tablet screen. So you should have some to choose from.

For earlier Unity versions;

You can scale the gui with GUI.matrix. If you design the GUI for the lowest supported resolution, you can then scale up the GUI for the other resolutions, given they are of the same aspect ratio to not get a squashed look. If you plan on supporting different aspect ratios, then make designs for the lowest resolution aspect ratios. Obviously, it won’t produce high end graphics for those with bigger resolution since it’ll just scale up the graphics, but the elements will occupy the same amount of screen space.

Hi guys,

I use this kind of trick to create a cross-platform solution.

using UnityEngine;
using System.Collections;

public class Singleton : MonoBehaviour {

    private static Singleton instance = null;
	public static float denistyScale;
	
    public static Singleton Instance {
        get { return instance; }
    }
	
    void Awake() {
        if (instance != null && instance != this) {
            Destroy(this.gameObject);
            return;
        } else {
            instance = this;
        }
	    Singleton.denistyScale = Screen.dpi / 160;
        DontDestroyOnLoad(this.gameObject);
    }
}

Just multiply everything you want “as-is” on multiple densities by Singleton.densityScale.
Example usage:

score.fontSize = 20 * Singleton.denistyScale;

Tested on:

  • iPad (Retina display)
  • Some androids

the best way to fit the gui texture on every screen is :

Try the follwing this…its works for every screen aspect ratio even for both ios android.

Steps:

1.Create a GUITexture.
2.In GUITexture parameter keep pixel inset to 0.(for everything).
3.Adjust the position and scale value in in Transform inspector.
4.then
use this script

using UnityEngine;
using System.Collections;

public class GUI_Controller : MonoBehaviour
{
public Vector2 scaleOnRatio1 = new Vector2(0.1f, 0.1f);
private Transform myTrans;
private float widthHeightRatio;

void Start () 
{
	myTrans = transform;
	SetScale();
}

void SetScale()
{
	//aspect ratio
	widthHeightRatio = (float)Screen.width/Screen.height;
	
	//Apply the scale. We only calculate y since our aspect ratio is x (width) authoritative: width/height (x/y)
	myTrans.localScale = new Vector3 (scaleOnRatio1.x, widthHeightRatio * scaleOnRatio1.y, 1);
}

}

5.Adjust the scale ratio in script transform…
6.TA ta i…i will works like boss…

thank u…

Try this in .js

var CalculatePositionForNewScreen:boolean=false;
var CalculateScaleForNewScreen:boolean=false;
var StartResolutions:Vector2;
var OffsetX:float;
var OffsetY:float;
var FindOffsetStart:boolean;
private var Can:boolean;


function Start () {
if(FindOffsetStart){
FindOffset();
}else{
Can=true;
}
}
private var XFaz:float;
private var YFaz:float;

function Update () {
var Widht=GetComponent.<GUITexture>().pixelInset.width ;
var Height=GetComponent.<GUITexture>().pixelInset.height;

if(Can){

if(OffsetX==0){
XFaz=((Widht-20)/2);
}else{
XFaz=0;
}
if(OffsetY==0){
YFaz=((Height-20)/2);
}else{
YFaz=0;
}
GetComponent.<GUITexture>().pixelInset.x=((Screen.width)/2)-XFaz-OffsetX;

GetComponent.<GUITexture>().pixelInset.y=((Screen.height)/2)-YFaz-OffsetY;
CalculateOffset();
}
}



function FindOffset () {
var Widht=GetComponent.<GUITexture>().pixelInset.width ;
var Height=GetComponent.<GUITexture>().pixelInset.height;

OffsetX= ((Screen.width)/2)-GetComponent.<GUITexture>().pixelInset.x;
OffsetY= ((Screen.height)/2)-GetComponent.<GUITexture>().pixelInset.y;

yield WaitForSeconds(0.1);
CalculateOffset();

}


function CalculateOffset() {
var Widht=GetComponent.<GUITexture>().pixelInset.width;
var Height=GetComponent.<GUITexture>().pixelInset.height;
var Xk=(StartResolutions.x)/Screen.width;
var Yk=(StartResolutions.y)/Screen.height;
if(CalculatePositionForNewScreen){
OffsetX=OffsetX/Xk;
OffsetY=OffsetY/Yk;

yield WaitForSeconds(0.001);
StartResolutions.x=Screen.width;
StartResolutions.y=Screen.height;
}
Can=true;
if(CalculateScaleForNewScreen){
GetComponent.<GUITexture>().pixelInset.width/=Xk;
GetComponent.<GUITexture>().pixelInset.height/=Yk;
}

}