// ============================================================================ // PlatformCacheHelper.cs — 平台文件系统缓存辅助 // 为各小游戏平台提供资源缓存路径和文件系统参数创建 // ============================================================================ using UnityEngine; using YooAsset; namespace ProjSG.Resource { /// /// 平台资源缓存辅助工具。 /// 为不同小游戏平台提供 YooAsset 文件系统参数创建和缓存路径获取。 /// public static class PlatformCacheHelper { private const string CACHE_DIR_NAME = "__GAME_FILE_CACHE"; /// /// 获取当前平台的资源缓存根路径。 /// public static string GetCacheRootPath() { #if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR return $"{WeChatWASM.WX.env.USER_DATA_PATH}/{CACHE_DIR_NAME}"; #elif UNITY_WEBGL && DOUYINMINIGAME && !UNITY_EDITOR return $"{TTSDK.TTFileSystem.USER_DATA_PATH}/{CACHE_DIR_NAME}"; #else // Standalone / Editor / 其他移动端:使用 persistentDataPath return $"{Application.persistentDataPath}/{CACHE_DIR_NAME}"; #endif } /// /// 为当前平台创建 WebPlayMode 的 WebServerFileSystemParameters。 /// 自动选择微信/抖音/默认 Web 文件系统。 /// /// 远程服务配置 /// 文件系统参数 public static FileSystemParameters CreateWebFileSystemParameters(IRemoteServices remoteServices) { #if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR string packageRoot = GetCacheRootPath(); return WechatFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices); #elif UNITY_WEBGL && DOUYINMINIGAME && !UNITY_EDITOR string packageRoot = GetCacheRootPath(); return TiktokFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices); #else // 默认 WebGL 文件系统 return FileSystemParameters.CreateDefaultWebServerFileSystemParameters(); #endif } /// /// 获取缓存大小限制(MB)。 /// 各平台有不同的存储限制。 /// public static int GetCacheSizeLimitMB() { return 1024; // var platform = PlatformFactory.GetCurrent(); // switch (platform.GetPlatformType()) // { // case PlatformType.WeChat: // return 200; // 微信小游戏用户数据上限约 200MB // case PlatformType.Douyin: // return 200; // 抖音小游戏类似限制 // case PlatformType.Vivo: // return 100; // Vivo 限制较小 // default: // return 1024; // Standalone 无严格限制 // } } /// /// 检查当前平台是否支持文件系统缓存。 /// public static bool IsCacheSupported() { #if UNITY_WEBGL return true; // 所有小游戏平台都支持缓存 #else return true; // 移动端/桌面端也支持 #endif } } }