using UnityEngine; /// /// 平台工厂类,用于创建对应平台的实现 /// public static class PlatformFactory { private static IPlatformService _currentPlatform; private static PlatformType? _forcedPlatformType; /// /// 获取当前平台实例 /// public static IPlatformService GetCurrent() { if (_currentPlatform == null) { _currentPlatform = CreatePlatform(); } return _currentPlatform; } /// /// 强制指定平台类型(用于测试) /// public static void ForcePlatform(PlatformType platformType) { _forcedPlatformType = platformType; _currentPlatform = null; // 重置当前平台 } /// /// 创建平台实例 /// 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(); } } /// /// 检测当前平台类型 /// 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 } }