Tz2/Assets/Scripts/QuestionManager.cs

244 lines
7.5 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 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<QuestionManager>();
DontDestroyOnLoad(go);
}
return instance;
}
}
private QuestionConfigData configData;
private Dictionary<string, Button> buttonCache = new Dictionary<string, Button>();
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<TextAsset>("QuestionConfigData");
if (jsonFile == null)
{
Debug.LogError("未找到题目配置文件请确保QuestionConfigData.json文件位于Resources文件夹中。");
return;
}
try
{
// 反序列化JSON数据
configData = JsonConvert.DeserializeObject<QuestionConfigData>(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<GlobalDataStorage>().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<Button>();
if (button == null)
{
Debug.LogError($"按钮对象 {pair.menuButtonPath} 上没有Button组件");
continue;
}
// 缓存按钮引用
buttonCache[pair.menuButtonPath] = button;
// 移除所有现有的点击事件
button.onClick.RemoveAllListeners();
// 添加新的点击事件
button.onClick.AddListener(() => OnButtonClick(pair));
}
}
// 按钮点击事件处理
// 按钮点击事件处理
private void OnButtonClick(ButtonPanelPair pair)
{
// 查找界面对象
GameObject panelObj = GameObject.Find(pair.panelObjectPath);
if (panelObj == null)
{
Debug.LogError($"未找到界面对象:{pair.panelObjectPath}");
return;
}
// 先禁用所有脚本
var allScripts = panelObj.GetComponents<MonoBehaviour>();
foreach (var script in allScripts)
{
script.enabled = false;
}
// 设置界面激活状态
panelObj.SetActive(pair.shouldActivate);
// 启用指定的脚本
if (!string.IsNullOrEmpty(pair.scriptTypeName))
{
string[] scriptNames = pair.scriptTypeName.Split(',');
foreach (var scriptName in scriptNames)
{
var script = allScripts.FirstOrDefault(s => s.GetType().Name == scriptName);
if (script != null)
{
script.enabled = true;
// 高亮打印激活的脚本名称
Debug.LogWarning($"🎯 <color=yellow>脚本已激活:</color> <color=green>{scriptName}</color> - 对象: {pair.panelObjectPath}");
}
else
{
Debug.LogWarning($"未找到脚本:{scriptName}");
}
}
}
// 打印所有激活的脚本状态总结
var activeScripts = allScripts.Where(s => s.enabled).ToList();
if (activeScripts.Count > 0)
{
Debug.LogWarning($"📋 <color=cyan>当前激活的脚本总数:</color> <color=yellow>{activeScripts.Count}</color>");
foreach (var activeScript in activeScripts)
{
Debug.LogWarning($" ✅ <color=green>{activeScript.GetType().Name}</color>");
}
}
}
/// <summary>
/// 获取所有大类配置
/// </summary>
public List<QuestionConfig> GetAllCategories()
{
return configData.configs.Where(c => c.configType == ConfigType.).ToList();
}
/// <summary>
/// 获取所有小类配置
/// </summary>
public List<QuestionConfig> GetAllQuestions()
{
return configData.configs.Where(c => c.configType == ConfigType.).ToList();
}
/// <summary>
/// 根据名称和类型查找配置
/// </summary>
public QuestionConfig FindConfigByNameAndType(string name, ConfigType type)
{
return configData.configs.FirstOrDefault(c =>
c.questionName == name && c.configType == type);
}
// 获取对象的Hierarchy路径
private string GetHierarchyPath(Transform trans)
{
string path = trans.name;
while (trans.parent != null)
{
trans = trans.parent;
path = trans.name + "/" + path;
}
return path;
}
// 重新加载配置并绑定事件(可以在运行时调用)
public void ReloadConfig()
{
LoadConfig();
}
}