- Home /
How to display also numbers of players that connect to my server in my Server list?
i want to add numbers of player inside my lobby in server list of my multiplay er menu?
er menu?
//this is my Menu script...
using UnityEngine; using System.Collections; using System.Text.RegularExpressions; public class Menu : MonoBehaviour { private string CurMenu; public string Name; public string MatchName;
 public int Players;
 Transform bg;
 Transform bg1;
 Transform mn;
 Transform sn;
 public GameObject textfield;
 public GameObject textfield1;
 public GameObject Serverlist;
 public Texture hostagame;
 public Texture PlayerName;
 public Texture SaveName;
 public Texture ServerList;
 public Texture Back;
 public Texture Create;
 public Texture matchnamee;
 public Texture refreshh;
 public Texture maxpleyer;
 public Texture plus;
 public Texture minus;
 public Texture startgame;
 public Texture connect;
 public GUIStyle customGuiStyle;
 
 public AudioClip beep;
 // Use this for initialization
 void Start () 
 {
     CurMenu = "Main";
     Name = PlayerPrefs.GetString("PlayerName");
 
 }
 
 // Update is called once per frame
 void Update () 
 {
     if ( !bg )
     {
         
         bg =  GameObject.Find( "bg" ).transform;
     }
     if ( !bg1 )
     {
         
         bg1 =  GameObject.Find( "bg1" ).transform;
     }
 
 }
 
 void ToMenu(string menu)
 {
     CurMenu = menu;
     
     
     
 }
 void OnGUI(){
     if(CurMenu == "Main")
         Main();
     if(CurMenu == "Host")
         Host();
     if(CurMenu == "Lobby")
         Lobby();
     if(CurMenu == "List")
         MatchList();
 }
 
 private void Main(){
     textfield1.SetActive(false);
     textfield.SetActive(true);
     Serverlist.SetActive(false);
     GUI.backgroundColor = Color.clear;
     if(GUI.Button(new Rect(300,345,400,100),hostagame))
     {
             
         textfield1.SetActive(true);
         audio.PlayOneShot(beep);
         ToMenu("Host");
     }
            GUI.Label(new Rect(710,110,380,120),PlayerName);
     Name = GUI.TextField(new Rect(755,210,135,32),Name,8,customGuiStyle);
     Name = Name.Replace("\n", "");
     Name = Regex.Replace(Name, @"[^a-zA-Z0-9 ]", "");
     
     if(GUI.Button(new Rect(625,250,400,110),SaveName)){
         PlayerPrefs.SetString("PlayerName",Name);
         audio.PlayOneShot(beep);
         
     }
     if(GUI.Button(new Rect(300,410,400,100),ServerList))
     {
         textfield.SetActive(false);
         Serverlist.SetActive(true);
         audio.PlayOneShot(beep);
         ToMenu("List");
     }
     if(GUI.Button(new Rect(300,475,400,100),Back)){
         audio.PlayOneShot(beep);
         Application.LoadLevel("Main_Menu");
     
     }
 
 }
 
 private void Host(){
     GUI.backgroundColor = Color.clear;
     textfield.SetActive(false);
     if(GUI.Button(new Rect(350,470,400,100),Create))
     {
             NetworkManager.Instance.StartServer(MatchName,Players);
             textfield1.SetActive(false);
             ToMenu("Lobby");
             audio.PlayOneShot(beep);
     }
     
     if(GUI.Button(new Rect(580,470,400,100),Back)){
         textfield.SetActive(true);
         ToMenu("Main");
         audio.PlayOneShot(beep);
         
     }
     MatchName = GUI.TextField(new Rect(720,240,140,32),MatchName,5,customGuiStyle);
     MatchName = MatchName.Replace("\n", "");
     MatchName = Regex.Replace(MatchName, @"[^a-zA-Z0-9 ]", "");
     GUI.Label(new Rect(450,200,380,110),matchnamee);
     Players = Mathf.Clamp(Players, 1, 3);
     GUI.Label(new Rect(450,280,380,110),maxpleyer);
     if(GUI.Button(new Rect(640,260,150,150),plus))
     {
         Players ++;
         audio.PlayOneShot(beep);
     }
     GUI.Label(new Rect(780,320,100,70),Players.ToString());
     if(GUI.Button(new Rect(770,260,150,150),minus))
     {
         audio.PlayOneShot(beep);
         Players --;
     }
 
}
 private void Lobby(){
     GUI.backgroundColor = Color.clear;
     if (Network.isServer)
     {
     
         //if(GUI.Button(new Rect(Screen.width -750,Screen.height - 135,300,100),startgame))
         if(GUI.Button(new Rect(500,400,300,100),startgame))
         {
             //NetworkManager.Instance.MatchStarted = true;
             NetworkManager.Instance.networkView.RPC("LoadLevel", RPCMode.All);
             //networkView.RPC("StartServer", RPCMode.All);
             audio.PlayOneShot(beep);
             
         }
     }    
     //if(GUI.Button(new Rect(Screen.width -128,Screen.height - 52,128,32),"Back")){
         //ToMenu("Host");
         //NetworkManager.Instance.OnPlayerDisconnected();
     //    Application.Quit();
         
     //}
 
 
     GUILayout.BeginArea(new Rect(550, 250, Screen.width / 2, Screen.height));
     foreach(Player pl in NetworkManager.Instance.PlayerList)
     {
         sn =  GameObject.Find( "Servername" ).transform;
         sn.guiText.text = "Waiting. . .";
         if(pl == NetworkManager.Instance.MyPlayer)
         {
             //GUI.color = Color.red;
             mn =  GameObject.Find( "Mn" ).transform;
             mn.guiText.text = MatchName;
             sn =  GameObject.Find( "Servername" ).transform;
             sn.guiText.text = "Waiting. . .";
         }
         else
         {
             sn =  GameObject.Find( "Servername" ).transform;
             sn.guiText.text = "Server Name :";
             
         }
         GUILayout.Label(pl.PlayerName,customGuiStyle);
         //    GUI.color = Color.black;
     }
     GUILayout.EndArea();
 }
 
 private void MatchList()
 {
     bg.renderer.enabled = false;
     bg1.renderer.enabled = true;
     GUI.backgroundColor = Color.clear;
     if(GUI.Button(new Rect(175,225,400,100),refreshh))
     {
         MasterServer.RequestHostList("Tut");
         audio.PlayOneShot(beep);
     }
     if(GUI.Button(new Rect(175,293,400,100),Back))
     {
         bg1.renderer.enabled = false;
         bg.renderer.enabled = true;
         textfield.SetActive(true);
         Serverlist.SetActive(false);
         audio.PlayOneShot(beep);
         ToMenu("Main");
     }
 
     GUILayout.BeginArea(new Rect(Screen.width / 2,0, Screen.width / 2, Screen.height), " ","box");
     foreach (HostData hd in MasterServer.PollHostList())
     {
         GUILayout.BeginHorizontal();
         GUILayout.Label(hd.gameName,customGuiStyle);
         if (GUILayout.Button("Connect"))
         {
         
             Network.Connect(hd);    
             ToMenu("Lobby");
             bg1.renderer.enabled = false;
             bg.renderer.enabled = true;
             Serverlist.SetActive(false);
             audio.PlayOneShot(beep);
         }
         GUILayout.EndHorizontal();
     }
     GUILayout.EndArea();
         
 }
 
 
 
 
 
 
 
 
}
//this is my network manager to masterserver...
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class NetworkManager : MonoBehaviour { public string PlayerName; public string MatchName; public static NetworkManager Instance;
 public List<Player> PlayerList = new List<Player>();
 
 public Player MyPlayer;
 public GameObject SpawnPlayer;
 //public GameObject SpawnEnemy;
 public bool MatchStarted;
 
 public Texture ready;
 public Texture giveup;
 public AudioClip beep;
 public int timer = 0;
 public bool alive = true;
 //public GameObject[] SpawnPoints;
 //public bool MatchLoaded;
 
 // Use this for initialization
 void Awake()
 {
     Instance = this;
 }
 
 void Start () {
     
     Instance = this;
     DontDestroyOnLoad(gameObject);
     
 
 }
 
 // Update is called once per frame
 void Update () {
     if (alive == false)
     {
         GameObject aaaa = GameObject.FindWithTag("Hell");
         aaaa.active = false;
         timer ++;
         if(timer == 1)
         {
             Application.LoadLevel("SceneLost");
         }
     }
     PlayerName = PlayerPrefs.GetString("PlayerName");
     
 }
 
 public void StartServer(string ServerName, int MaxPlayers){
     Network.InitializeSecurity();
     Network.InitializeServer(MaxPlayers,25002,true);//25565
     MasterServer.RegisterHost("Tut",ServerName,"");
     
     Debug.Log("Started Server");
 }
 void OnPlayerConnected(NetworkPlayer id)
 {
     //networkView.RPC("Server_PlayerJoined", RPCMode.All,Username, id);
     //foreach (Player pl in PlayerList)
     //{
         networkView.RPC ("Client_PlayerJoined", id, MyPlayer.PlayerName, MyPlayer.OnlinePlayer);
     //}
 }
 
 void OnServerInitialized()
 {
     Server_PlayerJoined(PlayerName, Network.player);
 }
 
 
 void OnConnectedToServer()
 {
     networkView.RPC("Server_PlayerJoined", RPCMode.Server, PlayerName, Network.player);
 }
 
 public void OnPlayerDisconnected(NetworkPlayer id)
 {
     Debug.Log("A Player is Leaving");
     networkView.RPC("RemovePlayer", RPCMode.All, id);
     Network.Destroy(GetPlayer(id).Manager.gameObject);
     Network.RemoveRPCs(id);
     Debug.Log("A Player is Left");
 }
 void OnDisconnectedFromServer(NetworkDisconnection info)
 {
     Debug.Log("You Are Leaving");
     PlayerList.Clear();
     Application.Quit();
     Debug.Log("You Left");
 }
 
 [RPC]
 public void Server_PlayerJoined(string Username,NetworkPlayer id)
 {
     networkView.RPC("Client_PlayerJoined", RPCMode.All,Username, id);
 }
 
 [RPC]
 public void Client_PlayerJoined(string Username, NetworkPlayer id)
 {
     Player temp = new Player();
     temp.PlayerName = Username;
     temp.OnlinePlayer = id;
     PlayerList.Add(temp);
     if(Network.player == id)
     {
         MyPlayer = temp;
         GameObject LastPlayer = Network.Instantiate(SpawnPlayer , Vector3.zero, Quaternion.identity, 0) as GameObject;
         //temp.Manager = LastPlayer.GetComponent<UserPlayer>();
         
     }
     
 }
 
 [RPC]
 public void RemovePlayer(NetworkPlayer id)
 {
     Player temp = new Player();
     foreach (Player pl in PlayerList)
     {
         if (pl.OnlinePlayer == id)
         {
             temp = pl;
         }
     }
     if (temp != null)
     {
         PlayerList.Remove(temp);
     }
 }
 
 [RPC]
 public void LoadLevel()
 {
     //MatchStarted = true
     Application.LoadLevel("Scene_1");
     MatchStarted = true;
     
 }
 
 public static Player GetPlayer(NetworkPlayer id)
 {
     foreach(Player pl in Instance.PlayerList)
     {
             if(pl.OnlinePlayer == id)
             return pl;
     }
     return null;
 }
 
 
 void OnGUI()
 {
     if (MatchStarted == true && !MyPlayer.IsAlive)
     {
         if(GUI.Button(new Rect(500,400,300,100),ready))
         {
             MyPlayer.Manager.FirstpersonCont.Spawn();
             int SpawnIndex = Random.Range (0, LevelManager.Ins.SpawnPoints.Length - 1);
             MyPlayer.Manager.FirstPerson.localPosition = LevelManager.Ins.SpawnPoints[SpawnIndex].transform.position;
             MyPlayer.Manager.FirstPerson.localRotation = LevelManager.Ins.SpawnPoints[SpawnIndex].transform.rotation;
             MyPlayer.Manager.networkView.RPC("Spawn", RPCMode.All);
             audio.PlayOneShot(beep);
             GameObject.Find("NetworkManager").GetComponent<Timer>().enabled = true;
             audio.PlayOneShot(beep);
             
         }
         
     }
     //if (Input.GetKeyUp( KeyCode.Escape ))
     if(Time.timeScale == 0)
     {
         //if (MatchStarted == true && MyPlayer.IsAlive)
         //{    if(GUI.Button(new Rect(300,345,400,100),hostagame))
         GUI.backgroundColor = Color.clear;
         if(GUI.Button(new Rect(580,390,200,80),giveup))
             {
                 alive = false;
                 Time.timeScale = 1;
                 audio.PlayOneShot(beep);
                 Screen.showCursor = true;
                 GameObject.Find("First Person Controller").GetComponent<MouseLook>().enabled = true;
                 GameObject.Find("Main Camera").GetComponent<MouseLook>().enabled = true;
                 PlayerList.Clear();
                 
                 //Application.LoadLevel("SceneLost");
             }
             
         ///}
     }
     
 }
 
} [System.Serializable] public class Player{ public string PlayerName; public NetworkPlayer OnlinePlayer; public float Health = 100; public UserPlayer Manager; public bool IsAlive; public int Deaths; public int Kills; public int Score;
}
Please help me because im to noob of this thankyou in advance ..
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                