- Home /
C# Most efficient way to create GUI for android games?
Hello,
I am creating my first android game using Unity 2D and I am 90% finished, I just need to create the GUI. At the moment I am just using onGUI. Some examples of my code:
GUI.Label (new Rect(Screen.width/2 - 500, Screen.height/2 - 400, 1000, 600), "Title");
if(GUI.Button (new Rect(Screen.width/2 - 500, Screen.height/2 - 300, 1000, 600), "Button"))
{
Application.LoadLevel ("Scene");
}
At the moment I am struggling to create a GUI that will resize to fit multiple screen sizes, as there are loads of android phone resolutions. So far I have tested it on a few devices, but the major problem at the moment is the size of the GUI. I have tested the game on HTC Explorer, Nexus 7 and Galaxy Note 2. The Nexus 7 and Note 2 GUI is very small and in the center of the screen, but on the Explorer it fits quite well (The HTC Explorer is a small phone).
Is onGUI the best method for creating a user interface for what I'm doing? if it is, how would I fix my problem I stated above?
Thanks in advance :)
Answer by Andre Barbosa · Feb 18, 2014 at 05:48 PM
If you don't mind the aspect ratio you can do the following:
Get the two resolution modifiers (x,y), based on your working resolution:
var resolution = new Vector2(Screen.width, Screen.height);
var resx = resolution.x/1280.0f; // 1280 is the x value of the working resolution
var resy = resolution.y/800.0f; // 800 is the y value of the working resolution
And then just use this modifier to resize/relocate the Rects that contain your textures:
//If you had originally used:
targetRect = Rect(70,370,150,140);
//You would now change it for:
targetRect = Rect(70*resx,370*resy,150*resx,140*resy);