Search Unity

Third Party PUN 2 Lobby UI, player names, changing characters, etc.

Discussion in 'Multiplayer' started by Foestar, Nov 15, 2020.

  1. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    So it's been a bit since I could do any Unity let alone PUN 2 as my computer died and I had nothing for like 4 months. But recently I jumped back into it and have already found myself unsure of how to go about something.

    So atm my project allows you to create a game, find games, and even connect no problem. When the player enters the game/room the UI for the room pops up and one of the functions we have when it's enabled refreshes the player listing content like so.

    Code (CSharp):
    1. private void refreshPlayerListing()
    2.     {
    3.         //this adds a player listing prefab for every player in the list
    4.         foreach (var playersName in PhotonNetwork.PlayerList)
    5.         {
    6.             //create the players name object
    7.             var newPlayerListing = Instantiate(playerListingPrefab, playerListContent);
    8.             //and set the players name
    9.             newPlayerListing.gameObject.transform.GetChild(0).GetComponent<Text>().text = playersName.NickName;
    10.         }
    11.     }
    Easy enough, it just does a foreach that picks out each player and instantiates a prefab (in this case a button with a text object) and sets the name of the player off their nickname. Below is a button showing how this looks.
    IMAGE

    The problem with this is it's only on the local side. There's no networking into any of this concept so far. And I realized this as I was setting up the Alignment part of the game room. The Icon on the left of the name (within the prefab that is instantiated) is going to change based off your alignment selection. So if you choose skull it turns into a skull, and vice versa for the happy face.

    This is where my brain fart comes in and I can't think of how to go about doing this. I can send a hidden variable through RPC's and when the list is refreshed it updates it. Or I thought about just instantiated it through the network so it's controlled by said player. The later of the 2 has very odd results with both scaling which I corrected, names not popping up right (which I think I can get to work through RPC's), and a very odd duplication of the whole player list UI rather than just the button.

    Anywho, I will be contemplating on how to do this while I go do laundry but figured I'd throw this question out there to see how others have done this concept in their projects. Really just looking for a best practice.
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,068
  3. Foestar

    Foestar

    Joined:
    Aug 12, 2013
    Posts:
    350
    Holy shmeckles, I remember using PUN 2 and smashing all my projects into multiplayer fast and easy. Not sure as to why I'm struggling so hard this time.

    I did read up on all 3 of the links you posted, and many others including my old own work. Even then I still struggled as I found the answers weren't as clear as I had hoped. I found myself having trouble with RPC's not working after getting a list of players when using OnEnable. I also found many issues with instantiating through Photon rather than regular instantiation UI wise including things not linking up, and scaling/positioning issues.

    Spent all day (actually only about 3 hours) today and some of yesterday fighting just to get the players the ability to change an Icon inside a prefab UI element that also contains their name and appears in a vertical layout group. This shouldn't have been as hard as it was, but in the end I got it with minimal networking, keeping more local side to reduce packages sent.

    In order to do this I instantiated the list objects as players joined and enabled the UI, gave the instantiated objects the name of the player. THEN, remove them when they leave. To change the UI image within their prefab name listing object I simply search for the correct one based off the name and adjust it accordingly through an RPC. Meaning the only networking I do here is a single RPC to change an image and all the names in the list are drawn from a Photon.PLayerList.

    All said and done, it works. lol
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,068
    Glad to read that!