- Home /
Adjusting GUI
Hello. I have made some GUI graphics and now i want to add them to the game.The problem is when i change the resoultion, the graphics are moving up or under the screen.How can i make the GUI to adjust to the resoultion of computer? Heres the code i use to it:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour {
private float maxHealth = 100;
private float currentHealth = 100;
private float maxArmour = 100;
private float currentArmour = 100;
public Texture2D cross;
void OnGUI()
{
GUI.Label (new Rect (65,Screen.height - 25,100,50), "HP " + currentHealth);
GUI.Label (new Rect (20,Screen.height - 30,Screen.width/10,Screen.height/12), cross);
GUI.Label (new Rect (200,Screen.height - 25,100,50), "Armor " + currentArmour);
}
void takeHit(float dmg)
{
if(currentArmour > 0) {
currentArmour -= dmg;
if(currentArmour < 0) {
currentHealth += currentArmour;
currentArmour = 0;
}
} else {
currentHealth -= dmg;
}
currentArmour = Mathf.Clamp(currentArmour, 0, maxArmour);
currentHealth = Mathf.Clamp(currentHealth, 0, maxHealth);
}
}
This is the line that makes "Cross"(graphic) show on the screen
GUI.Label (new Rect (20,Screen.height - 30,Screen.width/10,Screen.height/12), cross);
Answer by ricardo_arango · Jan 21, 2015 at 11:22 AM
You are positioning the cross texture using a fixed width and height, regardless of the screen size:
new Rect (20,Screen.height - 30
but you are setting the size using a width and height relative to the screen size:
Screen.width/10,Screen.height/12
That means that it's possible for the object to be outside of the screen bounds.
What you need to do is to position the object also using values relative to the object's size (which you are calculating using the Screen size). For example this code should place the cross in the corner of the screen:
float width = Screen.width/10;
float height = Screen.height/12;
GUI.Label (new Rect (height/2, Screen.height - height/2, widtdh, height), cross);
Your answer
Follow this Question
Related Questions
GUI adapting to screen resolution? 3 Answers
How to maintain high resolution custom background images for GUI elements on different screen sizes? 0 Answers
GUI texture to fit the screen at any resolution? 4 Answers
GUI and Screen Resolution 1 Answer
Best approach to define interactive screen areas regardless of it's resolution? 3 Answers