Unity display player list in room with unity's new UI

Options
Hello photon engeeners, photon developers and game developers who uses photon:)

I'm making a awaiting lobby inside room. Inside room lobby designed that to see connected players to existing room max 10 minimum 2 players. also Inside room lobby has count down number. when count down number will equal to "0" all players will spawn in Game Scene. Inside lobby is not global lobby which gives us photon. Inside lobby has designed with unity's new UI "AwaitingPanel" and it is possible when the room has created. there are 10 UI.text which will be PhotonNetwork.player.name when players join to existing room. So, Player1 creates room if there is no room and his name will be UI.text1 = Player1.
here I'm having problem, When Player2 and other Player3, 4,etc joins to the room which has been created by Player1. I can't figure out how can I make a list of connected players like UI.text2 = Player2, UI.text3 = Player3 etc.

I have tried with different methods but all my effort has no success. I'll post my codes here that u can understand.
I also got sample codes from here: http://forum.unity3d.com/threads/problem-with-playerlist-using-new-ui-pun.329627/#post-2136290
last 2 comments by alarm656 are mine.

my codes:
public class LoobyHook : Photon.MonoBehaviour {


	[SerializeField] GameObject MainPanel;
	[SerializeField] GameObject WaitingPanel;
	[SerializeField] InputField PlayerName;
	[SerializeField] GameObject playerInfo;
	[SerializeField] Transform Hover;

	public  GameObject P1;
	public  GameObject P2;
	public  GameObject P3;


	public static Transform[] pl;
	public Vector3[] lp;

	public static int playerID;
	public int pCount;

	public string roomName = "testRoom";

	private static PhotonView ScenePhotonView;

	void Awake () 
	{
		if (PhotonNetwork.connectionState == ConnectionState.Connected) 
		{

		}

		if (PhotonNetwork.connectionState == ConnectionState.Disconnected) 
		{
			MultiplayerConnector.Instance.Connect ();
		}
	}

	public void StartMatch ()
	{
		if (PhotonNetwork.connected == true) 
		{

			PhotonNetwork.JoinRandomRoom ();

			MainPanel.SetActive (false);
			WaitingPanel.SetActive (true);
		}
	}


	public void OnPhotonRandomJoinFailed ()
	{
		PhotonNetwork.CreateRoom (null);
		CreateRoom();

	}

	public void CreateRoom ()
	{
		RoomOptions roomOptions = new RoomOptions() {IsVisible = true, maxPlayers = 10};
		PhotonNetwork.JoinOrCreateRoom (roomName + System.Guid.NewGuid().ToString("N"), roomOptions, TypedLobby.Default);
		Debug.Log ("room created" + roomName);

	}

	private bool m_blocalPlayerJoined = false;

	private int joinedPlayer = 0;

	 void OnJoinedRoom ()
	{
		if (!m_blocalPlayerJoined) 
		{
			UpdatePlayerList ();
			m_blocalPlayerJoined = true;
			joinedPlayer = joinedPlayer + 1;
		}

			playerID = PhotonNetwork.player.ID;

		Debug.Log ("playerID: " + playerID);

	}

	private void UpdatePlayerList ()
	{
		foreach (PhotonPlayer player in PhotonNetwork.playerList) 
		{
			
				GameObject temp = (GameObject)Instantiate (playerInfo, lp [0], Quaternion.identity);
				temp.transform.SetParent (Hover.transform, false);
				temp.GetComponentInChildren<Text> ().text = "" + player.name;


		}
	}



	void Update () 
	{
		PhotonNetwork.player.name = PlayerName.text;


	}



	/*void OnGUI()
	{
		GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());

		foreach (RoomInfo game in PhotonNetwork.GetRoomList()) {
			if (GUILayout.Button (game.name + " " + game.playerCount + "/" + game.maxPlayers)) {
				PhotonNetwork.JoinRoom (game.name);
				MainPanel.SetActive (false);
				WaitingPanel.SetActive (true);
				//SpawnPlayerName ();

			}

		}

		foreach (PhotonPlayer pl in PhotonNetwork.playerList) 
		{
			GUILayout.Box (pl.name);

		}
	}*/

}
looks like if I use old GUI everything works fine but I would prefer to use new UI system.

some of my methods that I used was:
if(PhotonNetwork.playerList.Lenth == 1)
{
GameObject temp = (GameObject)Instantiate (playerInfo, lp [0], Quaternion.identity);
				temp.transform.SetParent (Hover.transform, false);
				temp.GetComponentInChildren<Text> ().text = "" + player.name;
}

