Search Unity

Server list unity with UI

Discussion in 'Scripting' started by EdgarCarrera, Apr 1, 2015.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    hello guys i wanna know how i can change GUI server list to UI.. example;

    upload_2015-4-1_14-52-8.png


    my code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NetworkManager : MonoBehaviour
    4. {
    5.     public Transform ServerParent;
    6.  
    7.     private const string typeName = "RacingGame";
    8.     private const string gameName = "RoomName";
    9.     private HostData[] hostList;
    10.     private float Timer;
    11.    
    12.     void OnGUI()
    13.     {
    14.         if (!Network.isClient && !Network.isServer)
    15.         {
    16.             if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
    17.                 StartServer();
    18.            
    19.             if (hostList != null)
    20.             {
    21.                 for (int i = 0; i < hostList.Length; i++)
    22.                 {
    23.                     if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
    24.                         JoinServer(hostList[i]);
    25.                 }
    26.             }
    27.         }
    28.     }
    29.        
    30.     public void StartServer()
    31.     {
    32.         if(gameName != null)
    33.         {
    34.             Network.InitializeServer(5, 25000, !Network.HavePublicAddress());
    35.             MasterServer.RegisterHost(typeName, gameName);
    36.         }
    37.     }
    38.    
    39.     void OnServerInitialized()
    40.     {
    41.         SpawnPlayer();
    42.     }
    43.    
    44.    
    45.     void Update()
    46.     {
    47.         Timer = Time.deltaTime;
    48.         if (Timer > 5 && MasterServer.PollHostList().Length > 0)
    49.         {
    50.             hostList = MasterServer.PollHostList();
    51.             Timer = 0;
    52.         }
    53.  
    54.  
    55.     }  
    56.    
    57.     private void JoinServer(HostData hostData)
    58.     {
    59.         Network.Connect(hostData);
    60.     }
    61.    
    62.     void OnConnectedToServer()
    63.     {
    64.         SpawnPlayer();
    65.     }
    66.    
    67.    
    68.     private void SpawnPlayer()
    69.     {
    70.         //Network.Instantiate(playerPrefab, Vector3.up * 5, Quaternion.identity, 0);
    71.     }
    72. }