Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by princeadj14 · Jan 10, 2014 at 06:27 PM · displaylobby

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 multiplayalt texter 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 ..

hasdf.jpg (194.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bloker · Jan 10, 2014 at 07:08 PM

Use Network.connections

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Network and lobbies tips and tricks ? 1 Answer

How to make GUI update for all players in a networked game? 1 Answer

Is it possible to have a server deliver complete levels to an otherwise empty client? 1 Answer

Problem with click to display information 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges