So Im basically hunting around for a script that changes the size and position of the health bar according to the screen resolution, currently im working in 1600x1050 and I need the health bar to be in its proper position and size in any resolution.
var xpos : float;
var ypos : float;
var scaledWidth : int;
var scaledHeight : int;
var originalWidth : int;
var originalHeight : int;
function OnGUI() {
originalWidth= guiTexture.texture.width;
originalHeight= guiTexture.texture.height;
originalHeight = Screen.height/30;
originalWidth = Screen.width/30;
scaledHeight = originalHeight;
scaledWidth = originalWidth;
xpos=Screen.width/40-(scaledWidth/2);
ypos=Screen.height/30-(scaledHeight/2);
transform.position = Vector3.zero;
transform.localScale = Vector3.zero;
//guiTexture.pixelInset = Rect (xpos, Screen.height-scaledHeight, scaledWidth, scaledHeight);
guiTexture.pixelInset = Rect (xpos, ypos, scaledWidth, scaledHeight);
}
But the issue with it is that the health bar Width does not change no matter what resolution the game is playing in, the height of the bar works, just not the width, anyone know why?
Didnt really seem to help me, i saw the thread before, infact i saw all the threads regarding gui resolution issues and none seemed to answer my question. I wanted to try using Matrix but have i have absolutly no idea HOW to use it, so i didnt give it a shot, and the other script posted by MrDude didnt seem to work at all for me.
Put that in a javascript file (I called it Toolbox). Make a Rect variable for your GUI element, containing small values (between 0 and 1). Upon Awake, convert your Rect by using this function.
Example : if your Rect values are (0.25, 0.25, 0.5, 0.5), the function will return you a Rect, x and y being at 25% from top, 25% from left, and size width and height of 50% of the width / height screen.
I had really trouble understanding what to do here, didnt really wanna sound uneducated or anything but the only thing im having such an issue with this GUI being in its proper position/size is because its a GUITexture, not a 2d texture, and i cannot change it because its a health bar right?
So when i want to define variables, its something like “var rect = GUITexture;” and usually it doesnt work.
A GUITexture displays a 2D Texture, unless I have skipped my own Unity lessons…
But what do you want to achieve ? The same relative position, no matter what the resolution is ? The same size no matter what the resolution is ? Both ?
Yeah thats exactly, what I want, but i couldnt understand your algorithm, please bare with me because this is my first few days with unity GUI, and im still trying to get a hang of it. I put the script on the GuiTexture game object, but i didnt understand what you ment by making small value numbers in the rect variable, and how do i convert the rect as you mentioned ?
Let’s say your screen width is 1024 pixels. What is the nearest pixel of the middle ? Pixel 512.
If 0 is 0, and 1024 is 1 (proportion speaking), then 512 is 0.5
My previous function transforms any relative rect in absolute rect. So, if you want the absolute position and size of a rect that must always fill the top left quarter of the screen, you would have a relative Rect of (0.0, 0.0, 0.5, 0.5).
Do you see the link ?
512 / 1024 = 0.5
512 = 1024 * 0.5
Using my function would return you (0.0, 0.0, 512.0, 384.0).
But since you only your health bar to be always at, for example, 10% from the left, 10% from the top, but keep the same size no matter what, my function won’t work.