TH1/Unity/Assets/Scripts/TH1_UI/View/Bottom/UIBottomNetView.cs
2026-01-10 21:33:47 +08:00

122 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 GameObject NetRowPrefab;
public RectTransform NetRowContainer;
// NetInfo related
public Transform NetInfo;
private List<UIBottomNetRowMono> _netInfoRowList = null;
private bool _initialized = false;
protected override void OnInit()
{
base.OnInit();
_netInfoRowList = new List<UIBottomNetRowMono>();
}
public void SetContent()
{
Init();
UpdateView();
}
//Init将确定进入游戏时所有房间成员的状态除非有新成员加入否则不会删减
private void Init()
{
_initialized = true;
if(Main.MapData == null) return;
bool isMulti = Main.MapData.Net.Mode == NetMode.Multi;
NetInfo.gameObject.SetActive(isMulti);
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<UIBottomNetRowMono>();
if (cpn == null) break;
_netInfoRowList.Add(cpn);
}
int i = 0;
foreach(var pair in mids)
{
_netInfoRowList[i].gameObject.SetActive(true);
_netInfoRowList[i].SetContent(pair.Key, pair.Value, pair.Key == lobbyInfo.GetLobbyOwnerId(), true);
i++;
}
while(i < _netInfoRowList.Count)
{
_netInfoRowList[i].gameObject.SetActive(false);
i++;
}
}
//UpdateView只会修改NetStatus和房主标记
public void UpdateView()
{
if (!_initialized) return;
if(Main.MapData == null) return;
bool isMulti = Main.MapData.Net.Mode == NetMode.Multi;
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();
}
public void CloseView()
{
_initialized = false;
//AudioManager.Instance.StopMusic();
}
}
}