- Home /
GUI box fit in all screens
Hello everyone! I made a GUI box and I want the position to be the same in all kind of screens. Code:
GUI.Box(new Rect(Screen.width / 2 - m1, Screen.height / 2 - m2, healthBarLength, 20), "");
In the little window on the editor I fit the corrent position. When I change my resolution to Full Screen my box changes position. How can I fix it?
Thanks, any help is appriciated! :)
These are variables which I can move my health bar
Answer by tw1st3d · Jul 21, 2013 at 09:09 PM
I don't know how well this will work, but I'd do something like this.
using UnityEngine;
using System.Collections;
class HUD_Display : MonoBehavior
{
public GUIStyle customStyle;
void OnGUI()
{
GUI.skin = customStyle;
int healthBarLength = 200;
GUI.BeginGroup(0, Screen.height - 20, Screen.width, 20);
GUI.Box(new Rect(0, 0, healthBarLength, 20), "");
GUI.EndGroup();
}
}
Create a GUI group at the bottom of your screen(or wherever you want it), and then create your health bar inside of that. Use absolute positions for creating the box, instead of multiple effectors on the Y position. I.E. Screen.height - Specific Amount Only
Any ideas how to do this on JS ?
Thankks in advance
try to use google or read the manual ;)
http://docs.unity3d.com/Documentation/ScriptReference/GUI.BeginGroup.html
Answer by reppiz01 · Jul 22, 2013 at 07:31 AM
if you have a "fix" aspect ratio you could try this:
GUI.matrix = Matrix4x4.TRS (Vector3.zero,
Quaternion.identity,
new Vector3 (Screen.width / scrW, Screen.height / scrH, 1));
in my case scrW is 1920, and scrH is 1080.
Your answer
Follow this Question
Related Questions
UI RectTransform Position && Screen Resolution 2 Answers
Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 1 Answer
x0,y0 doesn't always point to the bottom left corner 1 Answer
How to set position of 4 gameobjects at the bottom of the screen? 1 Answer
Mouse.position from center of player 1 Answer