123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Blue;
- using Newtonsoft.Json;
- public class RoomOtherUserPanel : MonoBehaviour, IController
- {
- public GameObject mOtherUserItem;
- private Dictionary<int, RTCUserInfo> UserDIc;
- public Dictionary<int, RoomOtherUserItem> mItemDIc = new Dictionary<int, RoomOtherUserItem>(); // 绑定RTCUserInfo的RoomOtherUserItem
- private IRTCService mRTCService;
- private void Start()
- {
- mRTCService = this.GetService<IRTCService>();
- this.RegisterEvent<NoEnterRoomEvent>(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<OtherUserJoinRoomEvent>(OtherUserJoinRoom).UnRegisterWhenGameObjectDestroyed(gameObject);
- this.RegisterEvent<OtherLeaveRoomEvent>(OtherLeaveRoom).UnRegisterWhenGameObjectDestroyed(gameObject);
- }
- private Vector3 mPosEffect = Vector3.zero; // 位置偏移
- /// <summary>
- /// 邀请用户
- /// </summary>
- public void ExeCuteInvite(Dictionary<int, RTCUserInfo> 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>();
- RoomOtherUserItem.Init(userInfo);
- go.SetActive(true);
- mPosEffect.x += 60f;
- mItemDIc.Add(userInfo.UserID,RoomOtherUserItem);
- }
- }
- /// <summary>
- /// 用户加入,主动进入
- /// </summary>
- private void OtherUserJoinRoom(OtherUserJoinRoomEvent e)
- {
- RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(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>();
- RoomOtherUserItem.Init(RTCUserInfo,true);
- go.SetActive(true);
- mPosEffect.x += 60f;
- UserDIc.Add(RTCUserInfo.UserID,RTCUserInfo);
- mItemDIc.Add(RTCUserInfo.UserID,RoomOtherUserItem);
- }
- }
- /// <summary>
- /// 用户退出,主动
- /// </summary>
- private void OtherLeaveRoom(OtherLeaveRoomEvent e)
- {
- RTCUserInfo RTCUserInfo = JsonConvert.DeserializeObject<RTCUserInfo>(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());
- }
- }
- }
- /// <summary>
- /// 刷新UI,有用户拒绝进入、无响应时执行
- /// </summary>
- 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;
- }
- }
|