using System.Collections; using System.Collections.Generic; using UnityEngine; using Blue; using Newtonsoft.Json; public class RoomOtherUserPanel : MonoBehaviour, IController { public GameObject mOtherUserItem; private Dictionary UserDIc; public Dictionary mItemDIc = new Dictionary(); // 绑定RTCUserInfo的RoomOtherUserItem private IRTCService mRTCService; private void Start() { mRTCService = this.GetService(); this.RegisterEvent(e => { if (UserDIc.ContainsKey(e.UserID)) { UserDIc.Remove(e.UserID); if(mItemDIc.ContainsKey(e.UserID)) mItemDIc.Remove(e.UserID); } StartCoroutine(RefreshUI()); }).UnRegisterWhenGameObjectDestroyed(gameObject); this.RegisterEvent(OtherUserJoinRoom).UnRegisterWhenGameObjectDestroyed(gameObject); this.RegisterEvent(OtherLeaveRoom).UnRegisterWhenGameObjectDestroyed(gameObject); } private Vector3 mPosEffect = Vector3.zero; // 位置偏移 /// /// 邀请用户 /// public void ExeCuteInvite(Dictionary mUserDIc) { UserDIc = mUserDIc; foreach (RTCUserInfo userInfo in mUserDIc.Values) { GameObject go = Instantiate(mOtherUserItem, transform); go.name = "OtherUserItem_" + transform.childCount.ToString(); go.transform.SetAsLastSibling(); go.transform.localPosition = mPosEffect; RoomOtherUserItem RoomOtherUserItem = go.GetComponent(); RoomOtherUserItem.Init(userInfo); go.SetActive(true); mPosEffect.x += 60f; mItemDIc.Add(userInfo.UserID,RoomOtherUserItem); } } /// /// 用户加入,主动进入 /// private void OtherUserJoinRoom(OtherUserJoinRoomEvent e) { RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject(e.rtcUserInfoJsonString); if(!UserDIc.ContainsKey(RTCUserInfo.UserID)) { GameObject go = Instantiate(mOtherUserItem, transform); go.name = "OtherUserItem_" + transform.childCount.ToString(); go.transform.SetAsLastSibling(); go.transform.localPosition = mPosEffect; RoomOtherUserItem RoomOtherUserItem = go.GetComponent(); RoomOtherUserItem.Init(RTCUserInfo,true); go.SetActive(true); mPosEffect.x += 60f; UserDIc.Add(RTCUserInfo.UserID,RTCUserInfo); mItemDIc.Add(RTCUserInfo.UserID,RoomOtherUserItem); } } /// /// 用户退出,主动 /// private void OtherLeaveRoom(OtherLeaveRoomEvent e) { RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject(e.rtcUserInfoJsonString); if(UserDIc.ContainsKey(RTCUserInfo.UserID)) { UserDIc.Remove(RTCUserInfo.UserID); if(mItemDIc.ContainsKey(RTCUserInfo.UserID)) { Destroy(mItemDIc[RTCUserInfo.UserID].gameObject); mItemDIc.Remove(RTCUserInfo.UserID); StartCoroutine(RefreshUI()); } } } /// /// 刷新UI,有用户拒绝进入、无响应时执行 /// public IEnumerator RefreshUI() { yield return null; mPosEffect = Vector3.zero; for (int i = 0; i < transform.childCount; i++) { Transform go = transform.GetChild(i); go.localPosition = mPosEffect; mPosEffect.x += 60; } } public void Reset() { foreach (RoomOtherUserItem item in mItemDIc.Values)// 在房间用户Item图标 { //Debug.LogError($"销毁:{item.name}"); Destroy(item.gameObject); } mItemDIc.Clear(); mPosEffect = Vector3.zero; } }