Reputation: 33
The title probably makes no sense But I did not know how else to explain it... I can better explain here.
I'm making a 3D game and I have the hud set to the bottom left corner and it will resize and adjust when I resize the Window/Pick a different resolution, but the Health/thirst bar stays in one area and does not adjust with it and stay on top of the hud...
In example if The hud is in the Bottom left and when I resize and change the resolution the health thirst bar stay one the left side but they go way up in the middle of the screen Hope that makes sense and stuff like that here is my code thanks in advance :)!
var hudSize : Vector2 = new Vector2(244, 500);
var size : Vector2 = new Vector2(240, 40);
var thirstSize : Vector2 = new Vector2(244,60);
var healthPos : Vector2 = new Vector2(20, 20);
var healthBarDisplay : float = 1;
var healthBarFull : Texture2D;
var hungerPos : Vector2 = new Vector2(20, 60);
var hungerBarDisplay : float = 1;
var hungerBarFull : Texture2D;
var thirstPos : Vector2 = new Vector2(20, 100);
var thirstBarDisplay : float = 1;
var thirstBarFull : Texture2D;
var healthFallRate : int = 150;
var hungerFallRate : int = 150;
var thirstFallRate : int = 100;
var hudPos : Vector2 = new Vector2(0, 0);
var hudDisplay : Texture2D;
var canJump : boolean = false;
var jumpTimer : float = 0.7;
function Start()
{
onMotor = GetComponent(CharacterController);
controller = GetComponent(CharacterController);
}
//OnScreenDrawing Textures
function OnGUI()
{
GUI.DrawTexture(new Rect (0, Screen.height - 125,244,125), hudDisplay);
//HealthBar
GUI.BeginGroup(new Rect (healthPos.x, healthPos.y, size.x, size.y));
GUI.BeginGroup(new Rect (0, 0, size.x * healthBarDisplay, size.y));
GUI.DrawTexture(Rect(0, 0, size.x, size.y), healthBarFull);
GUI.EndGroup();
GUI.EndGroup();
//ThirstBar
GUI.BeginGroup(new Rect (thirstPos.x, thirstPos.y, size.x, size.y));
GUI.BeginGroup(new Rect (0, 0, thirstSize.x * thirstBarDisplay, thirstSize.y));
GUI.DrawTexture(Rect(0, 0, thirstSize.x, thirstSize.y), thirstBarFull);
GUI.EndGroup();
GUI.EndGroup();
}
function Update()
{
if(hungerBarDisplay <= 100)
{
healthBarDisplay -= Time.deltaTime / healthFallRate * 2;
thirstBarDisplay -= Time.deltaTime / thirstFallRate * 5;
}
}
Upvotes: 1
Views: 117
Reputation: 6169
Unity's old OnGUI
stuff is outdated, and does not scale with screen resolution automatically, short of you increasing font and texture sizes manually.
The newer way to do UI stuff, as of Unity 4.6 and higher, is to use Unity's UI Canvas and related elements.
You can get started by clicking Create > UI > Image within the Hierarchy tab, which will create a Canvas and an Image for you.
Check out:
Upvotes: 2
Reputation: 1232
One Way of resizing the Unity Gui is to use screen specific width and height. For Example in your code
GUI.DrawTexture(new Rect (0, Screen.height - 125,244,125), hudDisplay);
you can do some thing like this.
GUI.DrawTexture(new Rect (0, Screen.height - (Screen.height*0.1f),Screen.width/2.0f,Screen.height/8.0f), hudDisplay);
This will make your content resize according to window.
Upvotes: 0