_xiaofang/xiaofang/Assets/Res/gsj/scripts/PlayBack.cs
2024-12-20 10:10:19 +08:00

358 lines
12 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.UI;
//管理角色信息的类
[System.Serializable]
public class RoleInfo
{
public string roleName; // 角色名称
public string jobTitle; // 职业名称
public string trainingDate; // 演练日期
public string scenarioName; // 场景名称
public string status; // 演练状态
public int operationErrors; // 操作错误次数
public int injuries; // 受伤次数
public bool extinguishFire; // 补灭明火状态
public bool evacuationStatus; // 安全疏散状态
}
public class PlayBack : MonoBehaviour
{
private Button backBtn;//回放按钮
private GameObject playback6;
private GameObject playback5;
//private Button recordbtn;
private GameObject Record;//录音的界面
private GameObject right1;
private GameObject right2;
private bool isrecord = false;//是否在录音界面
private GameObject mid;
public bool ishost;//是否为主持人
private bool lastHostState; // 保存上一次的 ishost 状态,便于检测变化
// 左侧按钮和页面
public Transform leftContainer; // 左侧按钮的父物体
public Transform pagesContainer; // 页面容器的父物体
public GameObject buttonPrefab; // 按钮预制体
public GameObject pagePrefab; // 页面预制体
public Sprite selectedSprite; // 按钮选中时的背景
public Sprite defaultSprite; // 按钮默认状态的背景
private List<Button> navButtons = new List<Button>(); // 左侧按钮列表
private List<GameObject> pages = new List<GameObject>(); // 页面列表
// 子物体信息
public GameObject peoplePrefab; // 子物体的预制体
private Dictionary<int, List<GameObject>> pageItems = new Dictionary<int, List<GameObject>>(); // 每个页面的子物体
private Dictionary<int, List<GameObject>> pageHighlightBars = new Dictionary<int, List<GameObject>>(); // 每个页面的高光栏
// 右侧详细信息UI
private Text roleNameText, jobTitleText, trainingStatusText, operationErrorsText, injuriesText;
private Text roleNameText2, jobTitleText2, trainingStatusText2, extinguishFireText, evacuationStatusText;
private List<RoleInfo> roleInfos = new List<RoleInfo>(); // 数据
private int currentPageIndex = 0; // 当前页面索引
private Dictionary<int, int> selectedItemIndex = new Dictionary<int, int>(); // 记录每个页面选中的子物体索引
void Start()
{
backBtn = transform.Find("backbtn").GetComponent<Button>();
backBtn.onClick.AddListener(OnClickBackBtn);
playback6 = transform.Find("playback6").gameObject;
playback5 = transform.Find("playback5").gameObject;
playback5.SetActive(false);
playback6.SetActive(false);
//recordbtn = transform.Find("Mid/left/recordImage").GetComponent<Button>();
//recordbtn.onClick.AddListener(OnRecordBtn);
Record = transform.Find("Mid/recording").gameObject;
Record.SetActive(false);
right1 = transform.Find("Mid/right1").gameObject;
right2 = transform.Find("Mid/right2").gameObject;
mid = transform.Find("Mid/mid").gameObject;
// 初始化角色数据
InitRoleData();
// 初始化右侧UI引用
InitializeRightUI();
// 创建左侧按钮和对应页面
for (int i = 0; i < 5; i++) // 假设有5个页面
{
CreatePageAndButton(i); // 动态创建按钮和页面
}
// 默认选中第一个页面
if (navButtons.Count > 0 && pages.Count > 0)
{
UpdatePage(0); // 默认显示第一个页面
}
UpdateVisibility();//更新右侧uI
}
void Update()
{
// 检查主持人状态变化
if (ishost != lastHostState)
{
UpdateVisibility(); // 更新右侧 UI 面板显示
lastHostState = ishost; // 保存最新的 ishost 状态
}
}
// 初始化角色数据
void InitRoleData()
{
for (int i = 0; i < 10; i++)
{
roleInfos.Add(new RoleInfo
{
roleName = $"角色名 {i + 1}",
jobTitle = $"职业 {i + 1}",
trainingDate = "2023-12-19",
scenarioName = "抢修救援组",
status = "演练中",
operationErrors = Random.Range(0, 10),
injuries = Random.Range(0, 5),
extinguishFire = (i % 2 == 0),
evacuationStatus = (i % 2 == 1)
});
}
}
// 初始化右侧UI
void InitializeRightUI()
{
// 非主持人界面
roleNameText = transform.Find("Mid/right1/juese").GetComponent<Text>();
jobTitleText = transform.Find("Mid/right1/occ").GetComponent<Text>();
trainingStatusText = transform.Find("Mid/right1/detail").GetComponent<Text>();
operationErrorsText = transform.Find("Mid/right1/blunder/count").GetComponent<Text>();
injuriesText = transform.Find("Mid/right1/hurt/count").GetComponent<Text>();
// 主持人界面
roleNameText2 = transform.Find("Mid/right2/juese").GetComponent<Text>();
jobTitleText2 = transform.Find("Mid/right2/occ").GetComponent<Text>();
trainingStatusText2 = transform.Find("Mid/right2/detail").GetComponent<Text>();
extinguishFireText = transform.Find("Mid/right2/fire/isfire").GetComponent<Text>();
evacuationStatusText = transform.Find("Mid/right2/safe/issafe").GetComponent<Text>();
}
// 动态创建页面和对应按钮
void CreatePageAndButton(int pageIndex)
{
// 创建页面
GameObject newPage = Instantiate(pagePrefab, pagesContainer);
newPage.name = $"Page_{pageIndex + 1}";
pages.Add(newPage);
// 为每个页面生成 ScrollView 子物体
Transform contentParent = newPage.transform.Find("Scroll View/Viewport/Content");
List<GameObject> items = new List<GameObject>();
List<GameObject> highlightBars = new List<GameObject>();
for (int i = 0; i < roleInfos.Count; i++)
{
GameObject item = Instantiate(peoplePrefab, contentParent);
Button button = item.GetComponent<Button>();
if (button != null)
{
int idx = i; // 防止闭包问题
button.onClick.AddListener(() => OnItemClicked(pageIndex, idx)); // 绑定点击事件
}
items.Add(item);
// 设置高光栏
GameObject highlightBar = item.transform.Find("Image").gameObject;
highlightBar.SetActive(false);
highlightBars.Add(highlightBar);
}
pageItems[pageIndex] = items;
pageHighlightBars[pageIndex] = highlightBars;
// 默认第一个子物体高亮
if (highlightBars.Count > 0)
{
highlightBars[0].SetActive(true);
}
// 创建按钮
GameObject newButton = Instantiate(buttonPrefab, leftContainer);
Button btn = newButton.GetComponent<Button>();
if (btn != null)
{
navButtons.Add(btn);
int index = pageIndex; // 防止闭包问题
btn.onClick.AddListener(() => OnNavButtonClicked(index));
}
}
// 点击左侧按钮时切换页面
void OnNavButtonClicked(int index)
{
UpdatePage(index);
}
// 更新页面显示
void UpdatePage(int index)
{
if (index < 0 || index >= pages.Count) return;
// 切换页面显示
for (int i = 0; i < pages.Count; i++)
{
pages[i].SetActive(i == index);
}
// 更新按钮状态
for (int i = 0; i < navButtons.Count; i++)
{
Image img = navButtons[i].GetComponent<Image>();
img.sprite = (i == index) ? selectedSprite : defaultSprite;
}
currentPageIndex = index;
// 更新子物体高光状态
UpdateHighlightBars(index);
//// 更新右侧详细信息为第一个子物体
//if (pageItems.ContainsKey(index) && pageHighlightBars[index].Count > 0)
//{
// pageHighlightBars[index][0].SetActive(true);
// UpdateRightPanel(0);
//}
}
// 更新页面的高光栏状态
void UpdateHighlightBars(int pageIndex)
{
if (!pageItems.ContainsKey(pageIndex) || !pageHighlightBars.ContainsKey(pageIndex)) return;
// 获取当前页面选中的物体索引
int selectedIndex = selectedItemIndex.ContainsKey(pageIndex) ? selectedItemIndex[pageIndex] : 0;
// 确保索引有效
if (selectedIndex < 0 || selectedIndex >= pageHighlightBars[pageIndex].Count)
{
selectedIndex = 0; // 默认选中第一个物体
}
// 隐藏所有高光栏
foreach (var highlight in pageHighlightBars[pageIndex])
{
highlight.SetActive(false);
}
// 显示当前选中的高光栏
pageHighlightBars[pageIndex][selectedIndex].SetActive(true);
// 更新右侧详细信息
UpdateRightPanel(selectedIndex);
}
// 点击子物体时更新右侧信息
void OnItemClicked(int pageIndex, int itemIndex)
{
// 隐藏所有高光栏
foreach (var highlight in pageHighlightBars[pageIndex])
{
highlight.SetActive(false);
}
// 显示当前选中子物体的高光栏
pageHighlightBars[pageIndex][itemIndex].SetActive(true);
// 更新右侧详细信息
UpdateRightPanel(itemIndex);
}
// 更新右侧详细信息
void UpdateRightPanel(int index)
{
if (index < 0 || index >= roleInfos.Count) return;
RoleInfo info = roleInfos[index];
roleNameText.text = info.roleName;
jobTitleText.text = info.jobTitle;
trainingStatusText.text = $"在[{info.trainingDate}]的[{info.scenarioName}]演练\n状态: {info.status}";
operationErrorsText.text = $"{info.operationErrors}";
injuriesText.text = $"{info.injuries}";
if (info.extinguishFire)
{
extinguishFireText.text = "成功";
extinguishFireText.color = Color.green;
}
else
{
extinguishFireText.text = "失败";
extinguishFireText.color = Color.red;
}
if (info.evacuationStatus)
{
evacuationStatusText.text = "成功";
evacuationStatusText.color = Color.green;
}
else
{
evacuationStatusText.text = "失败";
evacuationStatusText.color = Color.red;
}
}
// 更新右侧 UI 可见性
void UpdateVisibility()
{
//// 根据 ishost 状态显示主持人或普通用户的视图
//Transform right1 = transform.Find("Mid/right1").gameObject.transform;
//Transform right2 = transform.Find("Mid/right2").gameObject.transform;
right1.gameObject.SetActive(!ishost);
right2.gameObject.SetActive(ishost);
}
void OnClickBackBtn()
{
if (!isrecord)
{
playback5.gameObject.SetActive(true);
}
if (isrecord)
{
playback6.gameObject.SetActive(true);
}
}
void OnRecordBtn()
{
Record.SetActive(true);
mid.SetActive(false);
right1.SetActive(false);
right2.SetActive(false);
isrecord = true;
}
void OnEnable()
{
// 订阅 FigureState 的状态变化事件
figurestate.OnFigureStateChanged += UpdateButtonState;
}
void OnDisable()
{
// 取消事件订阅,防止内存泄漏
figurestate.OnFigureStateChanged -= UpdateButtonState;
}
// 根据状态更新目标按钮
void UpdateButtonState(bool isActive, figurestate sender)
{
// 根据传递的状态设置按钮交互性
backBtn.interactable = !isActive;
Debug.Log($"Target Button interactable = {isActive}. Triggered by {sender.gameObject.name}");
}
}