Hello I am working on a game that has a small form factor menu that slides out when the player presses it. The menu has a set of 3 buttons that when pressed, will collapse themselves and make room for another set of buttons. Think of it like a sub-menu. Anyways I have been able to make the buttons collapse themselves but I cannot get them to follow the slide-out menu when it is pressed.
**Because of the Unity Answers limitations I cannot attach more than 2 example pictures so please visit my personal portfolio website located here [http://w3.cnm.edu/~tteichelmann/unity/][1]**
Here is my script
`var MyBoxLeft = -200.0;
var MyButtonsLeft = 0.0;
var MyButtonsLeftSizeX = 200.0;
var SupplyButtonsLeft = 0.0;
var SupplyButtonsLeftSizeX = 200.0;
var icon: Texture;
var icon2: Texture;
var icon3: Texture;
var menuSkin: GUISkin;
var muunaBurger: float = 2.50;
var glucks: float = 1.50;
var bopster: float = 4.00;
//Total Burgers and Position
var totalBurgers: int;
var burgerPos: Vector2 = new Vector2(0, 0);
var burgerSize: Vector2 = new Vector2(0, 0);
//Total Glucks and Position
var totalGlucks: int;
var glucksPos: Vector2 = new Vector2(0, 0);
var glucksSize: Vector2 = new Vector2(0, 0);
//Total Glucks and Position
var totalBopsters: int;
var bopsterPos: Vector2 = new Vector2(0, 0);
var bopsterSize: Vector2 = new Vector2(0, 0);
var globalMoney: float = 100;
var TextPosition: Vector2 = new Vector2(44, 0);
var TextSize: Vector2 = new Vector2(134, 20.5);
function OnGUI() {
GUI.skin = menuSkin;
var tBoxRect = new Rect(MyBoxLeft, 300.0, 220, 80);
var tBoxRect2 = new Rect(MyBoxLeft + 200, 120, 20, 260);
var tButtonRect = new Rect(MyBoxLeft, 120.0, 200, 60);
var tButtonRect2 = new Rect(MyBoxLeft, 180.0, 200, 60);
var tButtonRect3 = new Rect(MyBoxLeft, 240.0, 200, 60);
var tBoxArt = new Rect(MyBoxLeft, 240.0, 120, 120);
var buttonSet1Rect1 = new Rect(MyBoxLeft, 120.0, 200, 60);
var buttonSet1Rect2 = new Rect(MyBoxLeft, 180.0, 200, 60);
var buttonSet1Rect3 = new Rect(MyBoxLeft, 240.0, 200, 60);
var mousePos: Vector2 = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
//Background and icons
GUI.Box(tBoxRect, icon);
GUI.Box(tBoxRect2, icon3);
GUI.DrawTexture(Rect(tBoxRect2), icon3, ScaleMode.StretchToFill);
if (Input.GetButtonDown("Fire1") && tBoxRect.Contains(mousePos) || Input.GetButtonDown("Fire1") && tBoxRect2.Contains(mousePos)) {
AnimateBox();
}
// GUI.Box(tBoxArt, icon2);
GUILayout.BeginArea(Rect(MyButtonsLeft, 0, MyButtonsLeftSizeX, 520));
//When Button is pressed then slide to the right to make room for more buttons.
if (GUI.Button(tButtonRect, "Buy Supplies")) {
AnimateMenu();
GUI.Button(buttonSet1Rect1, "Buy Muuna Burgers");
GUI.Button(buttonSet1Rect2, "Buy Glucks");
GUI.Button(buttonSet1Rect3, "Buy Bopsters");
CreateNewButtons();
}
GUI.Button(tButtonRect2, "Upgrades");
GUI.Button(tButtonRect3, "Stats & Employees");
GUILayout.EndArea();
//
//Amount of supplies
if (totalBurgers >= 999) {
totalBurgers = 999;
}
if (totalGlucks >= 999) {
totalGlucks = 999;
}
if (totalBopsters >= 999) {
totalBopsters = 999;
}
GUILayout.BeginArea(Rect(MyBoxLeft, burgerPos.y, burgerSize.x, burgerSize.y));
var burgerAmount: String = "" + totalBurgers;
GUILayout.Label("Muuna Burgers : " + burgerAmount);
GUILayout.EndArea();
GUILayout.BeginArea(Rect(MyBoxLeft, glucksPos.y, glucksSize.x, glucksSize.y));
var glucksAmount: String = "" + totalGlucks;
GUILayout.Label("Glucks : " + glucksAmount);
GUILayout.EndArea();
GUILayout.BeginArea(Rect(MyBoxLeft, bopsterPos.y, bopsterSize.x, bopsterSize.y));
var bopsterAmount: String = "" + totalBopsters;
GUILayout.Label("Bopsters : " + bopsterAmount);
GUILayout.EndArea();
//
//Money
GUI.Label(Rect(TextPosition.x, TextPosition.y, TextSize.x, TextSize.y), "$" + globalMoney);
//
}
function AnimateBox() {
if (MyBoxLeft == -200.0) {
while (MyBoxLeft < 0.0) {
MyBoxLeft += 5.0;
yield;
}
} else if (MyBoxLeft == 0.0) {
while (MyBoxLeft > -200.0) {
MyBoxLeft -= 5.0;
MyButtonsLeft -= 5.0;
MyButtonsLeft = 0.0;
yield;
}
}
}
function AnimateMenu() {
if (MyButtonsLeft == 0.0) {
while (MyButtonsLeft < 180){ //fix this line, While at any time MyButtonsLeft < 180 the menu can be retracted, causing an error
MyButtonsLeft += 5.0;
MyButtonsLeftSizeX -= 5.0;
yield;
}
} else if (MyButtonsLeft == 180) { //Fix this line, need to figure out how to retract MyButtonsLeft when MyBoxLeft is pressed and MyButtonsLeftSizeX = 20
while (MyButtonsLeft > 0.0) {
MyButtonsLeft -= 5.0;
MyButtonsLeftSizeX += 5.0;
yield;
}
}
}
function CreateNewButtons() {
if (SupplyButtonsLeft == 0.0){
while (SupplyButtonsLeft < 20){
SupplyButtonsLeft+=5.0;
yield;
}
}
while (SupplyButtonsLeft > 0.0){
SupplyButtonsLeft-= 5.0;
yield;
}
}`
[1]: http://w3.cnm.edu/~tteichelmann/unity/
↧