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

156 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 int curIndex;
protected override void Awake()
{
base.Awake();
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);
var ToogleText = item.GetControl<Text>("Label");
Toggle toggle = item.GetControl<Toggle>("Toggle");
int currentIndex = int.Parse(tmxh.topiceNumber) - 1;
toggle.onValueChanged.RemoveAllListeners();
toggle.onValueChanged.AddListener((ison) =>
{
if (ison)
{
fun(currentIndex);
}
});
// 如果是第一个Toggle设置为选中状态
Debug.Log(toggle.name);
if (index == 0)
{
toggle.isOn = true;
}
index++; // 更新索引
}
// 确保 ToggleGroup 状态正确
ToggleGroup.allowSwitchOff = false;
ToggleGroup.SetAllTogglesOff();
//if (Rigcontent.childCount > 0)
//{
// Rigcontent.GetChild(0).GetComponent<Toggle>().isOn = true;
//}
//StartCoroutine(SetFirstToggleNextFrame());
}
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);
}
else if (index_ == Root.choose.Count - 1)
{
NextQuestion_Btn.gameObject.SetActive(false);
}
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;
};
}
public override void ShowMe()
{
base.ShowMe();
}
public override void HideMe()
{
base.HideMe();
}
protected override void OnClick(string btnName)
{
Debug.Log(btnName);
switch (btnName)
{
case "retrun_Btn":
Bootstrap.Instance.scenesManager.LoadSceneAsyn(this, "MenuScene", () =>
{
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>();
});
});
});
break;
case "PreviousQuestion_Btn":
break;
case "NextQuestion_Btn":
break;
}
}
}