Basic Multi-player
///// Networking script /////
@script ExecuteInEditMode() // makes GUI show in edit mode
var gameName : String = "AnAuthenticTestName"; // make the name authentic to reduce chance of error
var refreshing = false;
var hostData : HostData[]; // a list of all current hosts
var playerPrefab : GameObject; // your player
var create = false;
var joining = false;
var serverName = "";
var serverInfo = "";
var serverPass = "";
var playerName = "";
var clientPass = "";
var scrollPosition : Vector2 = Vector2.zero;
function Start(){
playerName = PlayerPrefs.GetString("Player Name"); // loads your previosuly used player names
}
function OnGUI () {
if(!Network.isClient && !Network.isServer) { // if you arent the server or a client
if(!create && !joining){
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/2,100,20),"Create Game")) {
create = true;
}
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/2 + 30,100,20),"Find Game")) {
joining = true;
refreshHostList();
}
}
if (create){
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/3 + 110,100,50),"Create")) {
startServer();
}
GUI.Label(Rect (Screen.width/2 - 110,Screen.height/3,100,20),"Server Name:");
GUI.Label(Rect (Screen.width/2 + 40,Screen.height/3,100,20),"Password:");
GUI.Label(Rect (Screen.width/2 - 30,Screen.height/2 + 90,100,20),"Server Info:");
serverName = GUI.TextField (Rect (Screen.width/2 - 120,Screen.height/3 + 30,100,20), serverName, 12);
serverPass = GUI.PasswordField (Rect (Screen.width/2 + 20,Screen.height/3 + 30,100,20), serverPass, "*"[0], 12);
serverInfo = GUI.TextArea (Rect (Screen.width/2 - 70,Screen.height/2 + 120,150,40), serverInfo, 35);
if (GUI.Button(Rect(Screen.width/1.2,Screen.height/20,100,20),"Back")) {
create = false;
}
}
if (joining){
if(hostData) {
scrollPosition = GUI.BeginScrollView (Rect (Screen.width/4,Screen.height/6,Screen.width/1.5,Screen.height/2),scrollPosition, Rect (0, 0, 300, 1000/hostData.length * 30/));
GUI.Label(Rect(30,0,100,20),"Game Name");
GUI.Label(Rect(350,0,100,20),"Server Info");
GUI.Label(Rect(590,0,100,20),"Player Count");
GUI.Label(Rect(700,0,100,20),"Password");
for(var i:int = 0; i < hostData.length; i++) {
GUI.Label(Rect(0,30 + i * 30,200,22),hostData[i].gameName);
GUI.Label(Rect(160,30 + i * 30,500,22),hostData[i].comment);
GUI.Label(Rect(610,30 + i * 30,100,20),hostData[i].connectedPlayers + " / " + hostData[i].playerLimit);
if (hostData[i].passwordProtected){
clientPass = GUI.PasswordField (Rect (680,30 + i * 30,100,25), clientPass, "*"[0], 12);
}
if (GUI.Button(Rect(800,30 + i * 30,100,25),"Join")) {
Network.Connect(hostData[i], clientPass);
}
}
GUI.EndScrollView ();
}
if(!hostData){
GUI.Label(Rect(Screen.width/2 - 50,Screen.height/3,200,25),"No Games Found");
if (GUI.Button(Rect(Screen.width/2 - 50,Screen.height/3 + 30,105,25),"Refresh")) {
refreshHostList();
}
}
if (GUI.Button(Rect(Screen.width/1.2,Screen.height/20,100,20),"Back")) {
joining = false;
}
}
if (GUI.Button(Rect(Screen.width/20,Screen.height/20,100,20),"Quit")) {
Application.Quit();
}
GUI.Label(Rect(Screen.width/2 - 35,Screen.height/1.2 - 30,100,20),"Your Name:");
playerName = GUI.TextField (Rect (Screen.width/2 - 50,Screen.height/1.2,100,20), playerName, 12);
}
}
function Update () {
if(refreshing) {
if(MasterServer.PollHostList().Length > 0) {
refreshing = false;
hostData = MasterServer.PollHostList();
}
}
}
function startServer () {
if (serverPass != ""){
Network.incomingPassword = serverPass;
}
Network.InitializeServer(15,25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, serverName, serverInfo);
}
function OnServerInitialized () {
DontDestroyOnLoad (transform.gameObject);
Application.LoadLevel ("Lobby");
lobbySpawn();
}
function OnConnectedToServer () {
lobbySpawn();
}
function lobbySpawn(){
yield WaitForSeconds(0.1);
var made = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
made.GetComponent(playerMove).playerName = playerName;
PlayerPrefs.SetString("Player Name", playerName);
if(Network.isClient){
Destroy(this);
}
}
function refreshHostList () {
MasterServer.ClearHostList();
MasterServer.RequestHostList(gameName);
refreshing = true;
}
///// NetworkView script /////
function Awake() {
if (!networkView.isMine){
GetComponentInChildren(Camera).enabled = false;
GetComponent(MouseLook).enabled = false;
GetComponent(playerMove).enabled = false;
}
}
///// Basic movement script (with RPC's)/////
#pragma strict
var playerName : String;
function Update () {
if(Input.GetButton("w")){
networkView.RPC("forward",RPCMode.AllBuffered);
}
if(Input.GetButton("a")){
networkView.RPC("left",RPCMode.AllBuffered);
}
if(Input.GetButton("s")){
networkView.RPC("back",RPCMode.AllBuffered);
}
if(Input.GetButton("d")){
networkView.RPC("right",RPCMode.AllBuffered);
}
}
function OnGUI () {
GUI.Label(Rect(Screen.width/2,Screen.height/2,100,20),"X");
}
@RPC
function forward(){
transform.Translate(Vector3.forward / 10);
}
@RPC
function left(){
transform.Translate(Vector3.left / 10);
}
@RPC
function back(){
transform.Translate(Vector3.back / 10);
}
@RPC
function right(){
transform.Translate(Vector3.right / 10);
}