RoomOtherUserPanel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Blue;
  5. using Newtonsoft.Json;
  6. public class RoomOtherUserPanel : MonoBehaviour, IController
  7. {
  8. public GameObject mOtherUserItem;
  9. private Dictionary<int, RTCUserInfo> UserDIc;
  10. public Dictionary<int, RoomOtherUserItem> mItemDIc = new Dictionary<int, RoomOtherUserItem>(); // 绑定RTCUserInfo的RoomOtherUserItem
  11. private IRTCService mRTCService;
  12. private void Start()
  13. {
  14. mRTCService = this.GetService<IRTCService>();
  15. this.RegisterEvent<NoEnterRoomEvent>(e =>
  16. {
  17. if (UserDIc.ContainsKey(e.UserID))
  18. {
  19. UserDIc.Remove(e.UserID);
  20. if(mItemDIc.ContainsKey(e.UserID))
  21. mItemDIc.Remove(e.UserID);
  22. }
  23. StartCoroutine(RefreshUI());
  24. }).UnRegisterWhenGameObjectDestroyed(gameObject);
  25. this.RegisterEvent<OtherUserJoinRoomEvent>(OtherUserJoinRoom).UnRegisterWhenGameObjectDestroyed(gameObject);
  26. this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom).UnRegisterWhenGameObjectDestroyed(gameObject);
  27. }
  28. private Vector3 mPosEffect = Vector3.zero; // 位置偏移
  29. /// <summary>
  30. /// 邀请用户
  31. /// </summary>
  32. public void ExeCuteInvite(Dictionary<int, RTCUserInfo> mUserDIc)
  33. {
  34. UserDIc = mUserDIc;
  35. foreach (RTCUserInfo userInfo in mUserDIc.Values)
  36. {
  37. GameObject go = Instantiate(mOtherUserItem, transform);
  38. go.name = "OtherUserItem_" + transform.childCount.ToString();
  39. go.transform.SetAsLastSibling();
  40. go.transform.localPosition = mPosEffect;
  41. RoomOtherUserItem RoomOtherUserItem = go.GetComponent<RoomOtherUserItem>();
  42. RoomOtherUserItem.Init(userInfo);
  43. go.SetActive(true);
  44. mPosEffect.x += 60f;
  45. mItemDIc.Add(userInfo.UserID,RoomOtherUserItem);
  46. }
  47. }
  48. /// <summary>
  49. /// 用户加入,主动进入
  50. /// </summary>
  51. private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
  52. {
  53. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  54. if(!UserDIc.ContainsKey(RTCUserInfo.UserID))
  55. {
  56. GameObject go = Instantiate(mOtherUserItem, transform);
  57. go.name = "OtherUserItem_" + transform.childCount.ToString();
  58. go.transform.SetAsLastSibling();
  59. go.transform.localPosition = mPosEffect;
  60. RoomOtherUserItem RoomOtherUserItem = go.GetComponent<RoomOtherUserItem>();
  61. RoomOtherUserItem.Init(RTCUserInfo,true);
  62. go.SetActive(true);
  63. mPosEffect.x += 60f;
  64. UserDIc.Add(RTCUserInfo.UserID,RTCUserInfo);
  65. mItemDIc.Add(RTCUserInfo.UserID,RoomOtherUserItem);
  66. }
  67. }
  68. /// <summary>
  69. /// 用户退出,主动
  70. /// </summary>
  71. private void OtherLeaveRoom(OtherLeaveRoomEvent e)
  72. {
  73. RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(e.rtcUserInfoJsonString);
  74. if(UserDIc.ContainsKey(RTCUserInfo.UserID))
  75. {
  76. UserDIc.Remove(RTCUserInfo.UserID);
  77. if(mItemDIc.ContainsKey(RTCUserInfo.UserID))
  78. {
  79. Destroy(mItemDIc[RTCUserInfo.UserID].gameObject);
  80. mItemDIc.Remove(RTCUserInfo.UserID);
  81. StartCoroutine(RefreshUI());
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// 刷新UI,有用户拒绝进入、无响应时执行
  87. /// </summary>
  88. public IEnumerator RefreshUI()
  89. {
  90. yield return null;
  91. mPosEffect = Vector3.zero;
  92. for (int i = 0; i < transform.childCount; i++)
  93. {
  94. Transform go = transform.GetChild(i);
  95. go.localPosition = mPosEffect;
  96. mPosEffect.x += 60;
  97. }
  98. }
  99. public void Reset()
  100. {
  101. foreach (RoomOtherUserItem item in mItemDIc.Values)// 在房间用户Item图标
  102. {
  103. //Debug.LogError($"销毁:{item.name}");
  104. Destroy(item.gameObject);
  105. }
  106. mItemDIc.Clear();
  107. mPosEffect = Vector3.zero;
  108. }
  109. }