unityzgy/Assets/Scripts/UIScripts/Teacher/PageShowData/ShowMsgByPageBase.cs
ayuan9957 bf12e02276 feat: 多语言本地化系统 - 支持中/英/法/俄实时切换
- LocalizationManager: 新增翻译表(SetFormatted/SetDuration/LocalizeOperateMsg)
- LoginPanel: InputField placeholder本地化、字体颜色保持
- HistoryPanel: 用时数据本地化、placeholder本地化
- RecordDetailPanel: 操作详情消息本地化(LanguageChanged重建)
- AppraiseWindowBase: 评价等级本地化、操作消息重建
- EditorAppraiser: 所有评估消息改用InjectOperateMsgLocalized
- StudentOperateRecorder: 新增InjectOperateMsgLocalized方法
- LocalizationLanguageTestBar: 单例模式、ScreenSpaceOverlay筛选
- 字体切换时保留颜色和verticalOverflow
2026-07-16 10:05:59 +08:00

231 lines
6.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using XFramework.Objects;
/// <summary>
/// 一个可以逐页展示数据的窗口控制器 需要和group 配合使用 只关注子物体数量 初始化方法、更新方法 其他不做关注
/// </summary>
public class ShowMsgByPageBase : MonoBehaviour
{
public GameObject showMessPrefab; //子物体预制件
List<GameObject> showPool = new List<GameObject>();//当前显示或隐藏的所有子物体
int Page = 1;//默认显示第一页
int msgCount;//数据数量
int allPage;//总页数
public int onePageCount = 10;//单页显示的数量
Text showPage;
UnityAction<GameObject> initChildEvent;//初始化子物体 由外部注入确定UpdataChildFun 子物体首次创建时调用 只执行一次
UnityAction<int, GameObject> updateChildEvent;//更新子物体 由外部注入确定UpdataChildFun 子物体每次更新时调用
// Use this for initialization
void Awake()
{
InitComponent();
}
void InitComponent()
{
this.transform.parent.Find("Button_nextPage").GetComponent<Button>().onClick.AddListener(NextPage);
this.transform.parent.Find("Button_lastPage").GetComponent<Button>().onClick.AddListener(LastPage);
showPage = this.transform.parent.Find("Text_page").GetComponent<Text>();
}
///// <summary>
///// 注入数据前需要先注册界面 指定待创建的子物体 和子物体初始化方法 未确定此思路 待定
///// </summary>
///// <param name="initChildFun">初始化子物体的方法 int 数据索引 GameObject 带初始化的子物体 </param>
///// <param name="childPrefab"> 子物体预制件</param>
//public void RegistePanel(UnityAction<int, GameObject> initChildFun,GameObject childPrefab)
//{
// if (showMessPrefab!=null&& childPrefab != showMessPrefab)//指定了新的预制件 清空界面
// {
// for (int i = 0; i < showPool.Count; i++)//
// {
// Destroy(showPool[i]);
// }
// showPool = new List<GameObject>();
// }
//}
/// <summary>
/// 注入待展示的所有数据 覆盖此前数据
/// </summary>
/// <param name="count">待展示的数据总数</param>
/// <param name="initChildFun">子物体创建时的初始化方法</param>
/// <param name="UpdataChildFun">更新子物体的方法</param>
public virtual void InitPanel(int count, UnityAction<GameObject> initChildFun, UnityAction<int, GameObject> UpdataChildFun)
{
initChildEvent = initChildFun;
updateChildEvent = UpdataChildFun;
ShowData(count);
}
/// <summary>
/// 初始化后可以直接 传入数据数量 更新界面
/// </summary>
public void ShowData(int count)
{
msgCount = count;
GetMaxPage();
Page = 1;
ShowOnePageMessage(Page);
}
/// <summary>
/// 添加数据 当前在最大页数 计算是否需要增加页数 并更新界面
/// </summary>
/// <param name="count"></param>
public virtual void AddChild(int count)
{
if (msgCount == 0)
{
Debug.LogError("未注入原始数据前不能添加");
}
else
{
msgCount += count;
if (Page == allPage)//
{
GetMaxPage();
ShowOnePageMessage(Page);
}
}
}
/// <summary>
/// 减少数据 并更新界面
/// </summary>
/// <param name="count"></param>
public virtual void RemoveChild(int count)
{
if (msgCount == 0)
Debug.LogError("当前无数据");
else if (msgCount > count)
{
msgCount -= count;
GetMaxPage();
if (Page > allPage)//减少到当前页码超出最大页码时 更新当前页码
Page = allPage;
ShowOnePageMessage(Page);
}
}
//显示一页数据
void ShowOnePageMessage(int pageNumber)
{
HideAllMessage();
UpdatePageText(Page, allPage);
int maxIndex;//计算当前页数 数据的最大索引
maxIndex = pageNumber * onePageCount > msgCount ? msgCount : pageNumber * onePageCount;
for (int i = (pageNumber - 1) * onePageCount; i < maxIndex; i++)
{
//CreatShowMessageGO().GetComponent<MessagePrefabCtrl>().SetMessage(recordClasses[i]);
updateChildEvent?.Invoke(i, CreatShowMessageGO());
}
}
/// <summary>
/// 翻页
/// </summary>
/// <param name="turn"></param>
/// <returns>当前页码</returns>
int TurnPage(TurnPageType turn)
{
switch (turn)
{
case TurnPageType.Up:
if (Page > 1)
{
Page -= 1;
ShowOnePageMessage(Page);
}
break;
case TurnPageType.Down:
if (Page * onePageCount < msgCount)
{
Page += 1;
ShowOnePageMessage(Page);
}
break;
default:
break;
}
return Page;
}
//计算最大页数
private void GetMaxPage()
{
allPage = msgCount / onePageCount;
if (msgCount % onePageCount > 0)
{
allPage += 1;
}
}
/// <summary>
/// 隐藏所有信息
/// </summary>
void HideAllMessage()
{
for (int i = 0; i < showPool.Count; i++)
{
showPool[i].SetActive(false);
}
UpdatePageText(0, 0);
}
void NextPage()
{
TurnPage(TurnPageType.Down);
}
void LastPage()
{
TurnPage(TurnPageType.Up);
}
//更新页码显示
void UpdatePageText(int currentPage, int AllPage)
{
if (showPage == null) return;
LocalizationManager.SetFormatted(showPage, "第{0}页/共{1}页", currentPage, AllPage);
}
//展示一页数据时
GameObject CreatShowMessageGO()
{
GameObject returnGO = null;
for (int i = 0; i < showPool.Count; i++)
{
if (!showPool[i].gameObject.activeSelf)
{
returnGO = showPool[i];
returnGO.SetActive(true);
return returnGO;
}
}
returnGO = Instantiate(showMessPrefab);
returnGO.transform.SetParent(this.transform);
returnGO.transform.localScale = Vector3.one;
initChildEvent?.Invoke(returnGO);//新建物体需要初始化
showPool.Add(returnGO);
return returnGO;
}
void OnDisable()
{
HideAllMessage();
}
}