yyl
2026-02-11 1ad03cc2f91d75e80fc3dc42e2ac1fadc9a2bfec
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
using UnityEngine;
 
/// <summary>
/// 平台工厂类,用于创建对应平台的实现
/// </summary>
public static class PlatformFactory
{
        private static IPlatformService _currentPlatform;
        private static PlatformType? _forcedPlatformType;
        
        /// <summary>
        /// 获取当前平台实例
        /// </summary>
        public static IPlatformService GetCurrent()
        {
            if (_currentPlatform == null)
            {
                _currentPlatform = CreatePlatform();
            }
            return _currentPlatform;
        }
        
        /// <summary>
        /// 强制指定平台类型(用于测试)
        /// </summary>
        public static void ForcePlatform(PlatformType platformType)
        {
            _forcedPlatformType = platformType;
            _currentPlatform = null; // 重置当前平台
        }
        
        /// <summary>
        /// 创建平台实例
        /// </summary>
        private static IPlatformService CreatePlatform()
        {
            PlatformType platformType = DetectPlatformType();
            
            switch (platformType)
            {
                case PlatformType.WeChat:
                    Debug.Log("[PlatformFactory] 创建微信小游戏平台实例");
                    return new WeChatPlatform();
                    
                case PlatformType.Douyin:
                    Debug.Log("[PlatformFactory] 创建抖音小游戏平台实例");
                    return new DouyinPlatform();
                    
                case PlatformType.Vivo:
                    Debug.Log("[PlatformFactory] 创建vivo小游戏平台实例");
                    return new VivoPlatform();
                    
                case PlatformType.Standalone:
                default:
                    Debug.Log("[PlatformFactory] 创建编辑器测试平台实例");
                    return new StandalonePlatform();
            }
        }
        
        /// <summary>
        /// 检测当前平台类型
        /// </summary>
        private static PlatformType DetectPlatformType()
        {
            // 如果强制指定了平台类型,使用强制类型
            if (_forcedPlatformType.HasValue)
            {
                Debug.Log($"[PlatformFactory] 使用强制指定的平台类型: {_forcedPlatformType.Value}");
                return _forcedPlatformType.Value;
            }
            
            #if UNITY_EDITOR
            // Unity 编辑器环境
            return PlatformType.Standalone;
            #elif UNITY_WEBGL
            // WebGL 平台,需要通过 JavaScript 检测具体平台
            // TODO: 通过 Application.ExternalEval 检测微信、抖音等平台
            // 暂时默认返回微信
            return PlatformType.WeChat;
            #else
            // 其他平台默认为 Standalone
            return PlatformType.Standalone;
            #endif
        }
    }