Tz2/Assets/Scripts/UploadComManager.cs

68 lines
2.0 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;
using UnityEngine;
using UnityEngine.UI;
using Cysharp.Threading.Tasks;
using TMPro;
namespace DefaultNamespace
{
public class UploadComManager : MonoBehaviour
{
public Button uploadButton;
public GameObject win1;
public GameObject win2;
public TMP_Text text1;
private string originalText; // 保存原始文本内容
public GameObject uoloadBt;
public async void Start()
{
// 保存原始文本内容
if (text1 != null)
{
originalText = text1.text;
}
uploadButton.onClick.AddListener(async delegate
{
var (success, message) = await FileComponent.UploadExamFiles();
if (success)
{
// 成功隐藏win1显示win2
if (win1 != null) win1.SetActive(false);
if (win2 != null) win2.SetActive(true);
uoloadBt.gameObject.SetActive(false);
}
else
{
// 失败显示错误消息2秒后恢复原文本
await ShowErrorMessageAndRestore(message);
Debug.LogError($"上传失败:{message}");
}
});
}
/// <summary>
/// 显示错误消息并在2秒后恢复原始文本
/// </summary>
/// <param name="errorMessage">错误消息</param>
private async UniTask ShowErrorMessageAndRestore(string errorMessage)
{
if (text1 != null)
{
// 显示错误消息
text1.text=$"<color=red>{errorMessage}</color>";
// 等待2秒
await UniTask.Delay(3000);
// 恢复原始文本
text1.text = originalText;
}
}
}
}