My question revolves around passing variables. I am trying to wrap my head around the multiple ways of using GetComponent(), and I wanted to run these two scripts past you guys to see if I’m doing this correctly and efficiently. Any and all comments are appreciated.
*Edit - Ok. I’ve set it up like you’ve suggested Landern, but how do I continue to get my screen height and width so that my elements will maintain their position on screen resize? Everything gets stored and passed just once, which is great, but I can’t get it to pass through the variables again, and my method is in OnGUI… Sheesh.
using UnityEngine;
using System.Collections;
public class ShopManager : MonoBehaviour {
private int _GUIOMoney = 100;
private int _GUIOYTokens = 2;
private int _GUIOBTokens = 26;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public int Money{
get{return _GUIOMoney;}
set{_GUIOMoney = value;}
}
public int YTokens{
get{return _GUIOYTokens;}
set{_GUIOYTokens = value;}
}
public int BTokens{
get{return _GUIOBTokens;}
set{_GUIOBTokens = value;}
}
}
using UnityEngine;
using System.Collections;
public class GUIOverlay : MonoBehaviour {
public GUISkin GUIOverlaySkin;
public ShopManager sm;
public GameObject PlayerVariables; //Player Variables
//GUI Display Variables
private float _screenwidth = Screen.width;
private float _screenheight = Screen.height;
private const int LABEL_OFFSET = 15;
private const int LABEL_WIDTH = 100;
private const int LABEL_HEIGHT = 40;
private string _levelLoaded;
//GUI Button Variables
private const int BUTTON_OFFSET = 15;
private const int BUTTON_WIDTH = 128;
private const int BUTTON_HEIGHT = 128;
//GUI Button Textures
public Texture shpTexture;
public Texture wkshTexture;
public Texture wrhsTexture;
public Texture lotTexture;
//Setup Nav Bar Rectangle Variables
private Rect displayRectStore;
private Rect displayRectWorkshop;
private Rect displayRectWarehouse;
private Rect displayRectLotto;
//Setup Bottom Bar Rectangle Variables
private Rect displayRectBBox;
private Rect displayRectLMoney;
private Rect displayRectLYTokens;
private Rect displayRectLBTokens;
// //Display Variables for Scaling
// private float native_width = 1920;
// private float native_height = 1080;
// private float rx;
// private float ry;
// Use this for initialization
void Start () {
_levelLoaded = Application.loadedLevelName; //Pulls Scene name for use later
sm = PlayerVariables.GetComponent<ShopManager>(); //Gets reference to Shop Manager script
//Declare all Rectangles for display purposes
SetupRects();
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
GUI.skin = GUIOverlaySkin;
// //Set up scaling
// rx = Screen.width / native_width;
// ry = Screen.height / native_height;
// GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));
DisplayNavBar();
DisplayBottomBar();
}
private void SetupRects(){
//Setup Nav Bar Rectangles
displayRectStore = new Rect(BUTTON_OFFSET, BUTTON_OFFSET, BUTTON_WIDTH, BUTTON_HEIGHT);
displayRectWorkshop = new Rect(BUTTON_OFFSET * 2 + BUTTON_WIDTH, BUTTON_OFFSET, BUTTON_WIDTH, BUTTON_HEIGHT);
displayRectWarehouse = new Rect(BUTTON_OFFSET * 3 + BUTTON_WIDTH * 2, BUTTON_OFFSET, BUTTON_WIDTH, BUTTON_HEIGHT);
displayRectLotto = new Rect(BUTTON_OFFSET * 4 + BUTTON_WIDTH * 3, BUTTON_OFFSET, BUTTON_WIDTH, BUTTON_HEIGHT);
//Setup Bottom Bar Rects
displayRectBBox = new Rect(0, _screenheight - LABEL_HEIGHT, _screenwidth, LABEL_HEIGHT);
displayRectLMoney = new Rect(0, _screenheight - LABEL_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT);
displayRectLYTokens = new Rect(LABEL_WIDTH, _screenheight - LABEL_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT);
displayRectLBTokens = new Rect(LABEL_WIDTH * 2, _screenheight - LABEL_HEIGHT, LABEL_WIDTH, LABEL_HEIGHT);
}
//Displays Navigation Buttons
void DisplayNavBar(){
if(GUI.Button(displayRectStore, shpTexture))
Application.LoadLevel("Store");
if(GUI.Button(displayRectWorkshop, wkshTexture))
Application.LoadLevel("Workshop");
if(GUI.Button(displayRectWarehouse, wrhsTexture))
Application.LoadLevel("Warehouse");
if(GUI.Button(displayRectLotto, lotTexture))
Application.LoadLevel("Lotto");
//Testing Display of Loaded Level Name.
GUI.Label(new Rect(BUTTON_OFFSET * 5 + BUTTON_WIDTH * 4, BUTTON_OFFSET, BUTTON_WIDTH, BUTTON_HEIGHT), _levelLoaded);
}
//Displays Bottom Bar for the GUI
void DisplayBottomBar(){
GUI.Box(displayRectBBox, "");
GUI.Label(displayRectLMoney, "Money: " + sm.Money);
GUI.Label(displayRectLYTokens, "Y Tokens: " + sm.YTokens);
GUI.Label(displayRectLBTokens, "B Tokens: " + sm.BTokens);
}
}
Thanks again.