194 lines
7.0 KiB
C#
194 lines
7.0 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using Cysharp.Threading.Tasks;
|
||
using MotionFramework;
|
||
using TMPro;
|
||
using static InterfaceManager;
|
||
namespace DefaultNamespace
|
||
{
|
||
/// <summary>
|
||
/// 考试文件下载UI管理器
|
||
/// 展示如何使用单个文件下载功能
|
||
/// </summary>
|
||
public class ExamFileDownloadUI : MonoBehaviour
|
||
{
|
||
[Header("UI组件")]
|
||
[SerializeField] private Transform downloadButtonsParent; // 下载按钮的父容器
|
||
[SerializeField] private Button uploadButton; // 上传按钮
|
||
|
||
[Header("配置")]
|
||
[SerializeField] private string examTypeName = "新注册仓库的主数据运维流程"; // 题目名称
|
||
[SerializeField] private bool useGlobalExamName = true; // 是否使用全局题目名字
|
||
[SerializeField] private string downloadButtonPrefabPath = "Prefabs/下载按钮"; // 下载按钮预制体路径
|
||
|
||
private List<Button> downloadButtons = new List<Button>();
|
||
private List<TMP_Text> buttonTexts = new List<TMP_Text>();
|
||
private GameObject downloadButtonPrefab; // 下载按钮预制体
|
||
int downloadButtonIndex = 0;
|
||
|
||
// 存储文件名和对应的文件路径
|
||
private Dictionary<string, string> downloadedFilePaths = new Dictionary<string, string>();
|
||
private void Start()
|
||
{
|
||
InitializeUI();
|
||
// 订阅文件下载完成事件
|
||
FileComponent.OnFileDownloadCompleted += OnFileDownloadCompleted;
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
// 取消订阅文件下载完成事件
|
||
FileComponent.OnFileDownloadCompleted -= OnFileDownloadCompleted;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化UI
|
||
/// </summary>
|
||
private async void InitializeUI()
|
||
{
|
||
Debug.Log("开始初始化考试文件下载UI");
|
||
|
||
// 加载下载按钮预制体
|
||
LoadDownloadButtonPrefab();
|
||
|
||
List<(string name, string url)> fileInfos;
|
||
|
||
if (useGlobalExamName)
|
||
{
|
||
// 使用全局题目名字
|
||
fileInfos = await FileComponent.GetExamFileInfoWithUrls();
|
||
string examName = MotionEngine.GetModule<GlobalDataStorage>().ExamName;
|
||
Debug.Log($"使用全局题目名字:{examName}");
|
||
}
|
||
else
|
||
{
|
||
// 使用指定的题目名称
|
||
var examFileInfos = await FileComponent.GetExamFileInfos(examTypeName);
|
||
fileInfos = examFileInfos.Select(f => (f.name, f.url)).ToList();
|
||
Debug.Log($"使用指定题目名称:{examTypeName}");
|
||
}
|
||
|
||
if (fileInfos.Count == 0)
|
||
{
|
||
string examInfo = useGlobalExamName ?
|
||
MotionEngine.GetModule<GlobalDataStorage>().ExamName :
|
||
examTypeName;
|
||
Debug.LogWarning($"题目 '{examInfo}' 没有配置任何文件");
|
||
return;
|
||
}
|
||
|
||
string examDisplay = useGlobalExamName ?
|
||
MotionEngine.GetModule<GlobalDataStorage>().ExamName :
|
||
examTypeName;
|
||
Debug.Log($"为题目 '{examDisplay}' 准备 {fileInfos.Count} 个文件,等待下载完成后显示按钮");
|
||
|
||
// 不再在初始化时创建按钮,而是等待文件下载完成后创建
|
||
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载下载按钮预制体
|
||
/// </summary>
|
||
private void LoadDownloadButtonPrefab()
|
||
{
|
||
Debug.Log($"正在加载下载按钮预制体,路径:{downloadButtonPrefabPath}");
|
||
downloadButtonPrefab = Resources.Load<GameObject>(downloadButtonPrefabPath);
|
||
|
||
if (downloadButtonPrefab == null)
|
||
{
|
||
Debug.LogError($"无法加载下载按钮预制体,路径:{downloadButtonPrefabPath},请确保预制体存在于Resources文件夹中");
|
||
return;
|
||
}
|
||
|
||
Debug.Log("下载按钮预制体加载成功");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 文件下载完成事件处理
|
||
/// </summary>
|
||
/// <param name="fileName">文件名称</param>
|
||
/// <param name="filePath">文件路径</param>
|
||
private void OnFileDownloadCompleted(string fileName, string filePath)
|
||
{
|
||
Debug.Log($"文件下载完成事件触发:{fileName} -> {filePath}");
|
||
|
||
// 存储文件路径
|
||
downloadedFilePaths[fileName] = filePath;
|
||
|
||
// 创建打开文件按钮
|
||
CreateOpenFileButton(fileName, filePath);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建打开文件按钮
|
||
/// </summary>
|
||
/// <param name="fileName">文件名称</param>
|
||
/// <param name="filePath">文件路径</param>
|
||
private void CreateOpenFileButton(string fileName, string filePath)
|
||
{
|
||
Debug.Log($"创建打开文件按钮,文件:{fileName}");
|
||
|
||
if (downloadButtonPrefab == null)
|
||
{
|
||
Debug.LogError("下载按钮预制体未加载,无法创建按钮");
|
||
return;
|
||
}
|
||
|
||
if (downloadButtonsParent == null)
|
||
{
|
||
Debug.LogError("下载按钮父容器未设置,无法创建按钮");
|
||
return;
|
||
}
|
||
|
||
// 实例化按钮预制体
|
||
GameObject buttonGO = Instantiate(downloadButtonPrefab, downloadButtonsParent);
|
||
Button button = buttonGO.GetComponent<Button>();
|
||
TMP_Text buttonText = buttonGO.GetComponentInChildren<TMP_Text>();
|
||
|
||
if (button == null)
|
||
{
|
||
Debug.LogError("预制体中未找到Button组件");
|
||
Destroy(buttonGO);
|
||
return;
|
||
}
|
||
|
||
if (buttonText == null)
|
||
{
|
||
Debug.LogError("预制体中未找到Text组件");
|
||
Destroy(buttonGO);
|
||
return;
|
||
}
|
||
|
||
// 设置按钮文本为"打开文件"
|
||
buttonText.text = "打开" + fileName;
|
||
Debug.Log($"设置按钮文本为:打开{fileName}");
|
||
|
||
// 添加点击事件 - 只打开文件,不下载
|
||
string capturedFileName = fileName; // 捕获文件名
|
||
button.onClick.AddListener(() => OnOpenFileButtonClicked(capturedFileName));
|
||
|
||
// 保存按钮引用
|
||
downloadButtons.Add(button);
|
||
buttonTexts.Add(buttonText);
|
||
|
||
Debug.Log($"打开文件按钮创建完成,文件:{fileName}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开文件按钮点击事件
|
||
/// </summary>
|
||
/// <param name="fileName">文件名称</param>
|
||
private void OnOpenFileButtonClicked(string fileName)
|
||
{
|
||
Debug.Log($"点击打开文件按钮,文件:{fileName}");
|
||
|
||
// 使用FileComponent的打开文件功能
|
||
FileComponent.OpenDownloadedFile(fileName);
|
||
}
|
||
|
||
|
||
}
|
||
} |