PlayerOverviewPanel.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="PlayerNumbering.cs" company="Exit Games GmbH">
  3. // Part of: Asteroid Demo,
  4. // </copyright>
  5. // <summary>
  6. // Player Overview Panel
  7. // </summary>
  8. // <author>developer@exitgames.com</author>
  9. // --------------------------------------------------------------------------------------------------------------------
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.UI;
  13. using ExitGames.Client.Photon;
  14. using Photon.Realtime;
  15. using Photon.Pun.UtilityScripts;
  16. namespace Photon.Pun.Demo.Asteroids
  17. {
  18. public class PlayerOverviewPanel : MonoBehaviourPunCallbacks
  19. {
  20. public GameObject PlayerOverviewEntryPrefab;
  21. private Dictionary<int, GameObject> playerListEntries;
  22. #region UNITY
  23. public void Awake()
  24. {
  25. playerListEntries = new Dictionary<int, GameObject>();
  26. foreach (Player p in PhotonNetwork.PlayerList)
  27. {
  28. GameObject entry = Instantiate(PlayerOverviewEntryPrefab);
  29. entry.transform.SetParent(gameObject.transform);
  30. entry.transform.localScale = Vector3.one;
  31. entry.GetComponent<Text>().color = AsteroidsGame.GetColor(p.GetPlayerNumber());
  32. entry.GetComponent<Text>().text = string.Format("{0}\nScore: {1}\nLives: {2}", p.NickName, p.GetScore(), AsteroidsGame.PLAYER_MAX_LIVES);
  33. playerListEntries.Add(p.ActorNumber, entry);
  34. }
  35. }
  36. #endregion
  37. #region PUN CALLBACKS
  38. public override void OnPlayerLeftRoom(Player otherPlayer)
  39. {
  40. GameObject go = null;
  41. if (this.playerListEntries.TryGetValue(otherPlayer.ActorNumber, out go))
  42. {
  43. Destroy(playerListEntries[otherPlayer.ActorNumber]);
  44. playerListEntries.Remove(otherPlayer.ActorNumber);
  45. }
  46. }
  47. public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
  48. {
  49. GameObject entry;
  50. if (playerListEntries.TryGetValue(targetPlayer.ActorNumber, out entry))
  51. {
  52. entry.GetComponent<Text>().text = string.Format("{0}\nScore: {1}\nLives: {2}", targetPlayer.NickName, targetPlayer.GetScore(), targetPlayer.CustomProperties[AsteroidsGame.PLAYER_LIVES]);
  53. }
  54. }
  55. #endregion
  56. }
  57. }