190 lines
5.1 KiB
C#
Raw Normal View History

2025-05-14 18:28:46 +08:00
using System;
2024-12-14 18:27:59 +08:00
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Collections.Generic;
2024-12-30 19:15:34 +08:00
using static OperationController;
2025-05-14 18:28:46 +08:00
using TMPro;
2025-05-12 17:26:46 +08:00
using System.IO;
2024-12-14 18:27:59 +08:00
namespace QFramework.Example
{
public class UITipWindowData : UIPanelData
{
public class ItemData
{
public string txt;
public Action OnClick;
}
2025-05-12 17:26:46 +08:00
public string ExportVideoPath;
2024-12-14 18:27:59 +08:00
public string txt;
2025-01-08 16:02:57 +08:00
public string audio;
2024-12-14 18:27:59 +08:00
public List<ItemData> btns = new List<ItemData>();
}
public partial class UITipWindow : UIPanel
{
2025-01-08 16:02:57 +08:00
ResLoader loader;
2024-12-14 18:27:59 +08:00
protected override void OnInit(IUIData uiData = null)
{
mData = uiData as UITipWindowData ?? new UITipWindowData();
// please add init code here
2025-01-09 09:43:15 +08:00
TypeEventSystem.Global.Register<OnModuleQuit>((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject);
2024-12-30 19:15:34 +08:00
}
private void OnStepChanged(StepStatusOnChange change)
{
Hide();
2024-12-14 18:27:59 +08:00
}
protected override void OnOpen(IUIData uiData = null)
{
mData = uiData as UITipWindowData ?? new UITipWindowData();
2025-01-09 13:35:04 +08:00
TypeEventSystem.Global.Register<StepStatusOnChange>(OnStepChanged).UnRegisterWhenDisabled(gameObject);
2025-03-25 10:46:22 +08:00
if (mData.txt.Contains("{Score}"))
{
mData.txt = mData.txt.Replace("{Score}", ScoreController.Instance.GetCurScore().ToString());
}
2024-12-14 18:27:59 +08:00
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;
2025-01-08 16:02:57 +08:00
obj.name = item.txt;
2024-12-14 18:27:59 +08:00
obj.GetComponent<Button>().onClick.AddListener(() =>
{
item.OnClick?.Invoke();
Hide();
});
}
}
2025-01-08 16:02:57 +08:00
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();
}
2025-05-14 18:28:46 +08:00
transform.SetAsLastSibling();
ExportVideo();
2024-12-14 18:27:59 +08:00
}
2025-05-14 18:28:46 +08:00
2024-12-14 18:27:59 +08:00
protected override void OnShow()
{
}
protected override void OnHide()
{
2025-01-08 16:02:57 +08:00
AudioKit.StopVoice();
2024-12-14 18:27:59 +08:00
}
protected override void OnClose()
{
2025-01-08 16:02:57 +08:00
loader.Recycle2Cache();
2025-05-12 17:26:46 +08:00
2025-05-14 18:28:46 +08:00
}
private void ExportVideo()
{
2025-07-24 08:36:22 +08:00
Debug.Log("exportVideoPath:" + mData.ExportVideoPath);
2025-05-14 18:28:46 +08:00
if (!string.IsNullOrEmpty(mData.ExportVideoPath))
2025-05-12 17:26:46 +08:00
{
string OutPath = "";
string sourcePath = Global.videoPath + mData.ExportVideoPath;
2025-07-24 08:36:22 +08:00
Debug.Log("视频路径地址:"+ sourcePath);
2025-05-12 17:26:46 +08:00
2025-07-24 08:36:22 +08:00
#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);
2025-05-12 17:26:46 +08:00
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
2025-07-24 08:36:22 +08:00
string filePath = ChinarFileController.SaveProject(OutPath);
2025-05-12 17:26:46 +08:00
try
{
2025-07-24 08:36:22 +08:00
// 确保目标目录存在
2025-05-12 17:26:46 +08:00
string directory = Path.GetDirectoryName(sourcePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
2025-07-24 08:36:22 +08:00
// 尝试复制文件(覆盖已存在文件)
2025-05-12 17:26:46 +08:00
File.Copy(sourcePath, filePath, true);
2025-07-24 08:36:22 +08:00
Debug.Log("文件复制成功!");
2025-05-12 17:26:46 +08:00
}
catch (IOException e)
{
2025-07-24 08:36:22 +08:00
// 处理共享冲突或其他IO异常
Debug.LogError($"文件复制失败: {e.Message}");
2025-05-12 17:26:46 +08:00
2025-07-24 08:36:22 +08:00
// 可选:提供更具体的错误信息
2025-05-12 17:26:46 +08:00
if (e.Message.Contains("Sharing violation"))
{
2025-07-24 08:36:22 +08:00
Debug.LogError("错误:文件正在被其他程序使用!请关闭相关应用后重试。");
2025-05-12 17:26:46 +08:00
}
}
catch (System.Exception e)
{
2025-07-24 08:36:22 +08:00
// 处理其他异常
2025-05-12 17:26:46 +08:00
2025-07-24 08:36:22 +08:00
Debug.LogError($"发生未知错误: {e.Message}");
2025-05-12 17:26:46 +08:00
}
2025-05-14 18:28:46 +08:00
#endif
2025-05-12 17:26:46 +08:00
}
else
{
2025-05-14 18:28:46 +08:00
Debug.Log(mData.ExportVideoPath+"????");
2025-07-24 08:36:22 +08:00
Debug.Log("结束????");
2025-05-12 17:26:46 +08:00
}
2025-05-14 18:28:46 +08:00
}
2025-05-12 17:26:46 +08:00
2024-12-14 18:27:59 +08:00
}
2025-05-14 18:28:46 +08:00
2024-12-14 18:27:59 +08:00
}
2025-05-14 18:28:46 +08:00