2025-07-24 08:36:22 +08:00

190 lines
5.1 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
using static OperationController;
using TMPro;
using System.IO;
namespace QFramework.Example
{
public class UITipWindowData : UIPanelData
{
public class ItemData
{
public string txt;
public Action OnClick;
}
public string ExportVideoPath;
public string txt;
public string audio;
public List<ItemData> btns = new List<ItemData>();
}
public partial class UITipWindow : UIPanel
{
ResLoader loader;
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITipWindowData ?? new UITipWindowData();
// please add init code here
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
}
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITipWindowData ?? new UITipWindowData();
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
if (mData.txt.Contains("{Score}"))
{
mData.txt = mData.txt.Replace("{Score}", ScoreController.Instance.GetCurScore().ToString());
}
Label.text = mData.txt;
BtnContent.RemoveAllChildren();
if (mData != null)
{
foreach (var item in mData.btns)
{
GameObject obj = GameObject.Instantiate(BtnPrefab.gameObject, BtnContent);
obj.transform.Find("Label").GetComponent<TextMeshProUGUI>().text = item.txt;
obj.name = item.txt;
obj.GetComponent<Button>().onClick.AddListener(() =>
{
item.OnClick?.Invoke();
Hide();
});
}
}
if (string.IsNullOrEmpty(mData.audio) == false)
{
string path = Global.audioPath + mData.audio;
loader = ResLoader.Allocate();
loader.Add2Load(path.ToLocalAudioResName(), (success, res) =>
{
if (success)
{
AudioKit.PlayVoice(res.Asset.As<AudioClip>(), false);
}
});
loader.LoadAsync();
}
transform.SetAsLastSibling();
ExportVideo();
}
protected override void OnShow()
{
}
protected override void OnHide()
{
AudioKit.StopVoice();
}
protected override void OnClose()
{
loader.Recycle2Cache();
}
private void ExportVideo()
{
Debug.Log("exportVideoPath:" + mData.ExportVideoPath);
if (!string.IsNullOrEmpty(mData.ExportVideoPath))
{
string OutPath = "";
string sourcePath = Global.videoPath + mData.ExportVideoPath;
Debug.Log("视频路径地址:"+ sourcePath);
#if UNITY_WEBGL && !UNITY_EDITOR
string[] fileNames = mData.ExportVideoPath.Split('/');
string fileName = fileNames[0];
if (fileNames.Length > 1)
{
fileName = fileNames[1];
}
WebGLDownLoadFile.Instance.DownloadVideo(sourcePath, fileName);
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
string filePath = ChinarFileController.SaveProject(OutPath);
try
{
// 确保目标目录存在
string directory = Path.GetDirectoryName(sourcePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
// 尝试复制文件(覆盖已存在文件)
File.Copy(sourcePath, filePath, true);
Debug.Log("文件复制成功!");
}
catch (IOException e)
{
// 处理共享冲突或其他IO异常
Debug.LogError($"文件复制失败: {e.Message}");
// 可选:提供更具体的错误信息
if (e.Message.Contains("Sharing violation"))
{
Debug.LogError("错误:文件正在被其他程序使用!请关闭相关应用后重试。");
}
}
catch (System.Exception e)
{
// 处理其他异常
Debug.LogError($"发生未知错误: {e.Message}");
}
#endif
}
else
{
Debug.Log(mData.ExportVideoPath+"????");
Debug.Log("结束????");
}
}
}
}