using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
/// <summary>
|
/// 游戏主界面
|
/// </summary>
|
public class MainWin : UIBase
|
{
|
private GameObject windowBackground;
|
|
// 底部按钮组
|
private Button[] bottomTabButtons;
|
|
private GameObject[] bottomTabEffects;
|
|
// 当前选中的底部标签索引
|
private int currentTabIndex = 0;
|
|
// 当前打开的子界面
|
private UIBase currentSubUI;
|
|
/// <summary>
|
/// 初始化组件
|
/// </summary>
|
protected override void InitComponent()
|
{
|
base.InitComponent();
|
|
windowBackground = transform.Find("RawImgBackground").gameObject;
|
|
bottomTabButtons = new Button[5];
|
for (int i = 1; i <= 5; i++)
|
{
|
string buttonName = "Buttons/Button" + i;
|
bottomTabButtons[i-1] = transform.Find(buttonName).GetComponent<Button>();
|
#if UNITY_EDITOR
|
//测试代码
|
#endif
|
}
|
|
// 初始化UI组件事件
|
InitButtonEvents();
|
}
|
|
/// <summary>
|
/// 初始化UI组件事件
|
/// </summary>
|
private void InitButtonEvents()
|
{
|
// 初始化底部按钮
|
for (int i = 0; i < bottomTabButtons.Length; i++)
|
{
|
int index = i; // 捕获索引
|
bottomTabButtons[i].onClick.AddListener(() => {
|
OnBottomTabButtonClicked(index);
|
});
|
}
|
|
}
|
|
protected override void OnOpen()
|
{
|
base.OnOpen();
|
|
// 默认选中第一个标签
|
SelectBottomTab(0);
|
|
// 刷新UI
|
Refresh();
|
}
|
|
public override void Refresh()
|
{
|
UpdatePlayerInfo();
|
UpdateCurrency();
|
}
|
|
protected override void OnPreOpen()
|
{
|
base.OnPreOpen();
|
bottomTabEffects = new GameObject[bottomTabButtons.Length];
|
for (int i = 0; i < bottomTabButtons.Length; i++)
|
{
|
bottomTabEffects[i] = PlayUIEffect(1004, bottomTabButtons[i].transform, false);
|
}
|
}
|
|
protected override void OnPreClose()
|
{
|
base.OnPreClose();
|
foreach (var effectGO in bottomTabEffects)
|
{
|
DestroyImmediate(effectGO);
|
}
|
bottomTabEffects = null;
|
}
|
|
/// <summary>
|
/// 更新玩家信息
|
/// </summary>
|
private void UpdatePlayerInfo()
|
{
|
// 从玩家数据中获取信息并更新UI
|
// 例如:
|
// playerNameText.text = PlayerData.Instance.Name;
|
// playerLevelText.text = "Lv." + PlayerData.Instance.Level;
|
// powerText.text = PlayerData.Instance.Power.ToString();
|
// expSlider.value = PlayerData.Instance.ExpRatio;
|
}
|
|
/// <summary>
|
/// 更新货币信息
|
/// </summary>
|
private void UpdateCurrency()
|
{
|
// 从玩家数据中获取货币信息并更新UI
|
// 例如:
|
// goldText.text = PlayerData.Instance.Gold.ToString();
|
// diamondText.text = PlayerData.Instance.Diamond.ToString();
|
// energyText.text = PlayerData.Instance.Energy + "/" + PlayerData.Instance.MaxEnergy;
|
}
|
|
/// <summary>
|
/// 底部标签按钮点击
|
/// </summary>
|
private void OnBottomTabButtonClicked(int index)
|
{
|
SelectBottomTab(index);
|
}
|
|
/// <summary>
|
/// 选择底部标签
|
/// </summary>
|
private void SelectBottomTab(int index)
|
{
|
// 如果点击当前已选中的标签,不做处理
|
if (currentTabIndex == index && currentSubUI != null)
|
{
|
return;
|
}
|
|
// 更新当前选中的标签索引
|
currentTabIndex = index;
|
|
// 更新按钮状态
|
UpdateButtonsState();
|
|
// 关闭当前打开的子界面
|
CloseCurrentSubUI();
|
|
// 根据选中的标签打开对应的界面
|
OpenSubUIByTabIndex(index);
|
}
|
|
/// <summary>
|
/// 更新按钮状态
|
/// </summary>
|
private void UpdateButtonsState()
|
{
|
// 遍历所有按钮,设置选中状态
|
for (int i = 0; i < bottomTabButtons.Length; i++)
|
{
|
// 这里可以根据是否选中设置按钮的视觉效果
|
// 例如:改变图片、颜色等
|
// bottomTabButtons[i].GetComponent<Image>().color = (i == currentTabIndex) ? Color.blue : Color.white;
|
|
// 或者激活/禁用选中图标
|
bottomTabButtons[i].image.color = (i == currentTabIndex) ? Color.white : Color.gray;
|
}
|
}
|
|
/// <summary>
|
/// 关闭当前打开的子界面
|
/// </summary>
|
private void CloseCurrentSubUI()
|
{
|
if (currentSubUI != null)
|
{
|
// 关闭当前界面
|
currentSubUI.CloseWindow();
|
currentSubUI = null;
|
}
|
}
|
|
/// <summary>
|
/// 根据标签索引打开对应的子界面
|
/// </summary>
|
private void OpenSubUIByTabIndex(int index)
|
{
|
|
Debug.Log("打开子界面 : " + index);
|
// 主城 阵容 同盟 福利 冒险
|
windowBackground.SetActive(index != 4);
|
//根据索引打开不同的界面
|
switch (index)
|
{
|
case 0:
|
// 例如:打开主页界面
|
// currentSubUI = UIManager.Instance.OpenUI<HomeUI>();
|
Debug.Log("打开主城界面");
|
break;
|
case 1:
|
// 例如:打开角色界面
|
// currentSubUI = UIManager.Instance.OpenUI<CharacterUI>();
|
Debug.Log("打开阵容界面");
|
break;
|
case 2:
|
// 例如:打开背包界面
|
// currentSubUI = UIManager.Instance.OpenUI<BagUI>();
|
Debug.Log("打开同盟界面");
|
break;
|
case 3:
|
// 例如:打开任务界面
|
// currentSubUI = UIManager.Instance.OpenUI<QuestUI>();
|
Debug.Log("打开福利界面");
|
break;
|
case 4:
|
// 例如:打开设置界面
|
currentSubUI = UIManager.Instance.OpenWindow<PlaceWin>();
|
Debug.Log("打开冒险界面");
|
break;
|
default:
|
Debug.LogWarning("未知的标签索引: " + index);
|
break;
|
}
|
}
|
}
|