Files
tysdk-intergration/sdk-intergration/LocalPackages/LeviathanVideo/Editor/WeChatMiniGameMenu.cs
2026-03-24 11:43:22 +08:00

125 lines
5.1 KiB
C#

#if UNITY_WEBGL
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using System.IO;
namespace LeviathanVideo.Editor
{
public static class WeChatMiniGameMenu
{
private const string SourcePath = "Assets/WX-WASM-SDK-V2-v0.1.32~";
private const string TargetPath = "Assets/WX-WASM-SDK-V2";
private const string LeviathanWasmPath = "Assets/WX-WASM-SDK-V2/Editor/template/minigame/leviathan.wasm.br";
private const string WeChatSDKGitUrl = "https://github.com/wechat-miniprogram/minigame-tuanjie-transform-sdk.git";
private static AddRequest _addRequest;
// 验证函数:只在 WX-WASM-SDK-V2 文件夹不存在时启用
[MenuItem("微信小游戏/[LeviathanVideo] SDK", true)]
public static bool ValidateInstallWeChatSDK()
{
return !Directory.Exists(TargetPath);
}
[MenuItem("微信小游戏/[LeviathanVideo] SDK", false)]
public static void InstallWeChatSDK()
{
Debug.Log($"[LeviathanVideo] 开始安装微信SDK: {WeChatSDKGitUrl}");
_addRequest = Client.Add(WeChatSDKGitUrl);
EditorApplication.update += OnInstallProgress;
}
private static void OnInstallProgress()
{
if (_addRequest == null || !_addRequest.IsCompleted)
return;
EditorApplication.update -= OnInstallProgress;
if (_addRequest.Status == StatusCode.Success)
{
Debug.Log($"[LeviathanVideo] 微信SDK安装成功: {_addRequest.Result.packageId}");
EditorUtility.DisplayDialog("安装成功",
$"微信小游戏SDK安装成功!\n\n包ID: {_addRequest.Result.packageId}\n\n请执行下一步操作:\n微信小游戏 -> [LeviathanVideo] ****首次安装微信SDK后需要执行此操作****",
"确定");
}
else if (_addRequest.Status >= StatusCode.Failure)
{
Debug.LogError($"[LeviathanVideo] 微信SDK安装失败: {_addRequest.Error.message}");
EditorUtility.DisplayDialog("安装失败", $"微信小游戏SDK安装失败!\n\n错误: {_addRequest.Error.message}", "确定");
}
_addRequest = null;
}
// 验证函数:只在 leviathan.wasm.br 文件不存在时启用
[MenuItem("微信小游戏/[LeviathanVideo] ****SDK后需要执行此操作**** ", true)]
public static bool ValidateCopyFiles()
{
// 需要 SDK 已安装 且 leviathan.wasm.br 文件不存在
return Directory.Exists(TargetPath) && !File.Exists(LeviathanWasmPath);
}
[MenuItem("微信小游戏/[LeviathanVideo] ****SDK后需要执行此操作**** ", false)]
public static void CopyFiles()
{
string sourcePath = Path.GetFullPath(SourcePath);
string targetPath = Path.GetFullPath(TargetPath);
if (!Directory.Exists(sourcePath))
{
EditorUtility.DisplayDialog("错误", $"源路径不存在: {sourcePath}", "确定");
return;
}
if (!Directory.Exists(targetPath))
{
EditorUtility.DisplayDialog("错误", $"目标路径不存在: {targetPath}", "确定");
return;
}
try
{
CopyDirectory(sourcePath, targetPath);
AssetDatabase.Refresh();
EditorUtility.DisplayDialog("完成", $"已将文件从\n{SourcePath}\n复制到\n{TargetPath}", "确定");
Debug.Log($"[Leviathan] 文件复制完成: {SourcePath} -> {TargetPath}");
}
catch (System.Exception e)
{
EditorUtility.DisplayDialog("错误", $"复制文件时出错: {e.Message}", "确定");
Debug.LogError($"[Leviathan] 复制文件失败: {e}");
}
}
private static void CopyDirectory(string sourceDir, string targetDir)
{
// 复制所有文件
foreach (string file in Directory.GetFiles(sourceDir))
{
string fileName = Path.GetFileName(file);
string destFile = Path.Combine(targetDir, fileName);
File.Copy(file, destFile, true);
Debug.Log($"[Leviathan] 复制文件: {fileName}");
}
// 递归复制所有子目录
foreach (string dir in Directory.GetDirectories(sourceDir))
{
string dirName = Path.GetFileName(dir);
string destDir = Path.Combine(targetDir, dirName);
if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}
CopyDirectory(dir, destDir);
}
}
}
}
#endif