unityzgy/Assets/Scripts/UIScripts/Student/Mono/AnserWindowCtrl.cs
ayuan9957 69641904da fix: 翻译实时更新修复 + 路径检查 + IP选择 + 地形Layer修复
- IconBase: 修复(N)后缀车辆名翻译, 添加OnDestroy取消订阅防止MissingReference
- AnserWindowCtrl: 添加LanguageChanged订阅, 障碍提示实时翻译
- PlanningPlaneCtrl: 修复路径检查Count<0改为<1, 允许单段路径
- CarAIExtendCtrl: 空wayPoints数组防护
- GameObjectCreater: 重复key用索引赋值替代Add
- NetHelper: 优先返回10.x/192.168.x局域网IP, 跳过WSL虚拟网卡
- 海岛/沙漠/丘陵: Terrain Layer设为Floor(8), NavigationStatic
- 沙漠: TerrainLayer smoothness设为0
- 新增本地化条目: 携带装备/搭载人员/装备名称/空袭/维修厂等
2026-07-31 09:30:21 +08:00

158 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
//单选答题框
public class AnserWindowCtrl : MonoBehaviour
{
public Toggle toggle_Prefab;
public Text text_ask;
public Button button_sure;
public ToggleGroup toogleGroup;
public PromptTextCtrl promptText;
List<Toggle> curentToggles = new List<Toggle>();
private string askKey; // localization key for the question
private List<string> optionKeys; // localization keys for options
private string rightOptionKey; // key for right option
private bool showRightPrompt; // is the "wrong answer" prompt showing?
UnityAction<string> answerEvent;//答题完成事件 返回选项
UnityAction<bool, string> answerEventAndResult;//答题事件 返回答案和选项
Question questionData;
void OnEnable()
{
LocalizationManager.LanguageChanged += OnLanguageChanged;
}
void OnDisable()
{
LocalizationManager.LanguageChanged -= OnLanguageChanged;
}
private void OnLanguageChanged()
{
if (!string.IsNullOrEmpty(askKey))
text_ask.text = LocalizationManager.Get(askKey);
if (optionKeys != null)
{
for (int i = 0; i < optionKeys.Count && i < curentToggles.Count; i++)
{
if (curentToggles[i].gameObject.activeSelf)
curentToggles[i].GetComponentInChildren<Text>().text = LocalizationManager.Get(optionKeys[i]);
}
}
if (showRightPrompt && !string.IsNullOrEmpty(rightOptionKey))
{
promptText.SetPrompt(LocalizationManager.Format(LocalizationManager.Get("选择错误,应{0}"), LocalizationManager.Get(rightOptionKey)), Color.red, 2.5f);
}
}
// Start is called before the first frame update
void Start()
{
button_sure.onClick.AddListener(ButtonSure);
}
/// <summary>
/// 注入题库和答案 答题完成事件返回学员的选项
/// </summary>
/// <param name="ask"></param>
/// <param name="answer"></param>
public void SetAskAndAnswer(string ask,List<string> options,UnityAction<string> selectOver)
{
this.gameObject.SetActive(true);
button_sure.interactable = true;
askKey = ask;
optionKeys = options;
text_ask.text = LocalizationManager.Get(ask);
for (int i = 0; i < curentToggles.Count; i++)//关闭现有选项
{
curentToggles[i].gameObject.SetActive(false);
}
for (int i = 0; i < options.Count; i++)//更新选项
{
if (i > curentToggles.Count - 1)
curentToggles.Add(Instantiate<GameObject>(toggle_Prefab.gameObject, toogleGroup.transform).GetComponent<Toggle>());//创建新的UI
curentToggles[i].gameObject.SetActive(true);
curentToggles[i].isOn = false;
curentToggles[i].group = toogleGroup;
curentToggles[i].GetComponentInChildren<Text>().text = LocalizationManager.Get(options[i]);
}
answerEvent = selectOver;
}
/// <summary>
/// 注入问题 和答题结束事件
/// </summary>
/// <param name="question"> </param>
/// <param name="selectOver"> 答题结束时调用 并传回答题结果</param>
public void SetQuestion(Question question, UnityAction<bool,string> selectOver)
{
answerEventAndResult = selectOver;
questionData = question;
string ask = question.question;
List<string> options = new List<string>();
foreach (var item in question.options)
{
options.Add($"{item.Key}.{item.Value}");
}
SetAskAndAnswer(ask, options, null);
}
public void Hide()
{
this.gameObject.SetActive(false);
}
//确认
public void ButtonSure()
{
button_sure.interactable = false;
string selectOptionText = "";
for (int i = 0; i < curentToggles.Count; i++)
{
if (curentToggles[i].isOn)//选中的选项
{
selectOptionText = curentToggles[i].GetComponentInChildren<Text>().text;
break;
}
}
if (selectOptionText == "") //未选择选项时 无法提交
{
promptText.SetPrompt(LocalizationManager.Get("请选择选项后确认"), Color.red, 2f);
return;
}
if (answerEvent != null)
{
answerEvent.Invoke(selectOptionText);//返回选项
//answerEvent = null;
}
else if (answerEventAndResult != null)//
{
string xuanxiang = selectOptionText.Split('.')[0].Trim();//根据组装规则获取选项代号
answerEventAndResult.Invoke(questionData.rightOptionsKey == xuanxiang, selectOptionText);
//answerEventAndResult = null;
}
}
//选择错误时 显示答题结果
public void ShowRightResult(string msg)
{
rightOptionKey = msg;
showRightPrompt = true;
promptText.SetPrompt(LocalizationManager.Format(LocalizationManager.Get("选择错误,应{0}"), LocalizationManager.Get(msg)), Color.red, 2.5f);
}
}