- Home /
GUI Scale Problem
Hi guys, I'm having a problem with the placement of text done with code, when the resolution increases, The text is not positioned in the right place, he is in the same place of the previous resolution. Here the my script:
var MaxHealth : int = 100;
var CurrentHeath : int;
var styleHealth : GUIStyle;
function Start () {
CurrentHeath = MaxHealth;
}
function ApplyDamage ( Damage : float ) {
if( CurrentHeath < 0){
return;
}
CurrentHeath -= Damage;
if( CurrentHeath == 0 ){
Destroy( gameObject);
}
}
function OnGUI (){
GUI.Box(Rect(Screen.width - 200 ,Screen.height-55,100,20),"" + CurrentHeath, styleHealth);
}
Thanks.
That script should result in a box that is always 200 pixels from the bottom of the screen and 55 pixels from the right edge of the screen.
It will always stay the same size.
Answer by jahroy · Nov 21, 2011 at 01:32 AM
Here is the basic approach we use to scale our GUI:
var optimalHeight : int = 1200;
var welcomeRect : Rect = Rect(200, 200, 400, 300);
function getScale () : float
{
return Screen.height / optimalHeight;
}
function OnGUI ()
{
var scaledRect = scaleRectangle(welcomeRect);
GUI.Label(scaledRect, "I am a scaled Label);
}
function scaleRectangle ( someRect : Rect ) : Rect
{
var theScale : float = getScale();
var theX : float = theScale * someRect.x;
var theY : float = theScale * someRect.y;
var theW : float = theScale * someRect.width;
var theH : float = theScale * someRect.height;
return Rect(theX, theY, theW, theH);
}
The above code bases the scale entirely on the screen height, but it should be pretty obvious how to incorporate height and width.
It's not supposed to be something you add to your project. It's a demonstration of a way to approach scaling your GUI.
Your answer
Follow this Question
Related Questions
GUI Resolution Ajust 1 Answer
what is wrong with this online FPS script(not done) ? 1 Answer
[Closed] MainMenu Script error 2 Answers
Error BCE0051: Operater '==' cannot be used. Please Help! 1 Answer