- Home /
Gui placement help
Hi!
I have a problem with my flashlight script for placing an energy bar by using GUI.DrawTexture. In Unity it looks good (check image below).
Note! The flashlight bar is the green one! The yellow one is a stamina bar!
But when I'm playing the game after "Building" it it looks different for different resolutions (check images below).
The script
var BatteryTexture : Texture;
var border : Texture;
var lightSource : Light; //Connect the light source in the Inspector
var FlashlightSound : AudioClip;
static var energy : float = 0.0; //The energy amount of the flashlight
private static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
var drainSpeed : float = 0.4; //The speed that the energy is drained
function Update ()
{
if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
if (Input.GetKeyDown(KeyCode.F))
{
audio.PlayOneShot(FlashlightSound);
}
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight ()
{
turnedOn=!turnedOn;
if (turnedOn && energy>0)
{
TurnOnAndDrainEnergy();
}
else
{
lightSource.enabled = false;
}
}
function OnGUI ()
{
GUI.DrawTexture(new Rect( 30, Screen.height - 880, 4.05 * Flashlight.energy, 25 ), BatteryTexture );
GUI.DrawTexture(new Rect( 30, Screen.height - 880, 405, 25 ), border );
}
//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy ()
{
lightSource.enabled = true;
while (turnedOn && energy>0)
{
energy -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int)
{
energy = Mathf.Clamp(energy+amount, 0, 100);
}
Do any of you guys know how to fix this?
Answer by fafase · Apr 15, 2014 at 11:32 AM
Your dimensions for the rects are hardcoded:
GUI.DrawTexture(new Rect( 30, Screen.height - 880, 4.05 * Flashlight.energy, 25 ), BatteryTexture );
GUI.DrawTexture(new Rect( 30, Screen.height - 880, 405, 25 ), border );
Your border texture is 405 pixels regardless the screen dimensions.
To fix it use the screen dimensions to set your rect:
var width = Screen.width / ratioWidth;
var height = Screen.heighr / ratioHeight;
var xPos = Screen.width / marginRatio;
var yPos = Screen.height / heightRatio;
var rect :Rect = Rect(xPos, yPos, width, height);
So I just add that code at the top of the script and leave the gui code as it is?
I get some errors when i do that, "$$anonymous$$ identifier: ratioWidth", "$$anonymous$$ identifier: ratioHeight" etc...
Well, you need to declare them, probably as public so that you can tweak them and find the most suitable.
You could leave this code in the OnGUI, until you find satisfactory values. Then you move it all to the Start.
One thing though, if you do it in the Start and you change the size of the window, this will not recalculate, keeping it in the OnGUI would make the calculation on each run so if the screen size changes so does the values.
But the best is to use an event like checking in an Update if the width and height have changed and trigger an event for that.
It is all up to what you are after
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GUI problem 1 Answer
Full Screen Get smaller when i actived it 1 Answer
Pause Menu background problem 0 Answers
Pause Menu 5 Answers