topical media & game development
lib-unity-tutorial-network-m2h-Assets-Example3-MainMenu.js / js
/*
* This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl)
* The original author of this code is Mike Hergaarden, even though some small parts
* are copied from the Unity tutorials/manuals.
* Feel free to use this code for your own projects, drop us a line if you made something exciting!
*/
#pragma strict
static var SP : MainMenu;
private var joinMenuScript : JoinMenu;
private var gameLobbyScript : GameLobby;
private var multiplayerScript : MultiplayerMenu;
private var requirePlayerName : boolean = false;
private var playerNameInput : String = "";
function Awake(){
SP=this;
playerNameInput = PlayerPrefs.GetString("playerName", "");
requirePlayerName=true;
joinMenuScript = GetComponent(JoinMenu);
gameLobbyScript = GetComponent(GameLobby);
multiplayerScript = GetComponent(MultiplayerMenu);
OpenMenu("multiplayer");
}
function OnGUI(){
if(requirePlayerName){
myWindowRect = GUILayout.Window (9, Rect(Screen.width/2-150,Screen.height/2-100,300,100), NameMenu, "Please enter a name:");
}
}
function OpenMenu(newMenu : String){
if(requirePlayerName){
return;
}
if(newMenu=="multiplayer-quickplay"){
joinMenuScript.EnableMenu(true);//quickplay=true
}else if(newMenu=="multiplayer-host"){
gameLobbyScript.EnableLobby();
}else if(newMenu=="multiplayer-join"){
joinMenuScript.EnableMenu(false);//quickplay:false
}else if(newMenu=="multiplayer"){
multiplayerScript.EnableMenu();
}else{
Debug.LogError("Wrong menu:"+newMenu);
}
}
function NameMenu(id : int){
GUILayout.BeginVertical();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.Label("Please enter your name");
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(10);
playerNameInput = GUILayout.TextField(playerNameInput);
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if(playerNameInput.length>=1){
if(GUILayout.Button("Save")){
requirePlayerName=false;
PlayerPrefs.SetString("playerName", playerNameInput);
OpenMenu("multiplayer");
}
}else{
GUILayout.Label("Enter a name to continue...");
}
GUILayout.Space(10);
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.