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