Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* GUI */
- private var button_X : float;
- private var button_Y : float;
- private var button_W : float;
- private var button_H : float;
- /* Host Settings */
- public var gameName : String = "Game Name";
- public var hostName : String = "Test Server";
- public var hostDescription : String = "This is a test server.";
- /* Server */
- private var refreshing : boolean = false;
- private var hostData : HostData[];
- var spawn : Transform;
- var player : GameObject;
- function Start()
- {
- button_X = Screen.width * 0.05;
- button_Y = Screen.width * 0.05;
- button_W = Screen.width * 0.1;
- button_H = Screen.width * 0.1;
- }
- function startServer()
- {
- Network.InitializeServer(32, 25000, !Network.HavePublicAddress);
- MasterServer.RegisterHost(gameName, hostName, hostDescription);
- }
- function refreshHostList()
- {
- MasterServer.RequestHostList(gameName);
- refreshing = true;
- }
- function Spawn()
- {
- Network.Instantiate(player, spawn.position, Quaternion.identity, 0);
- }
- function OnServerInitialized()
- {
- Debug.Log("Server Initialized");
- Spawn();
- }
- function OnConnectedToServer()
- {
- Spawn();
- }
- function Update()
- {
- if(refreshing)
- {
- if(MasterServer.PollHostList().Length > 0)
- {
- refreshing = false;
- Debug.Log(MasterServer.PollHostList().Length);
- hostData = MasterServer.PollHostList();
- }
- }
- }
- function OnMasterServerEvent(mse : MasterServerEvent)
- {
- // checks for events.
- if(mse == MasterServerEvent.RegistrationSucceeded)
- {
- Debug.Log("Server is Registered");
- }
- }
- function OnGUI()
- {
- if(!Network.isClient && !Network.isServer)
- {
- if(GUI.Button(Rect(button_X, button_Y, button_W, button_H), "Start Server"))
- {
- Debug.Log("Starting Server");
- startServer();
- }
- if(GUI.Button(Rect(button_X, button_Y * 1.2 + button_H, button_W, button_H), "Refreash Host"))
- {
- Debug.Log("Refreashing");
- refreshHostList();
- }
- if(hostData)
- {
- for(var i : int = 0; i < hostData.length; i++)
- {
- if(GUI.Button(Rect(button_X * 1.2 + button_W, button_Y * 1.2 +(button_H * i), button_W * 3, button_H * 0.5), hostData[i].gameName))
- {
- Network.Connect(hostData[i]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement