So I’ve made a bunch of GUI buttons and labels. The fit where I want them in the Free aspect in game preview in unity. But I noticed that when I actually run it as an executable, it messes up the location of everything. How can I keep the size and placement of GUI elements consistent independent of screen or resolution?
ScreenShot:
https://www.dropbox.com/sh/1xa933q63hsq676/1gQ1xdT7xL
EDIT:
Just noticed that if I change the size of the game preview window in unity, they also change positions. Help! 
Hi,
please work with Screen.width and Screen.height in the GUI function.
Please post your code in your actually GUI function.
Hi,
Here is a solution to this problem:
// Definitions of default screen
public float screenWidth_Default = 1366.0f;
public float screenHeight_Default = 768.0f;
//Definitions for scale
private float sw;
private float sh;
private Vector3 GUIsF;
void Start () {
// Sets the position of elements with scale
sw = screen.width / screenWidth_Default;
sh = screen.height / screenHeight_Default;
GUIsF = new Vector3(sw, sh, 1);
}
void OnGUI () {
GUI.matrix Matrix4x4.TRS = (new Vector3 (GUIsF.x, GUIsF.y, 0), Quaternion.identity, GUIsF);
// Draw your GUI according to the default resolution
// Any GUI drawn below GUI.matrix be in scale with its default resolution.
GUI.Button (new Rect (200, 300, 100, 50), "My Button");
}
Change the resolution of your screen and test.
Ok, heres the code.
using UnityEngine;
using System.Collections;
using System; //Used for the Enum class
public class CharacterGenerator : MonoBehaviour {
private PlayerCharacter _toon;
private const int STARTING_POINTS = 350;
private const int MIN_STARTING_ATTRIBUTE_VALUE = 10;
private const int STARTING_VALUE = 50;
private int pointsLeft;
private const int OFFSET = 110;
private const int LINE_HEIGHT = 20;
private const int STAT_LABEL_WIDTH = 100;
private const int BASEVALUE_LABEL_WIDTH = 30;
private const int BUTTON_WIDTH = 20;
private const int BUTTON_HEIGHT = 20;
private int statStartingPos = 100;
public GUISkin MySkin;
public GUISkin VitalSkin;
// Use this for initialization
void Start () {
_toon = new PlayerCharacter();
_toon.Awake();
pointsLeft = STARTING_POINTS;
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
_toon.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
pointsLeft -= (STARTING_VALUE - MIN_STARTING_ATTRIBUTE_VALUE);
}
_toon.StatUpdate();
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
GUI.skin = MySkin;
DisplayName();
DisplayPointsLeft();
DisplayAttributes();
GUI.skin = VitalSkin;
DisplayVitals();
GUI.skin = MySkin;
DisplaySkills();
}
private void DisplayName() {
GUI.Label(new Rect(110, //X
60, //Y
50, //Width
25 //Height
), "Name:");
_toon.Name = GUI.TextField(new Rect(165, //X
60, //Y
100, //Width
25 //Height
), _toon.Name);
}
private void DisplayAttributes() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
GUI.Label(new Rect(OFFSET, //X
statStartingPos + (cnt * LINE_HEIGHT), //Y
STAT_LABEL_WIDTH, //Width
LINE_HEIGHT //Height
), ((AttributeName)cnt).ToString());
GUI.Label(new Rect(STAT_LABEL_WIDTH + OFFSET, //X
statStartingPos + (cnt * LINE_HEIGHT), //Y
BASEVALUE_LABEL_WIDTH, //Width
LINE_HEIGHT //Height
), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH, //X
statStartingPos + (cnt * BUTTON_HEIGHT), //Y
BUTTON_WIDTH, //Width
BUTTON_HEIGHT //Height
), "-")) {
if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE) {
_toon.GetPrimaryAttribute(cnt).BaseValue--;
pointsLeft++;
_toon.StatUpdate();
}
}
if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH, //X
statStartingPos + (cnt * BUTTON_HEIGHT), //Y
BUTTON_WIDTH, //Width
BUTTON_HEIGHT //Height
), "+")) {
if(pointsLeft > 0) {
_toon.GetPrimaryAttribute(cnt).BaseValue++;
pointsLeft--;
_toon.StatUpdate();
}
}
}
}
private void DisplayVitals() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7) * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt).ToString());
GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 7) * LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString());
}
}
private void DisplaySkills() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2 + OFFSET * 2, statStartingPos + (cnt * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((SkillName)cnt).ToString());
GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2 + OFFSET * 2 + STAT_LABEL_WIDTH, statStartingPos + (cnt * LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetSkill(cnt).AdjustedBaseValue.ToString());
}
}
private void DisplayPointsLeft() {
GUI.Label(new Rect(290, 60, 100, 25), "Points Left: " + pointsLeft);
}
}
Could you maybe just give me a quick tip on how to integrate this set of code with your solution?
The Capitol lettered text are constants
if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH, //X
statStartingPos + (cnt * BUTTON_HEIGHT), //Y
BUTTON_WIDTH, //Width
BUTTON_HEIGHT //Height
), "+")) {
Try this code
void OnGUI(){
//inside the area you will place your buttons
GUILayout.BeginArea (new Rect(0, 0, 500, 300));
//create a vertical list of items
GUILayout.BeginVertical ();
//create a horizontal list of items
GUILayout.BeginHorizontal ();
GUILayout.Label ("MY ATTRIBUTE NAME");
GUILayout.Label ("MY POINTS");
GUILayout.Button ("-");
GUILayout.Button ("+");
GUILayout.EndHorizontal ();
//create a horizontal list of items
GUILayout.BeginHorizontal ();
GUILayout.Label ("MY ATTRIBUTE2 NAME");
GUILayout.Label ("MY POINTS2");
GUILayout.Button ("-");
GUILayout.Button ("+");
GUILayout.EndHorizontal ();
//if you want to leave fixed sizes use
GUILayout.BeginHorizontal ();
GUILayout.Label ("MY ATTRIBUTE3 NAME", GUILayout.MaxWidth(250));
GUILayout.Label ("MY POINTS3", GUILayout.MaxWidth(150));
GUILayout.Button ("-", GUILayout.MaxWidth(50));
GUILayout.Button ("+", GUILayout.MaxWidth(50));
GUILayout.EndHorizontal ();
GUILayout.BeginHorizontal ();
GUILayout.Label ("MY ATTRIBUTE4 NAME", GUILayout.MaxWidth(250));
GUILayout.Label ("MY POINTS4", GUILayout.MaxWidth(150));
GUILayout.Button ("-", GUILayout.MaxWidth(50));
GUILayout.Button ("+", GUILayout.MaxWidth(50));
GUILayout.EndHorizontal ();
GUILayout.EndVertical ();
GUILayout.EndArea ();
}
Awsome Thanks Man, Ill test it out.