MultiplaySample.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Unity.RenderStreaming.Samples
  7. {
  8. class MultiplaySample : MonoBehaviour
  9. {
  10. [SerializeField] ToggleGroup toggleGroupRole;
  11. [SerializeField] InputField inputFieldUsername;
  12. [SerializeField] Button buttonStart;
  13. [SerializeField] SignalingManager renderStreaming;
  14. [SerializeField] GameObject prefabHost;
  15. [SerializeField] GameObject prefabGuest;
  16. [SerializeField] GameObject prefabPlayer;
  17. [SerializeField] GameObject menuCamera;
  18. [SerializeField] GameObject panel;
  19. [SerializeField] RawImage videoImage;
  20. [SerializeField] ShowStatsUI statsUI;
  21. enum Role
  22. {
  23. Host = 0,
  24. Guest = 1
  25. }
  26. private RenderStreamingSettings settings;
  27. private void Awake()
  28. {
  29. settings = SampleManager.Instance.Settings;
  30. }
  31. void Start()
  32. {
  33. buttonStart.onClick.AddListener(OnClickButtonStart);
  34. inputFieldUsername.text = UnityEngine.Random.Range(0, 99999).ToString("00000");
  35. inputFieldUsername.onValueChanged.AddListener(OnValueChangedUserName);
  36. }
  37. void OnValueChangedUserName(string value)
  38. {
  39. bool hasNullValue =
  40. string.IsNullOrEmpty(inputFieldUsername.text);
  41. buttonStart.interactable = !hasNullValue;
  42. }
  43. void OnClickButtonStart()
  44. {
  45. var username = inputFieldUsername.text;
  46. var connectionId = Guid.NewGuid().ToString();
  47. var toggles = toggleGroupRole.GetComponentsInChildren<Toggle>();
  48. var activeToggle = toggleGroupRole.ActiveToggles().First();
  49. var indexRole = Array.FindIndex(toggles, _ => _ == activeToggle);
  50. var role = (Role)indexRole;
  51. panel.SetActive(false);
  52. switch (role)
  53. {
  54. case Role.Host:
  55. SetUpHost(username);
  56. break;
  57. case Role.Guest:
  58. StartCoroutine(SetUpGuest(username, connectionId));
  59. break;
  60. }
  61. }
  62. void SetUpHost(string username)
  63. {
  64. menuCamera.SetActive(false);
  65. var instance = GameObject.Instantiate(prefabHost);
  66. var handler = instance.GetComponent<Multiplay>();
  67. // host player
  68. var hostPlayer = GameObject.Instantiate(prefabPlayer);
  69. var playerController = hostPlayer.GetComponent<PlayerController>();
  70. playerController.SetLabel(username);
  71. var playerInput = hostPlayer.GetComponent<InputReceiver>();
  72. playerInput.PerformPairingWithAllLocalDevices();
  73. playerController.CheckPairedDevices();
  74. statsUI.AddSignalingHandler(handler);
  75. if (settings != null)
  76. renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
  77. if (settings?.SignalingSettings != null)
  78. renderStreaming.SetSignalingSettings(settings.SignalingSettings);
  79. renderStreaming.Run(handlers: new SignalingHandlerBase[] {handler});
  80. }
  81. IEnumerator SetUpGuest(string username, string connectionId)
  82. {
  83. var guestPlayer = GameObject.Instantiate(prefabGuest);
  84. var handler = guestPlayer.GetComponent<SingleConnection>();
  85. statsUI.AddSignalingHandler(handler);
  86. if (settings != null)
  87. renderStreaming.useDefaultSettings = settings.UseDefaultSettings;
  88. if (settings?.SignalingSettings != null)
  89. renderStreaming.SetSignalingSettings(settings.SignalingSettings);
  90. renderStreaming.Run(handlers: new SignalingHandlerBase[] {handler});
  91. videoImage.gameObject.SetActive(true);
  92. var receiveVideoViewer = guestPlayer.GetComponent<VideoStreamReceiver>();
  93. receiveVideoViewer.OnUpdateReceiveTexture += texture => videoImage.texture = texture;
  94. var channel = guestPlayer.GetComponent<MultiplayChannel>();
  95. channel.OnStartedChannel += _ => { StartCoroutine(ChangeLabel(channel, username)); };
  96. if(settings != null)
  97. receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);
  98. // todo(kazuki):
  99. yield return new WaitForSeconds(1f);
  100. handler.CreateConnection(connectionId);
  101. yield return new WaitUntil(() => handler.IsConnected(connectionId));
  102. }
  103. IEnumerator ChangeLabel(MultiplayChannel channel, string username)
  104. {
  105. yield return new WaitUntil(() => channel.IsConnected);
  106. channel.ChangeLabel(username);
  107. }
  108. }
  109. }