using UnityEngine; using System.Linq; using System; using System.Collections; using System.Collections.Generic; using DefaultNamespace; using MotionFramework; using UnityEngine.UI; using Newtonsoft.Json; public class QuestionManager : MonoBehaviour { private static QuestionManager instance; public static QuestionManager Instance { get { if (instance == null) { GameObject go = new GameObject("QuestionManager"); instance = go.AddComponent(); DontDestroyOnLoad(go); } return instance; } } private QuestionConfigData configData; private Dictionary buttonCache = new Dictionary(); public CanvasGroup questionGroup; private void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); LoadConfig(); } else { Destroy(gameObject); } } private void LoadConfig() { // 从Resources文件夹加载JSON文件 TextAsset jsonFile = Resources.Load("QuestionConfigData"); if (jsonFile == null) { Debug.LogError("未找到题目配置文件!请确保QuestionConfigData.json文件位于Resources文件夹中。"); return; } try { // 反序列化JSON数据 configData = JsonConvert.DeserializeObject(jsonFile.text); if (configData == null) { Debug.LogError("配置文件格式错误!"); return; } // 初始化时绑定所有按钮事件 BindAllButtonEvents(); questionGroup.alpha = 1; questionGroup.gameObject.SetActive(false); } catch (Exception e) { Debug.LogError($"加载配置文件时发生错误:{e.Message}"); } } // 绑定所有按钮事件 private void BindAllButtonEvents() { string examName = MotionEngine.GetModule().ExamName; // string examName = "库存物资库存状态转换"; Debug.Log($"当前题目名称:{examName}"); // 查找对应的题目配置 QuestionConfig currentConfig = null; // 首先尝试查找小类(完整名字匹配) currentConfig = configData.configs.FirstOrDefault(c => c.questionName == examName && c.configType == ConfigType.小类); // 如果没找到小类,则查找大类(题目名包含大类名字) if (currentConfig == null) { currentConfig = configData.configs.FirstOrDefault(c => c.configType == ConfigType.大类 && examName.Contains(c.questionName)); if (currentConfig != null) { Debug.Log($"找到匹配的大类:{currentConfig.questionName},题目名:{examName} 包含大类名"); } } else { Debug.Log($"找到匹配的小类:{currentConfig.questionName}"); } if (currentConfig == null) { Debug.LogError($"未找到题目 {examName} 的配置,既不是小类也不匹配任何大类"); return; } foreach (var pair in currentConfig.pairs) { // 查找按钮对象 GameObject buttonObj = GameObject.Find(pair.menuButtonPath); if (buttonObj == null) { Debug.LogError($"未找到按钮对象:{pair.menuButtonPath}"); continue; } else { Debug.Log("找到按钮对象:" + pair.menuButtonPath); } // 获取按钮组件 Button button = buttonObj.GetComponent