yyl
2026-03-26 1ab047b5fdd933c38ba0519ec2e83a44512ea8d7
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
87
88
89
90
// ============================================================================
// RemoteServicesImpl.cs — IRemoteServices 实现
// 配合平台抽象层提供 CDN 地址,支持多平台 CDN 路由
// ============================================================================
 
using YooAsset;
 
namespace ProjSG.Resource
{
    /// <summary>
    /// YooAsset 远程服务实现。
    /// 提供主 CDN 和备用 CDN 的资源下载地址拼接。
    /// </summary>
    public class RemoteServicesImpl : IRemoteServices
    {
        private readonly string _mainUrl;
        private readonly string _fallbackUrl;
 
        /// <summary>
        /// 创建远程服务配置。
        /// </summary>
        /// <param name="mainUrl">主 CDN 根地址(如 https://cdn.example.com/bundles/)</param>
        /// <param name="fallbackUrl">备用 CDN 根地址</param>
        public RemoteServicesImpl(string mainUrl, string fallbackUrl)
        {
            _mainUrl = mainUrl;
            _fallbackUrl = fallbackUrl;
        }
 
        /// <summary>
        /// 获取主 CDN 下载地址。
        /// </summary>
        public string GetRemoteMainURL(string fileName)
        {
            return $"{_mainUrl}/{fileName}";
        }
 
        /// <summary>
        /// 获取备用 CDN 下载地址。
        /// </summary>
        public string GetRemoteFallbackURL(string fileName)
        {
            return $"{_fallbackUrl}/{fileName}";
        }
 
        /// <summary>
        /// 根据当前平台创建 RemoteServicesImpl。
        /// 通过 IPlatformService + PlatformFactory 自动路由到对应平台的 CDN。
        /// </summary>
        /// <param name="baseCdnUrl">基础 CDN 地址(如 https://cdn.example.com)</param>
        /// <param name="fallbackCdnUrl">备用 CDN 地址(可选,默认同 baseCdnUrl)</param>
        /// <returns>平台对应的 RemoteServicesImpl 实例</returns>
        // public static RemoteServicesImpl CreateForCurrentPlatform(string baseCdnUrl, string fallbackCdnUrl = null)
        // {
        //     fallbackCdnUrl = fallbackCdnUrl ?? baseCdnUrl;
 
        //     var platform = PlatformFactory.GetCurrent();
        //     var platformType = platform.GetPlatformType();
 
        //     // 按平台类型路由子路径
        //     string platformPath = GetPlatformCdnPath(platformType);
 
        //     string mainUrl = $"{baseCdnUrl}/{platformPath}";
        //     string fallbackUrl = $"{fallbackCdnUrl}/{platformPath}";
 
        //     UnityEngine.Debug.Log($"[RemoteServicesImpl] Platform={platformType}, CDN={mainUrl}");
        //     return new RemoteServicesImpl(mainUrl, fallbackUrl);
        // }
 
        /// <summary>
        /// 获取平台对应的 CDN 子路径。
        /// </summary>
        private static string GetPlatformCdnPath(PlatformType platformType)
        {
            switch (platformType)
            {
                case PlatformType.WeChat:
                    return "wechat/bundles";
                case PlatformType.Douyin:
                    return "douyin/bundles";
                case PlatformType.Vivo:
                    return "vivo/bundles";
                // OPPO / Kuaishou 等可在此扩展
                case PlatformType.Standalone:
                default:
                    return "standalone/bundles";
            }
        }
    }
}