Skip to content

Instantly share code, notes, and snippets.

@Gillissie
Created March 29, 2011 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gillissie/891849 to your computer and use it in GitHub Desktop.
Save Gillissie/891849 to your computer and use it in GitHub Desktop.
Unity GUI script
// The main GUI script, attached to the main camera.
@script ExecuteInEditMode();
var guiSkin:GUISkin;
var titleTexture:Texture2D;
var soundOnTexture:Texture2D;
var soundOffTexture:Texture2D;
static var leaderboard:Leaderboard;
static var soundOn:boolean;
private var mainScript:MainScript;
private var viewWidth:int;
private var viewHeight:int;
private var audioListener:AudioListener;
private var firstDraw:boolean;
function OnGUI()
{
GUI.skin = guiSkin;
var y:int = 0;
var x:int = 0;
switch (mainScript.gameMode)
{
case GameMode.MENU:
// Menu/Title
var menuY:int = 50;
GUI.Label(Rect(10,10,256,256),titleTexture);
if (Application.isPlaying && leaderboard.currentUser == null)
{
GUI.Label(Rect(viewWidth - 320,menuY,300,40),"Retrieving User Info...","smallLabel");
}
else
{
if (GUI.Button(Rect(viewWidth - 320,menuY,300,40),"Start New Game"))
{
mainScript.StartNewGame();
}
menuY += 60;
if (GUI.Button(Rect(viewWidth - 320,menuY,300,40),"Invite Friends"))
{
Application.ExternalCall("ba.inviteFriends", 0);
}
if (mainScript.score > 0)
{
menuY += 60;
if (GUI.Button(Rect(viewWidth - 320,menuY,300,40),"Celebrate Score: " + mainScript.score))
{
Application.ExternalCall("ba.postMessageUI", new Array(mainScript.score,leaderboard.currentRank));
}
}
if (mainScript.inProgress)
{
menuY += 60;
if (GUI.Button(Rect(viewWidth - 320,menuY,300,40),"Resume Game"))
{
mainScript.ResumeGame();
}
}
else if (Application.isPlaying && leaderboard.currentUser != null)
{
menuY += 60;
if (GUI.Button(Rect(viewWidth - 320,menuY,300,40),"View High Scores"))
{
mainScript.GoToLeaderboard();
}
}
}
GUI.Label(Rect(viewWidth / 2,viewHeight - 40,1,1),"Copyright 2011 BestGamesEva\nMusic by Kevin MacLeod","smallLabel");
break;
case GameMode.INGAME:
// Game in progress.
GUI.Label(Rect(10,viewHeight - 50,1,1),"Level: " + mainScript.level,"mediumLeft");
GUI.Label(Rect(viewWidth / 2,viewHeight - 50,1,1),mainScript.hits + "/" + mainScript.hitsGoal,"mediumCenter");
GUI.Label(Rect(viewWidth - 10,viewHeight - 50,1,1),"Time: " + mainScript.SecondsRemaining(),"mediumRight");
break;
case GameMode.LEVELCOMPLETE:
if (mainScript.level == 0)
{
GUI.Label(Rect(viewWidth / 2,viewHeight / 2,1,1),"Click " + mainScript.hitsGoal + " Bats\nBefore Time Expires","levelComplete");
}
else
{
y = -125;
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 + y,1,1),"Level " + mainScript.level + " Complete!","levelComplete");
if (Time.time - mainScript.endTime > 1)
{
y += 50;
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 + y,1,1),"Accuracy Bonus: " + mainScript.accuracyBonus,"mediumCenter");
if (Time.time - mainScript.endTime > 2)
{
y += 50;
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 + y,1,1),"Time Bonus: " + mainScript.timeBonus,"mediumCenter");
if (Time.time - mainScript.endTime > 3)
{
y += 50;
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 + y,1,1),"Canary Bonus: " + mainScript.canaryBonus,"mediumCenter");
if (Time.time - mainScript.endTime > 4)
{
y += 100;
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 + y,1,1),"Score: " + mainScript.score,"levelComplete");
}
}
}
}
}
break;
case GameMode.GAMEOVER:
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 - 100,1,1),"Game Over\nOn Level " + mainScript.level,"levelComplete");
GUI.Label(Rect(viewWidth / 2,viewHeight / 2 + 50,1,1),"Score: " + mainScript.score,"levelComplete");
GUI.Label(Rect(viewWidth / 2,viewHeight - 30,1,1),"(Press Esc to Continue)","smallLabel");
if (Time.time - mainScript.endTime > 2)
{
if (GUI.Button(Rect(viewWidth / 2 - 110,viewHeight / 2 + 150,220,40),"View High Scores"))
{
mainScript.GoToLeaderboard();
}
}
break;
case GameMode.LEADERBOARD:
GUI.Label(Rect(10,10,256,256),titleTexture);
GUI.Label(Rect(450,10,1,1)," Top 10 Friend High Scores","mediumCenter");
x = viewWidth - 430; // For easier relative positioning of all leaderboard Label elements
y = 60;
GUI.Label(Rect(x,y,1,1),"Rank","leaderboardValue");
GUI.Label(Rect(x + 110,y,1,1),"Score","leaderboardValue");
GUI.Label(Rect(x + 150,y,1,1),"Name","leaderboardName");
y += 35;
for (var i:int = 0; i < 10; i++)
{
if (i < leaderboard.friends.length)
{
var rank:int = i + 1;
var friend:Friend = leaderboard.friends[i];
GUI.Label(Rect(x,y,1,1),"" + rank,"leaderboardValue");
GUI.Label(Rect(x + 110,y,1,1),"" + friend.score,"leaderboardValue");
GUI.Label(Rect(x + 150,y,1,1),"" + friend.name,"leaderboardName");
y += 35;
}
}
GUI.Label(Rect(viewWidth / 2,505,1,1),"Your High Score: " + leaderboard.currentUser.score + ", Rank: " + leaderboard.currentRank,"mediumCenter");
GUI.Label(Rect(viewWidth / 2,viewHeight - 30,1,1),"(Click to Continue)","smallLabel");
break;
}
if (GUI.Button(Rect (viewWidth - 21, 5, 16, 16), (soundOn ? soundOnTexture : soundOffTexture)))
{
soundOn = !soundOn;
audioListener.volume = (soundOn ? 1 : 0);
}
}
function Start()
{
mainScript = GetComponent(MainScript);
leaderboard = new Leaderboard();
audioListener = GetComponent(AudioListener);
viewWidth = Camera.main.pixelWidth;
viewHeight = Camera.main.pixelHeight;
soundOn = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment