organize GUI for any screen size in Unity5

Asked

Viewed 1,893 times

0

I’m creating a 3d project, making a menu. When I run the game at different resolutions always changes the menus.
I do everything in Free aspect, my intention is that even in other resolutions the menu and its buttons remain equal.

I have done with gui.Skyn, my measurements are based that way :

 posiY = Screen.height/2 + (Screen.height/2-93);
 posiX = Screen.width/2 -(Screen.width/18-20);
 function OnGUI(){    
GUI.skin.font = fonte;   
GUI.skin.label.fontSize = Screen.height/10;                                                if(colocarItem.exibeMenu){      
GUI.skin = perSkin[1];           
if (GUI.Button(Rect( posiX2  ,posiY2  ,Screen.width/12 -5 ,Screen.height/6),"")
     {

     }     
 }

1 answer

1

Good morning Diogo, good to two options for you to solve this problem, one that I used before the new UI of Unity the Famous "Canvas".

1º The first solution is you use your equation based on "%", an example , first of all Padronize your Camera Ratio, do the game in 16:9 or 16:10, after that you have to do the calculations of your positions always in %.

  • In your case there is always this variation because you use exact pixels, as you probably do not lock a fixed resolution within your game, this changes according to the resolution chosen by the user.

Code Example Using Gameobject as GUI:

void Start () 
{
    GameObject.Find("QualquerObjeto").transform.position = camera.ScreenToWorldPoint ( new Vector3 ( (Screen.width * 20) / 100, Screen.height * 90 / 100 , 10 ) );
}

This small Snippet of code has the function of always at start adjust this object in 20% of the screen in X and 90% of the screen in Y, that is no matter the resolution it will always be (20% in X and 90% in Y)... try to do the same thing in your GUI camera.Screentoworldpoint (transforms the screen space position(pixel) to the world space(scalar product)). It will not be necessary to use this since you are using GUI

2º This I find much simpler, is to use the new GUI of Unity Canvas, as it is complex and a lot to explain I will give you a tutorial link that can make it easier to use. https://www.youtube.com/watch?v=xVFqnDNq84w

  • Opa muito obrigado, the solution to this was really the Canvas, it facilitates the work, for those who have this problem I suggest that use the Canvas tbm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.