- Home /
GUI error calculating
so, i create a gui like this..
#pragma strict
var bg : Texture;
var bg_w : float = Screen.width*0.75;
var bg_h : float = Screen.height*0.9;
var posbg_w : float= Screen.width*0.1;
var posbg_h : float= Screen.height*0.05;
var b_width:float= 420;
var b_height:float=100;
//konfigurasi button_perkalian
var button_perkalian: GUIStyle;
var pos_b1w: float = posbg_w+(bg_w*0.1);
var pos_b1h: float = posbg_h+(bg_h*0.05);
//konfigurasi button_luas
var button_luas: GUIStyle;
var pos_b2h: float = pos_b1h+100;
//konfigurasi button_satuan
var button_satuan: GUIStyle;
var pos_b3h: float = pos_b2h+100;
function Start () {
print(Screen.width);
}
function OnGUI (){
GUI.DrawTexture(Rect(posbg_w,posbg_h,bg_w,bg_h),bg);
GUI.Button(Rect(pos_b1w,pos_b1h,b_width,b_height)," ",button_perkalian);
GUI.Button(Rect(pos_b1w,pos_b2h,b_width,b_height)," ",button_luas);
GUI.Button(Rect(pos_b1w,pos_b3h,b_width,b_height)," ",button_satuan);
}
i know this code must work.. but why it's have a different value on Inspector? i print the screen.width and says it was 726.. if i times it with 0.75 it will become 554, but in the inspector, it says 175.5 ! i don't know why this is happening, anyone could explain me?
please answer in the answer section, i can't accept it here... :3 but thanks very much @ziv03 u really do me a favor here..
i don't sure why but it's working this way..
Sure, I converted it to an answer.
The reason it works this way is because the scripts are comiled before you run the game. so lets say you wrote it outside the Start function, after the script is comiled the value will always be the same, without any relation to the screen bounds. the Start and Awake functions are called after the gameobject is initialised.
i understand now, thanks so much @ziv03 i'll keep this to remember .. :D
Answer by ziv03 · Jan 14, 2014 at 12:01 PM
the values that aren't in the start or in the onGUI functions are pre-compiled. when you run the game, the values stay the same no matter what the Screen bounds are. get all the values in the Start function.
function Start()
{
bg_w = Screen.width*0.75;
}
Answer by HappyMoo · Jan 14, 2014 at 12:01 PM
During Initialization it probably takes the screenwidth of the game view. Try Maximizing while play if you need this to be the whole screen.
yeah, i've try this too, but the problem still there, the calculation still odd and my gui is so wrecked.. xD
and it comes up, the problem i should have initiate the number on Start function :)