_xiaofang/xiaofang/Assets/Res/gsj/scripts/PlayBack.cs

358 lines
12 KiB
C#
Raw Normal View History

2024-12-20 10:10:19 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.UI;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
[System.Serializable]
public class RoleInfo
{
public string roleName; // <20><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
public string jobTitle; // ְҵ<D6B0><D2B5><EFBFBD><EFBFBD>
public string trainingDate; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public string scenarioName; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public string status; // <20><><EFBFBD><EFBFBD>״̬
public int operationErrors; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public int injuries; // <20><><EFBFBD>˴<EFBFBD><CBB4><EFBFBD>
public bool extinguishFire; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬
public bool evacuationStatus; // <20><>ȫ<EFBFBD><C8AB>ɢ״̬
}
public class PlayBack : MonoBehaviour
{
private Button backBtn;//<2F>طŰ<D8B7>ť
private GameObject playback6;
private GameObject playback5;
//private Button recordbtn;
private GameObject Record;//¼<><C2BC><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD>
private GameObject right1;
private GameObject right2;
private bool isrecord = false;//<2F>Ƿ<EFBFBD><C7B7><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private GameObject mid;
public bool ishost;//<2F>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private bool lastHostState; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD> ishost ״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD>ڼ<EFBFBD><DABC><EFBFBD><EFBFBD>
// <20><><EFBFBD>ఴť<E0B0B4><C5A5>ҳ<EFBFBD><D2B3>
public Transform leftContainer; // <20><><EFBFBD>ఴť<E0B0B4>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
public Transform pagesContainer; // ҳ<><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD>
public GameObject buttonPrefab; // <20><>ťԤ<C5A5><D4A4><EFBFBD><EFBFBD>
public GameObject pagePrefab; // ҳ<><D2B3>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
public Sprite selectedSprite; // <20><>ťѡ<C5A5><D1A1>ʱ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
public Sprite defaultSprite; // <20><>ťĬ<C5A5><C4AC>״̬<D7B4>ı<EFBFBD><C4B1><EFBFBD>
private List<Button> navButtons = new List<Button>(); // <20><><EFBFBD>ఴť<E0B0B4>б<EFBFBD>
private List<GameObject> pages = new List<GameObject>(); // ҳ<><D2B3><EFBFBD>б<EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
public GameObject peoplePrefab; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
private Dictionary<int, List<GameObject>> pageItems = new Dictionary<int, List<GameObject>>(); // ÿ<><C3BF>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private Dictionary<int, List<GameObject>> pageHighlightBars = new Dictionary<int, List<GameObject>>(); // ÿ<><C3BF>ҳ<EFBFBD><D2B3><EFBFBD>ĸ߹<C4B8><DFB9><EFBFBD>
// <20>Ҳ<EFBFBD><D2B2><EFBFBD>ϸ<EFBFBD><CFB8>ϢUI
private Text roleNameText, jobTitleText, trainingStatusText, operationErrorsText, injuriesText;
private Text roleNameText2, jobTitleText2, trainingStatusText2, extinguishFireText, evacuationStatusText;
private List<RoleInfo> roleInfos = new List<RoleInfo>(); // <20><><EFBFBD><EFBFBD>
private int currentPageIndex = 0; // <20><>ǰҳ<C7B0><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private Dictionary<int, int> selectedItemIndex = new Dictionary<int, int>(); // <20><>¼ÿ<C2BC><C3BF>ҳ<EFBFBD><D2B3>ѡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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;
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
InitRoleData();
// <20><>ʼ<EFBFBD><CABC><EFBFBD>Ҳ<EFBFBD>UI<55><49><EFBFBD><EFBFBD>
InitializeRightUI();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ఴť<E0B0B4>Ͷ<EFBFBD>Ӧҳ<D3A6><D2B3>
for (int i = 0; i < 5; i++) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5<EFBFBD><35>ҳ<EFBFBD><D2B3>
{
CreatePageAndButton(i); // <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><C5A5>ҳ<EFBFBD><D2B3>
}
// Ĭ<><C4AC>ѡ<EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB>ҳ<EFBFBD><D2B3>
if (navButtons.Count > 0 && pages.Count > 0)
{
UpdatePage(0); // Ĭ<><C4AC><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>һ<EFBFBD><D2BB>ҳ<EFBFBD><D2B3>
}
UpdateVisibility();//<2F><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>uI
}
void Update()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4>
if (ishost != lastHostState)
{
UpdateVisibility(); // <20><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD> UI <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
lastHostState = ishost; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD> ishost ״̬
}
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
void InitRoleData()
{
for (int i = 0; i < 10; i++)
{
roleInfos.Add(new RoleInfo
{
roleName = $"<22><>ɫ<EFBFBD><C9AB> {i + 1}",
jobTitle = $"ְҵ {i + 1}",
trainingDate = "2023-12-19",
scenarioName = "<22><><EFBFBD>޾<EFBFBD>Ԯ<EFBFBD><D4AE>",
status = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>",
operationErrors = Random.Range(0, 10),
injuries = Random.Range(0, 5),
extinguishFire = (i % 2 == 0),
evacuationStatus = (i % 2 == 1)
});
}
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD>Ҳ<EFBFBD>UI
void InitializeRightUI()
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD><CBBD><EFBFBD>
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>();
// <20><><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD><CBBD><EFBFBD>
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>();
}
// <20><>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD>Ͷ<EFBFBD>Ӧ<EFBFBD><D3A6>ť
void CreatePageAndButton(int pageIndex)
{
// <20><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3>
GameObject newPage = Instantiate(pagePrefab, pagesContainer);
newPage.name = $"Page_{pageIndex + 1}";
pages.Add(newPage);
// Ϊÿ<CEAA><C3BF>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ScrollView <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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; // <20><>ֹ<EFBFBD>հ<EFBFBD><D5B0><EFBFBD><EFBFBD><EFBFBD>
button.onClick.AddListener(() => OnItemClicked(pageIndex, idx)); // <20>󶨵<EFBFBD><F3B6A8B5><EFBFBD><EFBFBD>¼<EFBFBD>
}
items.Add(item);
// <20><><EFBFBD>ø߹<C3B8><DFB9><EFBFBD>
GameObject highlightBar = item.transform.Find("Image").gameObject;
highlightBar.SetActive(false);
highlightBars.Add(highlightBar);
}
pageItems[pageIndex] = items;
pageHighlightBars[pageIndex] = highlightBars;
// Ĭ<>ϵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (highlightBars.Count > 0)
{
highlightBars[0].SetActive(true);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť
GameObject newButton = Instantiate(buttonPrefab, leftContainer);
Button btn = newButton.GetComponent<Button>();
if (btn != null)
{
navButtons.Add(btn);
int index = pageIndex; // <20><>ֹ<EFBFBD>հ<EFBFBD><D5B0><EFBFBD><EFBFBD><EFBFBD>
btn.onClick.AddListener(() => OnNavButtonClicked(index));
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ఴťʱ<C5A5>л<EFBFBD>ҳ<EFBFBD><D2B3>
void OnNavButtonClicked(int index)
{
UpdatePage(index);
}
// <20><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>ʾ
void UpdatePage(int index)
{
if (index < 0 || index >= pages.Count) return;
// <20>л<EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD>ʾ
for (int i = 0; i < pages.Count; i++)
{
pages[i].SetActive(i == index);
}
// <20><><EFBFBD>°<EFBFBD>ť״̬
for (int i = 0; i < navButtons.Count; i++)
{
Image img = navButtons[i].GetComponent<Image>();
img.sprite = (i == index) ? selectedSprite : defaultSprite;
}
currentPageIndex = index;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߹<EFBFBD>״̬
UpdateHighlightBars(index);
//// <20><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ϸ<EFBFBD><CFB8>ϢΪ<CFA2><CEAA>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//if (pageItems.ContainsKey(index) && pageHighlightBars[index].Count > 0)
//{
// pageHighlightBars[index][0].SetActive(true);
// UpdateRightPanel(0);
//}
}
// <20><><EFBFBD><EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD>ĸ߹<C4B8><DFB9><EFBFBD>״̬
void UpdateHighlightBars(int pageIndex)
{
if (!pageItems.ContainsKey(pageIndex) || !pageHighlightBars.ContainsKey(pageIndex)) return;
// <20><>ȡ<EFBFBD><C8A1>ǰҳ<C7B0><D2B3>ѡ<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int selectedIndex = selectedItemIndex.ContainsKey(pageIndex) ? selectedItemIndex[pageIndex] : 0;
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
if (selectedIndex < 0 || selectedIndex >= pageHighlightBars[pageIndex].Count)
{
selectedIndex = 0; // Ĭ<><C4AC>ѡ<EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и߹<D0B8><DFB9><EFBFBD>
foreach (var highlight in pageHighlightBars[pageIndex])
{
highlight.SetActive(false);
}
// <20><>ʾ<EFBFBD><CABE>ǰѡ<C7B0>еĸ߹<C4B8><DFB9><EFBFBD>
pageHighlightBars[pageIndex][selectedIndex].SetActive(true);
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ϸ<EFBFBD><CFB8>Ϣ
UpdateRightPanel(selectedIndex);
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>Ϣ
void OnItemClicked(int pageIndex, int itemIndex)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и߹<D0B8><DFB9><EFBFBD>
foreach (var highlight in pageHighlightBars[pageIndex])
{
highlight.SetActive(false);
}
// <20><>ʾ<EFBFBD><CABE>ǰѡ<C7B0><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ߹<C4B8><DFB9><EFBFBD>
pageHighlightBars[pageIndex][itemIndex].SetActive(true);
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ϸ<EFBFBD><CFB8>Ϣ
UpdateRightPanel(itemIndex);
}
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>ϸ<EFBFBD><CFB8>Ϣ
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 = $"<22><>[{info.trainingDate}]<5D><>[{info.scenarioName}]<5D><><EFBFBD><EFBFBD>\n״̬: {info.status}";
operationErrorsText.text = $"{info.operationErrors}";
injuriesText.text = $"{info.injuries}";
if (info.extinguishFire)
{
extinguishFireText.text = "<22>ɹ<EFBFBD>";
extinguishFireText.color = Color.green;
}
else
{
extinguishFireText.text = <><CAA7>";
extinguishFireText.color = Color.red;
}
if (info.evacuationStatus)
{
evacuationStatusText.text = "<22>ɹ<EFBFBD>";
evacuationStatusText.color = Color.green;
}
else
{
evacuationStatusText.text = <><CAA7>";
evacuationStatusText.color = Color.red;
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD> UI <20>ɼ<EFBFBD><C9BC><EFBFBD>
void UpdateVisibility()
{
//// <20><><EFBFBD><EFBFBD> ishost ״̬<D7B4><CCAC>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD><CBBB><EFBFBD>ͨ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>ͼ
//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()
{
// <20><><EFBFBD><EFBFBD> FigureState <20><>״̬<D7B4><EFBFBD>¼<EFBFBD>
figurestate.OnFigureStateChanged += UpdateButtonState;
}
void OnDisable()
{
// ȡ<><C8A1><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD>ֹ<EFBFBD>ڴ<EFBFBD>й©
figurestate.OnFigureStateChanged -= UpdateButtonState;
}
// <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>Ŀ<EFBFBD>갴ť
void UpdateButtonState(bool isActive, figurestate sender)
{
// <20><><EFBFBD>ݴ<EFBFBD><DDB4>ݵ<EFBFBD>״̬<D7B4><CCAC><EFBFBD>ð<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
backBtn.interactable = !isActive;
Debug.Log($"Target Button interactable = {isActive}. Triggered by {sender.gameObject.name}");
}
}