// -------------------------------------------------------------------------------------------------------------------- // // Part of: Asteroid Demo, // // // Player Overview Panel // // developer@exitgames.com // -------------------------------------------------------------------------------------------------------------------- using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using ExitGames.Client.Photon; using Photon.Realtime; using Photon.Pun.UtilityScripts; namespace Photon.Pun.Demo.Asteroids { public class PlayerOverviewPanel : MonoBehaviourPunCallbacks { public GameObject PlayerOverviewEntryPrefab; private Dictionary playerListEntries; #region UNITY public void Awake() { playerListEntries = new Dictionary(); foreach (Player p in PhotonNetwork.PlayerList) { GameObject entry = Instantiate(PlayerOverviewEntryPrefab); entry.transform.SetParent(gameObject.transform); entry.transform.localScale = Vector3.one; entry.GetComponent().color = AsteroidsGame.GetColor(p.GetPlayerNumber()); entry.GetComponent().text = string.Format("{0}\nScore: {1}\nLives: {2}", p.NickName, p.GetScore(), AsteroidsGame.PLAYER_MAX_LIVES); playerListEntries.Add(p.ActorNumber, entry); } } #endregion #region PUN CALLBACKS public override void OnPlayerLeftRoom(Player otherPlayer) { GameObject go = null; if (this.playerListEntries.TryGetValue(otherPlayer.ActorNumber, out go)) { Destroy(playerListEntries[otherPlayer.ActorNumber]); playerListEntries.Remove(otherPlayer.ActorNumber); } } public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps) { GameObject entry; if (playerListEntries.TryGetValue(targetPlayer.ActorNumber, out entry)) { entry.GetComponent().text = string.Format("{0}\nScore: {1}\nLives: {2}", targetPlayer.NickName, targetPlayer.GetScore(), targetPlayer.CustomProperties[AsteroidsGame.PLAYER_LIVES]); } } #endregion } }