if(PhotonNetwork.playerList.Lenth == 2)
{
GameObject temp = (GameObject)Instantiate (playerInfo, lp [1], Quaternion.identity);
				temp.transform.SetParent (Hover.transform, false);
				temp.GetComponentInChildren<Text> ().text = "" + player.name;
}
All players when they joins gets single position.

I hope everybody has understood what I wanted to explain.
Thank you very much for taking time to read my post.
I hope you'll give me an solution, thanks

Best Answers

  • lello200
    lello200
    Answer ✓
    Options
    Okay now it's more clear. Here's what you should fix in your code:
    private void UpdatePlayerList ()
    	{
    		for (int i = 0; i < PhotonNetwork.playerList.Length; i++) 
    		{
                                   GameObject temp = (GameObject)Instantiate (playerInfo, lp [i], Quaternion.identity);
    				temp.transform.SetParent (Hover.transform, false);
    				temp.GetComponentInChildren<Text> ().text = "" + player.name;
    		}
    	}
    You were using a static number "0" when calling the Vector3 lp variable, that's why all players are on the same spawn position.

    Try it and let me know!

    P.S.: don't use "foreach" in unity for now because it is having memory allocation problems (from what I've heard).
  • lello200
    lello200
    Answer ✓
    Options
    Try this:
    private void UpdatePlayerList ()
    {
    	for (int i = 0; i < PhotonNetwork.playerList.Length; i++) 
    	{
                    GameObject temp = (GameObject)Instantiate (playerInfo, lp [i], Quaternion.identity);
                    temp.transform.SetParent (Hover.transform, false);
                    temp.GetComponentInChildren<Text> ().text = "" + PhotonNetwork.playerList[i].name;
    	}
    }
    If that doesn't help then i'm sorry i can't find the problem.
  • jeanfabre
    jeanfabre mod
    Answer ✓
    Options
    ok,

    Please find a fully working scene dealing with player's list

    PlayerList_UI.unitypackage

    It's addressing a lot of typical issues when having to deal with caching information to display it inside the new UI system, because unlike the old UI you can't simply feed on every update the data, it has to be efficient, and only creating/destroying object when absolutely necessary, and also not updating content every update as it impact performances a lot.

    The package was done with Unity 4.7, but works on Unity 5 too.




    This sample is a good starting point for simple Player listing. It doesn't take in consideration custom Player properties for example. To do this, each item would then get the responsibility to watch for player properties changes, it's a lot more work, but totally feasible of course.

    Let me know if you have questions or issues :)

    Bye,

    Jean

