using System; using System.Collections.Generic; using Logic.Multilingual; using TH1_Logic.Net; using TH1_Logic.Steam; using TH1Renderer; using TH1Resource; using TMPro; using UnityEngine; using UnityEngine.UI; using RuntimeData; using TH1_Logic.Core; using UnityEngine.Serialization; namespace TH1_UI.View.Bottom { public class UIBottomNetView : Base.View { public ViDelegateAssisstant.Dele OnBtnCloseClick; public ViDelegateAssisstant.Dele OnChatMessageSend; public GameObject NetRowPrefab; public RectTransform NetRowContainer; // NetInfo related public RectTransform NetInfo; public RectTransform ScrollViewRect; // ChatArea public UIBottomNetChatAreaMono ChatArea; private List _netInfoRowList = null; private bool _initialized = false; protected override void OnInit() { base.OnInit(); _netInfoRowList = new List(); } public void SetContent() { Init(); UpdateView(); } //Init将确定进入游戏时所有房间成员的状态,除非有新成员加入,否则不会删减 private void Init() { _initialized = true; if(Main.MapData == null) return; bool isMulti = Main.MapData.Net.Mode == NetMode.Multi; if (NetInfo != null) NetInfo.gameObject.SetActive(isMulti); // ChatArea 跟随 NetInfo 一起初始化(每局重新订阅 OnSendMessage) if (ChatArea != null) { ChatArea.OnSendMessage -= OnChatMessageSendInternal; ChatArea.OnSendMessage += OnChatMessageSendInternal; ChatArea.Init(); } if (!isMulti) return; var lobbyInfo = LobbyManager.Instance.Lobby as SteamLobbyManager; if (lobbyInfo == null) return; var mids = lobbyInfo.GetAllMemberInfo(); while (_netInfoRowList.Count < mids.Count) { var obj = Instantiate(NetRowPrefab, NetRowContainer); var cpn = obj.GetComponent(); if (cpn == null) break; _netInfoRowList.Add(cpn); } int i = 0; foreach(var pair in mids) { _netInfoRowList[i].gameObject.SetActive(true); _netInfoRowList[i].InitContent(pair.Key, pair.Value, pair.Key == lobbyInfo.GetLobbyOwnerId(), true); i++; } int activeCount = i; while(i < _netInfoRowList.Count) { _netInfoRowList[i].gameObject.SetActive(false); i++; } ResizeScrollView(activeCount); } private void ResizeScrollView(int activeRowCount) { if (ScrollViewRect == null || activeRowCount <= 0) return; float rowHeight = ((RectTransform)NetRowPrefab.transform).sizeDelta.y; var layout = NetRowContainer.GetComponent(); float spacing = layout != null ? layout.spacing : 0f; float totalHeight = activeRowCount * rowHeight + Mathf.Max(0, activeRowCount - 1) * spacing; if (layout != null) totalHeight += layout.padding.top + layout.padding.bottom; var sd = ScrollViewRect.sizeDelta; sd.y = totalHeight; ScrollViewRect.sizeDelta = sd; } //UpdateView只会修改NetStatus和房主标记 public void UpdateView() { if (!_initialized) return; if(Main.MapData == null) return; bool isMulti = Main.MapData.Net.Mode == NetMode.Multi; if (NetInfo != null) NetInfo.gameObject.SetActive(isMulti); if (!isMulti) return; var lobbyInfo = LobbyManager.Instance.Lobby as SteamLobbyManager; if (lobbyInfo == null) return; var mids = lobbyInfo.GetAllMemberInfo(); //如果发现人数变多了,重新Init(比如继续一个3人局游戏,开始进来的时候只有2人 if (mids.Count > _netInfoRowList.Count) Init(); for (int i = 0; i < _netInfoRowList.Count; i++) { if (!_netInfoRowList[i].gameObject.activeSelf) break; bool isOnline = false; bool isRoomOwner = false; foreach(var pair in mids) if (pair.Key == _netInfoRowList[i].MemberId) { isOnline = true; isRoomOwner = pair.Key == lobbyInfo.GetLobbyOwnerId(); break; } _netInfoRowList[i].UpdateNetStatus(isRoomOwner, isOnline); } } private void Update() { UpdateView(); } private void OnChatMessageSendInternal(string message) { OnChatMessageSend?.Invoke(message); } public void CloseView() { _initialized = false; // 清理 ChatArea if (ChatArea != null) { ChatArea.OnSendMessage -= OnChatMessageSendInternal; ChatArea.Shutdown(); } //AudioManager.Instance.StopMusic(); } } }