organize GUI to any screen size in Unity5

0

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

I have done with gui.Skyn , my measurements are based this 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),"")
     {

     }     
 }
    
asked by anonymous 22.04.2016 / 05:45

1 answer

0

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

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

  • In your case there is always this variation because you use exact pixel, 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 a GUI:

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

This little bit of code always has the function to set this object to 20% of the screen in X and 90% of the screen in Y, ie no matter what resolution it will always be (20% in X and 90% % in Y) ... try doing the same thing in your GUI camera.ScreenToWorldPoint (turns the position of screen space (pixel) into the world space (scalar product)). You will not need to use this since you are using GUI

2º This I think is much simpler, is to use the new GUI of Unity Canvas, as it is complex and a lot to explain I will pass you a tutorial link that can make it easier to use. link

    
01.06.2016 / 14:50