PlayerUI.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace Mirror.Examples.Basic
  4. {
  5. public class PlayerUI : MonoBehaviour
  6. {
  7. [Header("Player Components")]
  8. public Image image;
  9. [Header("Child Text Objects")]
  10. public Text playerNameText;
  11. public Text playerDataText;
  12. // Sets a highlight color for the local player
  13. public void SetLocalPlayer()
  14. {
  15. // add a visual background for the local player in the UI
  16. image.color = new Color(1f, 1f, 1f, 0.1f);
  17. }
  18. // This value can change as clients leave and join
  19. public void OnPlayerNumberChanged(byte newPlayerNumber)
  20. {
  21. playerNameText.text = string.Format("Player {0:00}", newPlayerNumber);
  22. }
  23. // Random color set by Player::OnStartServer
  24. public void OnPlayerColorChanged(Color32 newPlayerColor)
  25. {
  26. playerNameText.color = newPlayerColor;
  27. }
  28. // This updates from Player::UpdateData via InvokeRepeating on server
  29. public void OnPlayerDataChanged(ushort newPlayerData)
  30. {
  31. // Show the data in the UI
  32. playerDataText.text = string.Format("Data: {0:000}", newPlayerData);
  33. }
  34. }
  35. }