MultiplayChannel.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. namespace Unity.RenderStreaming.Samples
  5. {
  6. enum ActionType
  7. {
  8. ChangeLabel = 0,
  9. }
  10. [Serializable]
  11. class Message
  12. {
  13. public ActionType type;
  14. public string argument;
  15. }
  16. /// <summary>
  17. ///
  18. /// </summary>
  19. [Serializable]
  20. class ChangeLabelEvent : UnityEvent<string> {};
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. class MultiplayChannel : DataChannelBase
  25. {
  26. public ChangeLabelEvent OnChangeLabel;
  27. protected override void OnMessage(byte[] bytes)
  28. {
  29. string str = System.Text.Encoding.UTF8.GetString(bytes);
  30. var message = JsonUtility.FromJson<Message>(str);
  31. switch(message.type)
  32. {
  33. case ActionType.ChangeLabel:
  34. OnChangeLabel?.Invoke(message.argument);
  35. return;
  36. }
  37. }
  38. public void ChangeLabel(string text)
  39. {
  40. var msg = new Message
  41. {
  42. type = ActionType.ChangeLabel,
  43. argument = text
  44. };
  45. Send(JsonUtility.ToJson(msg));
  46. }
  47. }
  48. }