PeerListView.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. namespace Unity.WebRTC.Editor
  5. {
  6. internal delegate void OnChangeSelectPeerHandler(RTCPeerConnection peer);
  7. internal class PeerListView
  8. {
  9. private static readonly Color ButtonBackground = new Color(70 / 255f, 70 / 255f, 70 / 255f);
  10. public event OnChangeSelectPeerHandler OnChangePeer;
  11. private WebRTCStats m_parent;
  12. public PeerListView(WebRTCStats parent)
  13. {
  14. m_parent = parent;
  15. }
  16. public VisualElement Create()
  17. {
  18. var root = new ScrollView();
  19. var container = new VisualElement();
  20. root.Add(container);
  21. m_parent.OnPeerList += peerList =>
  22. {
  23. container.Clear();
  24. foreach (var weakReference in peerList)
  25. {
  26. if (!weakReference.TryGetTarget(out var peerConnection))
  27. {
  28. continue;
  29. }
  30. var button = new Button(() =>
  31. {
  32. OnChangePeer?.Invoke(peerConnection);
  33. }) {text = $"peer {peerConnection.GetHashCode()}",};
  34. if (EditorGUIUtility.isProSkin)
  35. {
  36. button.style.backgroundColor = ButtonBackground;
  37. }
  38. container.Add(button);
  39. }
  40. };
  41. return root;
  42. }
  43. }
  44. }