Search Unity

UI player prefab doesn't disappear after player left the room

Discussion in 'Multiplayer' started by Clashman, May 17, 2021.

  1. Clashman

    Clashman

    Joined:
    Feb 18, 2019
    Posts:
    53
    Hi !
    I'm using PUN 2. I maked a scene called "waiting room". All players who joined a room go to this scene. Each player in the scene can see the icone of other connected players. However, when a player leaves the room, his icon is still visible. Can u help me, please ? What can I do to remove his icone ?
    Thanks for your answers.

    This is my code :
    Code (CSharp):
    1. public class PlayerListingsDisplay : MonoBehaviourPunCallbacks
    2. {
    3.     [SerializeField]
    4.     private Transform _content;
    5.     [SerializeField]
    6.     private PlayerListing _playerListing;
    7.  
    8.     private List<PlayerListing> _listings = new List<PlayerListing>();
    9.  
    10.     private GameObject LaunchButtonObject;
    11.  
    12.     private const byte EVENT_ISREADY = 14;
    13.  
    14.     public void Awake()
    15.     {
    16.         LaunchButtonObject = GameObject.Find("LaunchButton");
    17.  
    18.         if (!PhotonNetwork.IsMasterClient)
    19.         {
    20.             LaunchButtonObject.SetActive(false);
    21.         }
    22.         else
    23.         {
    24.             LaunchButtonObject.SetActive(true);
    25.             LaunchButtonObject.GetComponent<Button>().interactable = false;
    26.         }
    27.     }
    28.     public void Update()
    29.     {
    30.         Debug.Log("Nombre de joueurs : " + PhotonNetwork.CurrentRoom.Players.Count);
    31.         Debug.Log("Nombre de listings : " + _listings.Count);
    32.         GetCurrentRoomPlayers();
    33.         UpdateListings();
    34.     }
    35.  
    36.     public override void OnPlayerEnteredRoom(Player NewPlayer)
    37.     {
    38.         if (PhotonNetwork.CurrentRoom.Players.Last().Value == NewPlayer)
    39.         {
    40.             AddPlayerListing(PhotonNetwork.CurrentRoom.Players.Last());
    41.         }
    42.  
    43.         foreach (PlayerListing Listing in _listings)
    44.         {
    45.             if (Listing.Player != NewPlayer)
    46.             {
    47.                 SendEvent_PlayerIsReady(Listing.Player, Listing.IsReady);
    48.             }
    49.         }
    50.     }
    51.  
    52.     public override void OnPlayerLeftRoom(Player OtherPlayer)
    53.     {
    54.         PlayerListing ListingToDelete = _listings.Where(x => x.Player == OtherPlayer).FirstOrDefault();
    55.  
    56.         if(ListingToDelete != null)
    57.         {
    58.             Destroy(ListingToDelete);
    59.             _listings.Remove(ListingToDelete);
    60.  
    61.             foreach (PlayerListing Listing in _listings)
    62.             {
    63.                 if (Listing.ID != 1 && Listing.ID != 2)
    64.                 {
    65.                     Listing.ID -= 1;
    66.                 }
    67.             }
    68.         }
    69.     }
    70.  
    71.     public void AddPlayerListing(KeyValuePair<int, Player> PlayerInfo)
    72.     {
    73.         int Index = _listings.FindIndex(x => x.Player == PlayerInfo.Value && x.ID == PlayerInfo.Key);
    74.         if (Index != -1)
    75.         {
    76.             _listings[Index].SetPlayerInfo(PlayerInfo.Value, PlayerInfo.Key);
    77.         }
    78.         else
    79.         {
    80.             PlayerListing Listing = Instantiate(_playerListing, _content);
    81.             if (Listing != null)
    82.             {
    83.                 Listing.SetPlayerInfo(PlayerInfo.Value, PlayerInfo.Key);
    84.                 _listings.Add(Listing);
    85.             }
    86.         }
    87.     }
    88.  
    89.     private void GetCurrentRoomPlayers()
    90.     {
    91.         if (!PhotonNetwork.IsConnected)
    92.             return;
    93.         if (PhotonNetwork.CurrentRoom == null || PhotonNetwork.CurrentRoom.Players == null)
    94.             return;
    95.  
    96.         foreach (KeyValuePair<int, Player> PlayerInfo in PhotonNetwork.CurrentRoom.Players)
    97.         {
    98.             AddPlayerListing(PlayerInfo);
    99.         }
    100.     }
    101.  
    102.     private void UpdateListings()
    103.     {
    104.         int ReadyPlayersCount = 0;
    105.  
    106.         foreach (PlayerListing Listing in _listings)
    107.         {
    108.             Listing.GetComponent<RawImage>().texture = Resources.Load<Texture2D>("Textures/Sprites/GUI/Icons/Player" + Listing.ID);
    109.  
    110.             Debug.Log(Listing.Player.NickName + " ready : " + Listing.IsReady);
    111.  
    112.             if (Listing.IsReady == true)
    113.             {
    114.                 ReadyPlayersCount += 1;
    115.                 Listing.ReadyIcon().transform.localScale = new Vector3(1, 1, 1);
    116.             }
    117.             else
    118.             {
    119.                 Listing.ReadyIcon().transform.localScale = Vector3.zero;
    120.             }
    121.         }
    122.  
    123.         if (ReadyPlayersCount == _listings.Count)
    124.         {
    125.             LaunchButtonObject.GetComponent<Button>().interactable = true;
    126.         }
    127.         else
    128.         {
    129.             LaunchButtonObject.GetComponent<Button>().interactable = false;
    130.         }
    131.     }
    132.  
    133.     public void OnClick_SetPlayerReady()
    134.     {
    135.         foreach (PlayerListing Listing in _listings)
    136.         {
    137.             if (Listing.Player.NickName == PhotonNetwork.NickName)
    138.             {
    139.                 Listing.SwitchIsReady();
    140.                 SendEvent_PlayerIsReady(Listing.Player, Listing.IsReady);
    141.             }
    142.         }
    143.     }
    144.  
    145.     private void OnEnable()
    146.     {
    147.         PhotonNetwork.NetworkingClient.EventReceived += OnReceivedEvent;
    148.     }
    149.  
    150.     private void OnDisable()
    151.     {
    152.         PhotonNetwork.NetworkingClient.EventReceived -= OnReceivedEvent;
    153.     }
    154.  
    155.     private void OnReceivedEvent(EventData obj)
    156.     {
    157.         object[] ReceivedObjects = (object[])obj.CustomData;
    158.  
    159.         if (obj.Code == EVENT_ISREADY)
    160.         {
    161.             foreach (PlayerListing Listing in _listings)
    162.             {
    163.                 if (Listing.Player == (Player) ReceivedObjects[0])
    164.                 {
    165.                     Listing.IsReady = (bool) ReceivedObjects[1];
    166.                 }
    167.             }
    168.         }
    169.     }
    170.  
    171.     private void SendEvent_PlayerIsReady(Player Gamer, bool IsReady)
    172.     {
    173.         object[] SendedObjects = new object[] { Gamer, IsReady };
    174.         PhotonNetwork.RaiseEvent(EVENT_ISREADY, SendedObjects, RaiseEventOptions.Default, SendOptions.SendUnreliable);
    175.     }
    176. }