- Home /
Question by
Stormy102 · Apr 30, 2014 at 02:02 PM ·
networkingmultiplayer
Multiplayer Password
I am using a simple multiplayer script for javascript and I would like to add password protection and chat to it. Could someone please show me how to do this. I looked on docs.unity3d.com but I couldn't decipher it. I would like a gui password box to enter the password into. Here is the code:
#pragma strict
private var gameName : String = "New Game";
private var roomName : String = "Example game";
var versionNo : String = "V1.0";
private var refreshing : boolean;
private var hostData : HostData[];
private var outputText : String = "";
private var userName : String;
private var btnX : float;
private var btnY : float;
private var btnW : float;
private var btnH : float;
function Start()
{
btnX = Screen.width * 0.05;
btnY = Screen.width * 0.05;
btnW = Screen.width * 0.1;
btnH = Screen.width * 0.1;
userName = PlayerPrefs.GetString("Username");
}
function startServer()
{
Network.InitializeServer(32, 25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, roomName, versionNo);
}
function refreshHostList()
{
MasterServer.RequestHostList(gameName);
refreshing = true;
}
function Update()
{
if (refreshing)
{
if (MasterServer.PollHostList().Length > 0)
refreshing = false;
Debug.Log(MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
}
}
//Messages
function OnServerInitialized()
{
Debug.Log("Server initialized");
yield WaitForSeconds(0.5);
Debug.Log("Room name is " + gameName);
yield WaitForSeconds(1.5);
Debug.Log("Player name is " + userName);
}
function OnConnectedToServer (){
Debug.Log("Player name is " + userName);
}
function OnMasterServerEvent(mse : MasterServerEvent)
{
if(mse == MasterServerEvent.RegistrationSucceeded)
{
Debug.Log("Registered Server!");
}
}
//GUI
function OnGUI()
{
if (!Network.isServer && !Network.isClient)
{
if (GUI.Button(Rect(Screen.width / 32, Screen.height / 16, 200,25), "Start Server"))
{
Debug.Log("Starting Server");
startServer();
}
gameName = GUI.TextField(Rect(Screen.width / 32, Screen.height / 16 + 30, 100, 25), gameName);
if (GUI.Button(Rect(Screen.width / 32, btnY * 1.4 + btnH, 200, 25), "Refresh Hosts"))
{
Debug.Log("Refreshing");
refreshHostList();
}
if (hostData)
{
for (var i:int = 0; i < hostData.Length; i++)
{
if (GUI.Button(Rect(Screen.width / 32 * 1.5 + btnW, Screen.height / 16 + (btnH * i),btnW * 3, btnH * 0.5), hostData[i].gameName))
{
Network.Connect(hostData[i]);
}
}
}
}
}
//Chat portion
Comment
Your answer

Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Client side Player prefab spawned by overriding GameManager return false for isLocalPlayer 0 Answers
A connection has already been set as ready. There can only be one. 0 Answers
create android multiplayer game using wifi ? 0 Answers
Animation & sound over network 1 Answer