Answers

  • will be great if I handle with UpdatePlayerList when someone joins and when someone leaves. Updating everything until countdown number equal to "0". thanks
  • lello200
    Options
    Hello,

    I'm not sure if I understood you but is your problem the following:
    - player1 creates the room he only sees his name
    - player2 joins the room: player2 sees player1 AND player2's names, but player1 doesn't see player2's name
    - player3 joins the room: player3 sees player1 AND player2 AND player3's names, but players 2 and 3 don't see player3, etc..

    If that's your problem then what you are looking for is the following extra code:
    void OnPhotonPlayerConnected(PhotonPlayer otherPlayer)
     {
          UpdatePlayerList ();
     }
    
    void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer)
     {
          UpdatePlayerList ();
     }
    Hope that answers your question.
    If not then please do elaborate more, or ask it in a clearer way.
  • lello200 said:

    Hello,

    I'm not sure if I understood you but is your problem the following:
    - player1 creates the room he only sees his name
    - player2 joins the room: player2 sees player1 AND player2's names, but player1 doesn't see player2's name
    - player3 joins the room: player3 sees player1 AND player2 AND player3's names, but players 2 and 3 don't see player3, etc..

    If that's your problem then what you are looking for is the following extra code:

    void OnPhotonPlayerConnected(PhotonPlayer otherPlayer)
     {
          UpdatePlayerList ();
     }
    
    void OnPhotonPlayerDisconnected(PhotonPlayer otherPlayer)
     {
          UpdatePlayerList ();
     }
    Hope that answers your question.
    If not then please do elaborate more, or ask it in a clearer way.
    Hi, thank you lello200
    maybe yes, But when I made a build and run it first Player 1 spawns and appeared on position example "A"
    Than I run Unity Editor, joined to the room my Player 2 in Unity Editor spawns but it has also spawned on same position with Player 1. I checked it in Unity Editor Herriarche. Thank you very much.
    Player 1 positions (0, 80, 0)
    Player 2 position also (0, 80,0)
    Throw my code I have understood why it happens. Because I can't Identify spawned players.

    Example how I want to see them:

    1) Player 1
    2) Player 2
    3) Waiting For Other Players To join...
    4) Waiting For Other Players To join...
    etc
  • NomadicWarrior
    edited August 2016
    Options
  • lello200
    lello200
    Answer ✓
    Options
    Okay now it's more clear. Here's what you should fix in your code:
    private void UpdatePlayerList ()
    	{
    		for (int i = 0; i < PhotonNetwork.playerList.Length; i++) 
    		{
                                   GameObject temp = (GameObject)Instantiate (playerInfo, lp [i], Quaternion.identity);
    				temp.transform.SetParent (Hover.transform, false);
    				temp.GetComponentInChildren<Text> ().text = "" + player.name;
    		}
    	}
    You were using a static number "0" when calling the Vector3 lp variable, that's why all players are on the same spawn position.

    Try it and let me know!

    P.S.: don't use "foreach" in unity for now because it is having memory allocation problems (from what I've heard).
  • Yeah, thank you very much. yahoo works excellent except names:)
    Player 1 sees all other players's name as Player 1.
    Player 2 sees all other players's names as Player 2.
    I know why its happening, its because everytime new player joins the roomList updates and sets names to last joined players name.
    I tried with different method like = PhotonNetwork.playerName, PhotonNetwork.playerName[i], PhotonNetwork.player.name, photonView.owner.name. no success. About having correct positions its yes great. Of course I didn't tested yet on OnPhotonPlayerDisconected (),
    PhotonNetwork.LeaveRoom (), OnLeftRoom ().
    exaple image attached, please see:
    https://www.dropbox.com/s/69pgsacem54pgbt/PUNtest.png?dl=0

  • lello200
    lello200
    Answer ✓
    Options
    Try this:
    private void UpdatePlayerList ()
    {
    	for (int i = 0; i < PhotonNetwork.playerList.Length; i++) 
    	{
                    GameObject temp = (GameObject)Instantiate (playerInfo, lp [i], Quaternion.identity);
                    temp.transform.SetParent (Hover.transform, false);
                    temp.GetComponentInChildren<Text> ().text = "" + PhotonNetwork.playerList[i].name;
    	}
    }
    If that doesn't help then i'm sorry i can't find the problem.
  • I don't know how to thank you, everything works like a charm! Thanks again. Hope we will see in other posts. Wish you success in all you do! Have a great day!
  • lello200
    Options
    Glad i could help :smile:
    And goodluck in your game!
  • jeanfabre
    jeanfabre mod
    Answer ✓
    Options
    ok,

    Please find a fully working scene dealing with player's list

    PlayerList_UI.unitypackage

    It's addressing a lot of typical issues when having to deal with caching information to display it inside the new UI system, because unlike the old UI you can't simply feed on every update the data, it has to be efficient, and only creating/destroying object when absolutely necessary, and also not updating content every update as it impact performances a lot.

    The package was done with Unity 4.7, but works on Unity 5 too.




    This sample is a good starting point for simple Player listing. It doesn't take in consideration custom Player properties for example. To do this, each item would then get the responsibility to watch for player properties changes, it's a lot more work, but totally feasible of course.

    Let me know if you have questions or issues :)

    Bye,

    Jean
  • jeanfabre said:

    ok,

    Please find a fully working scene dealing with player's list

    PlayerList_UI.unitypackage

    It's addressing a lot of typical issues when having to deal with caching information to display it inside the new UI system, because unlike the old UI you can't simply feed on every update the data, it has to be efficient, and only creating/destroying object when absolutely necessary, and also not updating content every update as it impact performances a lot.

    The package was done with Unity 4.7, but works on Unity 5 too.




    This sample is a good starting point for simple Player listing. It doesn't take in consideration custom Player properties for example. To do this, each item would then get the responsibility to watch for player properties changes, it's a lot more work, but totally feasible of course.

    Let me know if you have questions or issues :)

    Bye,

    Jean

    Hello Jean, sorry to bring up an old topic but I figured this is the best place to post this. I'm using a modified version of this for a world space scoreboard in my game. It worked great in the demo provided but when I used it myself it didn't do much. I connected into a new room which has the scoreboard but using the UI controller script on it as a solo player doesn't seem to create the player item. It does work if I have two players join then it seems to update.

    The "OnJoinedRoom" only works if there's two players? My work around for it was just to comment that out, and call the "CleanUpList" and "UpdateUI" methods in the Start method. That seems to work, but I don't know if that's the proper way to approach this.

    I'm trying to use this as a scoreboard so the "player ID" text I use for points. My current approach to this is to add that text to a list in the "Refresh data" method of the Player Item script. This is probably not the best way to approach it as I believe that method gets called every time someone joins.

    I'm having issues with the display items showing up swapped on the master and client. Usually player 2 will show up above player one, but it's flipped on the other computer. I tried this both as a client and master and it's always the same. Could you provide some input on how I should approach this better?