yyl
2026-02-12 5667c0a5e6b83964e2538a17dcfd6b719692ddda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// ============================================================================
// PlatformCacheHelper.cs — 平台文件系统缓存辅助
// 为各小游戏平台提供资源缓存路径和文件系统参数创建
// ============================================================================
 
using UnityEngine;
using YooAsset;
 
namespace ProjSG.Resource
{
    /// <summary>
    /// 平台资源缓存辅助工具。
    /// 为不同小游戏平台提供 YooAsset 文件系统参数创建和缓存路径获取。
    /// </summary>
    public static class PlatformCacheHelper
    {
        private const string CACHE_DIR_NAME = "__GAME_FILE_CACHE";
 
        /// <summary>
        /// 获取当前平台的资源缓存根路径。
        /// </summary>
        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
        }
 
        /// <summary>
        /// 为当前平台创建 WebPlayMode 的 WebServerFileSystemParameters。
        /// 自动选择微信/抖音/默认 Web 文件系统。
        /// </summary>
        /// <param name="remoteServices">远程服务配置</param>
        /// <returns>文件系统参数</returns>
        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
        }
 
        /// <summary>
        /// 获取缓存大小限制(MB)。
        /// 各平台有不同的存储限制。
        /// </summary>
        public static int GetCacheSizeLimitMB()
        {
            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 无严格限制
            }
        }
 
        /// <summary>
        /// 检查当前平台是否支持文件系统缓存。
        /// </summary>
        public static bool IsCacheSupported()
        {
#if UNITY_WEBGL
            return true; // 所有小游戏平台都支持缓存
#else
            return true; // 移动端/桌面端也支持
#endif
        }
    }
}