MatchGUI.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Mirror.Examples.MultipleMatch
  5. {
  6. public class MatchGUI : MonoBehaviour
  7. {
  8. Guid matchId;
  9. [Header("GUI Elements")]
  10. public Image image;
  11. public Toggle toggleButton;
  12. public Text matchName;
  13. public Text playerCount;
  14. [Header("Diagnostics - Do Not Modify")]
  15. public CanvasController canvasController;
  16. public void Awake()
  17. {
  18. #if UNITY_2021_3_OR_NEWER
  19. canvasController = GameObject.FindAnyObjectByType<CanvasController>();
  20. #else
  21. // Deprecated in Unity 2023.1
  22. canvasController = GameObject.FindObjectOfType<CanvasController>();
  23. #endif
  24. toggleButton.onValueChanged.AddListener(delegate { OnToggleClicked(); });
  25. }
  26. [ClientCallback]
  27. public void OnToggleClicked()
  28. {
  29. canvasController.SelectMatch(toggleButton.isOn ? matchId : Guid.Empty);
  30. image.color = toggleButton.isOn ? new Color(0f, 1f, 0f, 0.5f) : new Color(1f, 1f, 1f, 0.2f);
  31. }
  32. [ClientCallback]
  33. public Guid GetMatchId() => matchId;
  34. [ClientCallback]
  35. public void SetMatchInfo(MatchInfo infos)
  36. {
  37. matchId = infos.matchId;
  38. matchName.text = $"Match {infos.matchId.ToString().Substring(0, 8)}";
  39. playerCount.text = $"{infos.players} / {infos.maxPlayers}";
  40. }
  41. }
  42. }