CultivationOfBrewing/Assets/Scripts/UI/UIPanel/UI_ExamPanel.cs

206 lines
6.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UI_ExamPanel : BasePanel
{
public string jsonFileName = "题目"; // JSON文件名
Root Root = new Root();
public RectTransform Rigcontent;
public ToggleGroup ToggleGroup;
public UI_Topicitem UI_Topicitem;
public Button PreviousQuestion_Btn;
public Button NextQuestion_Btn;
public Toggle[] toggles;
public TextMeshProUGUI topicetype;
public TextMeshProUGUI topicText;//题目
public TextMeshProUGUI TopicType_Text;
public TextMeshProUGUI[] topicLabel;//题目选项
private List<Toggle> questionToggles = new List<Toggle>(); // 动态生成的题目Toggle列表
public CanvasGroup canvasGroup;
public int curIndex;
protected override void Awake()
{
base.Awake();
canvasGroup.alpha = 0;
int index = 0; // 用于跟踪当前是第几个Toggle
Root = JsonManager.LoadData<Root>(jsonFileName);
foreach (var tmxh in Root.choose)
{
var item = Instantiate(UI_Topicitem, Rigcontent);
item.Init(tmxh.topiceNumber);
Toggle toggle = item.GetControl<Toggle>("Toggle");
toggle.group = ToggleGroup;
questionToggles.Add(toggle);
int currentIndex = index;
toggle.onValueChanged.RemoveAllListeners();
toggle.onValueChanged.AddListener((ison) =>
{
if (ison)
{
Debug.Log($"Toggle选中:{currentIndex}");
fun(currentIndex);
}
});
if (index == 0) toggle.isOn = true;
index++;
ToggleGroup.allowSwitchOff = false;
UpdateQuestionUI();
}
//// 确保 ToggleGroup 状态正确
//ToggleGroup.allowSwitchOff = false;
//ToggleGroup.SetAllTogglesOff();
//UpdateQuestionUI();
}
public override async void ShowMe(int time = 0)
{
base.ShowMe();
await ToolManager.CanvasFadeIn(canvasGroup, 0.5f);
}
public override async void HideMe(int time = 0)
{
base.HideMe(1000);
await ToolManager.CanvasFadeOut(canvasGroup, 0.5f);
}
protected override void OnClick(string btnName)
{
Debug.Log(btnName);
switch (btnName)
{
case "retrun_Btn":
//StartCoroutine(ToolManager.CanvasFadeOut(canvasGroup, 1f));
Bootstrap.Instance.uiManager.ShowPanel<UI_SelectModePanel>(this, E_UI_Layer.Top, (panel) =>
{
Bootstrap.Instance.uiManager.ShowPanel<UI_BGPanel>(this, E_UI_Layer.Bot, (panel) =>
{
Debug.Log("UI_ExamPanel显示");
Bootstrap.Instance.uiManager.HidePanel<UI_ExamPanel>();
Bootstrap.Instance.uiManager.HidePanel<UI_MainTitlePanel>();
});
});
//Bootstrap.Instance.scenesManager.LoadSceneAsyn( "MenuScene", () =>
//{
//});
break;
case "PreviousQuestion_Btn":
SwitchQuestion(-1);
break;
case "NextQuestion_Btn":
SwitchQuestion(1);
break;
}
}
// 更新题目UI
private void UpdateQuestionUI()
{
if (curIndex < 0 || curIndex >= Root.choose.Count) return;
var currentQuestion = Root.choose[curIndex];
// 更新题目类型
bool isJudgment = currentQuestion.topicetype == "1";
topicetype.text = isJudgment ? "[判断题]" : "[选择题]";
TopicType_Text.text = isJudgment ? "判断题" : "选择题";
// 显示/隐藏选项
toggles[2].gameObject.SetActive(!isJudgment);
toggles[3].gameObject.SetActive(!isJudgment);
// 更新题目文本和选项
topicText.text = currentQuestion.topiceName;
topicLabel[0].text = currentQuestion.chooseA;
topicLabel[1].text = currentQuestion.chooseB;
topicLabel[2].text = currentQuestion.chooseC;
topicLabel[3].text = currentQuestion.ChooseD;
// 更新按钮状态
PreviousQuestion_Btn.gameObject.SetActive(curIndex > 0);
NextQuestion_Btn.gameObject.SetActive(curIndex < Root.choose.Count - 1);
// 选中当前题目的Toggle
if (questionToggles.Count > curIndex)
{
questionToggles[curIndex].isOn = true;
}
}
private void SwitchQuestion(int offset)
{
int newIndex = curIndex + offset;
if (newIndex < 0 || newIndex >= questionToggles.Count) return;
curIndex = newIndex;
questionToggles[curIndex].isOn = true;
UpdateQuestionUI();
}
private void fun(int index_)
{
Debug.Log(index_);
if (Root.choose[index_].topicetype == "1")
{
topicetype.text = "[判断题]";
TopicType_Text.text = "判断题";
toggles[2].gameObject.SetActive(false);
toggles[3].gameObject.SetActive(false);
}
else
{
topicetype.text = "[选择题]";
TopicType_Text.text = "选择题";
for (int i = 0; i < toggles.Length; i++)
{
toggles[i].gameObject.SetActive(true);
}
}
curIndex = index_;
if (index_ == 0)
{
PreviousQuestion_Btn.gameObject.SetActive(false);
NextQuestion_Btn.gameObject.SetActive(true);
}
else if (index_ == Root.choose.Count - 1)
{
NextQuestion_Btn.gameObject.SetActive(false);
PreviousQuestion_Btn.gameObject.SetActive(true);
}
else
{
PreviousQuestion_Btn.gameObject.SetActive(true);
NextQuestion_Btn.gameObject.SetActive(true);
}
for (int i = 0; i < Root.choose.Count; i++)
{
topicText.text = Root.choose[index_].topiceName;
topicLabel[0].text = Root.choose[index_].chooseA;
topicLabel[1].text = Root.choose[index_].chooseB;
topicLabel[2].text = Root.choose[index_].chooseC;
topicLabel[3].text = Root.choose[index_].ChooseD;
};
}
}