- Home /
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.
Answer by robert_mathew · Jan 05, 2012 at 12:25 PM
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
Answer by DaveA · Jan 05, 2012 at 10:00 PM
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.
What's the equivalent version number of Unity for Windows to 3.5b? I'm using 3.4.2f3 with Windows. I'm quite new to Unity so I'm not too well acquainted with it yet.
So I made a CameraCheck.js file and attached it to the game scene. The script's doing something like that so far:
public var camera : Camera;
function Start() {
if ( Application.platform == RuntimePlatform.Android ) {
var resolution : Resolution = Screen.currentResolution;
if ( resolution.width == X && resolution == Y ) {
//Do something to the camera
}
}
}
I'm wondering now how to detect/get the proper screen resolution and how I control the camera with those values?
Answer by Statement · Jan 06, 2012 at 02:50 PM
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.
Thanks for the info. So with GUI.matrix I can scale the GUI but how do I get the resolution of the physical Android device? For instance the Nexus One has 480x800 pixels while the Galaxy Nexus has 720x1280. I don't know how I can get that information in the first place.
You can use Screen.currentResolution.width & Screen.currentResolution.height to know about the current resolution of your device.
Answer by peterwilli · Mar 14, 2014 at 09:57 AM
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
Answer by selvap · Mar 14, 2014 at 11:09 AM
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..
Where does scaleOnRatio1 come from. Needs more explanation.
Your answer
Follow this Question
Related Questions
Android Button Screen 1 Answer
Finding Screen Resolution of an Android Device 0 Answers
How to make my game fit into my droid? 1 Answer
Excluding android devices with resolution less than X 0 Answers
GUI and Screen Resolution 1 Answer