using System; using System.Runtime.InteropServices; using Cysharp.Threading.Tasks; using UnityEngine; /// /// 抖音小游戏平台实现 /// public class DouyinPlatform : IPlatformService { #if UNITY_WEBGL && !UNITY_EDITOR // JavaScript 插件方法声明 [DllImport("__Internal")] private static extern bool TT_Init(); [DllImport("__Internal")] private static extern void TT_Login(string callbackObjectName, string callbackMethodName); [DllImport("__Internal")] private static extern string TT_GetSystemInfo(); [DllImport("__Internal")] private static extern void TT_ShareAppMessage(string title, string imageUrl); [DllImport("__Internal")] private static extern void TT_CreateRewardedVideoAd(string adUnitId, string callbackObjectName, string callbackMethodName); [DllImport("__Internal")] private static extern void TT_SetStorageSync(string key, string value); [DllImport("__Internal")] private static extern string TT_GetStorageSync(string key); [DllImport("__Internal")] private static extern void TT_VibrateShort(); [DllImport("__Internal")] private static extern void TT_VibrateLong(); #endif private SystemInfo _cachedSystemInfo; private bool _isInitialized = false; public async UniTask InitAsync() { Debug.Log("[DouyinPlatform] 初始化抖音小游戏平台"); #if UNITY_WEBGL && !UNITY_EDITOR try { _isInitialized = TT_Init(); if (_isInitialized) { _cachedSystemInfo = GetSystemInfo(); Debug.Log("[DouyinPlatform] 初始化成功"); } return _isInitialized; } catch (Exception e) { Debug.LogError($"[DouyinPlatform] 初始化失败: {e.Message}"); return false; } #else await UniTask.Delay(100); _isInitialized = true; _cachedSystemInfo = CreateMockSystemInfo(); return true; #endif } public PlatformType GetPlatformType() { return PlatformType.Douyin; } public async UniTask LoginAsync() { Debug.Log("[DouyinPlatform] 开始登录"); #if UNITY_WEBGL && !UNITY_EDITOR // TODO: 实现抖音登录逻辑 await UniTask.Delay(500); return new LoginResult { Success = true, UserId = "douyin_user" }; #else await UniTask.Delay(500); return new LoginResult { Success = true, UserId = "douyin_test_user" }; #endif } public async UniTask ShareAsync(ShareData shareData) { Debug.Log($"[DouyinPlatform] 分享: {shareData.Title}"); #if UNITY_WEBGL && !UNITY_EDITOR TT_ShareAppMessage(shareData.Title, shareData.ImageUrl); #endif await UniTask.Delay(300); return true; } public async UniTask ShowAdAsync(AdType adType) { Debug.Log($"[DouyinPlatform] 显示广告: {adType}"); #if UNITY_WEBGL && !UNITY_EDITOR // TODO: 实现抖音广告逻辑 #endif await UniTask.Delay(1000); return new AdResult { Success = true, Completed = true }; } public async UniTask SaveDataAsync(string key, string value) { #if UNITY_WEBGL && !UNITY_EDITOR TT_SetStorageSync(key, value); #endif await UniTask.Yield(); return true; } public async UniTask LoadDataAsync(string key) { #if UNITY_WEBGL && !UNITY_EDITOR string value = TT_GetStorageSync(key); await UniTask.Yield(); return value; #else await UniTask.Yield(); return null; #endif } public async UniTask DownloadFileAsync(string url, string localPath, Action onProgress = null) { Debug.Log($"[DouyinPlatform] 下载文件: {url}"); await UniTask.Delay(500); return localPath; } public SystemInfo GetSystemInfo() { if (_cachedSystemInfo != null) return _cachedSystemInfo; #if UNITY_WEBGL && !UNITY_EDITOR try { string jsonInfo = TT_GetSystemInfo(); _cachedSystemInfo = JsonUtility.FromJson(jsonInfo); return _cachedSystemInfo; } catch (Exception e) { Debug.LogError($"[DouyinPlatform] 获取系统信息失败: {e.Message}"); return CreateMockSystemInfo(); } #else return CreateMockSystemInfo(); #endif } public void Vibrate(VibrationType vibrationType) { #if UNITY_WEBGL && !UNITY_EDITOR switch (vibrationType) { case VibrationType.Light: case VibrationType.Medium: TT_VibrateShort(); break; case VibrationType.Heavy: TT_VibrateLong(); break; } #endif } private SystemInfo CreateMockSystemInfo() { return new SystemInfo { DeviceModel = UnityEngine.SystemInfo.deviceModel, PlatformVersion = "Douyin 1.0.0", ScreenWidth = Screen.width, ScreenHeight = Screen.height, SafeArea = SafeAreaData.FromRect(Screen.safeArea), PixelRatio = Screen.dpi / 160f }; } }