| | |
| | | using System; |
| | | using System.Runtime.InteropServices; |
| | | using Cysharp.Threading.Tasks; |
| | | using UnityEngine; |
| | | // using System; |
| | | // using System.Runtime.InteropServices; |
| | | // using Cysharp.Threading.Tasks; |
| | | // using UnityEngine; |
| | | |
| | | /// <summary> |
| | | /// 抖音小游戏平台实现 |
| | | /// </summary> |
| | | public class DouyinPlatform : IPlatformService |
| | | { |
| | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | | // JavaScript 插件方法声明 |
| | | [DllImport("__Internal")] |
| | | private static extern bool TT_Init(); |
| | | // /// <summary> |
| | | // /// 抖音小游戏平台实现 |
| | | // /// </summary> |
| | | // 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 void TT_Login(string callbackObjectName, string callbackMethodName); |
| | | |
| | | [DllImport("__Internal")] |
| | | private static extern string TT_GetSystemInfo(); |
| | | // [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_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_CreateRewardedVideoAd(string adUnitId, string callbackObjectName, string callbackMethodName); |
| | | |
| | | [DllImport("__Internal")] |
| | | private static extern void TT_SetStorageSync(string key, string value); |
| | | // [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 string TT_GetStorageSync(string key); |
| | | |
| | | [DllImport("__Internal")] |
| | | private static extern void TT_VibrateShort(); |
| | | // [DllImport("__Internal")] |
| | | // private static extern void TT_VibrateShort(); |
| | | |
| | | [DllImport("__Internal")] |
| | | private static extern void TT_VibrateLong(); |
| | | #endif |
| | | // [DllImport("__Internal")] |
| | | // private static extern void TT_VibrateLong(); |
| | | // #endif |
| | | |
| | | private SystemInfo _cachedSystemInfo; |
| | | private bool _isInitialized = false; |
| | | // private SystemInfo _cachedSystemInfo; |
| | | // private bool _isInitialized = false; |
| | | |
| | | public async UniTask<bool> InitAsync() |
| | | { |
| | | Debug.Log("[DouyinPlatform] 初始化抖音小游戏平台"); |
| | | // public async UniTask<bool> 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 |
| | | } |
| | | // #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 PlatformType GetPlatformType() |
| | | // { |
| | | // return PlatformType.Douyin; |
| | | // } |
| | | |
| | | public async UniTask<LoginResult> LoginAsync() |
| | | { |
| | | Debug.Log("[DouyinPlatform] 开始登录"); |
| | | // public async UniTask<LoginResult> 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 |
| | | } |
| | | // #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<bool> ShareAsync(ShareData shareData) |
| | | { |
| | | Debug.Log($"[DouyinPlatform] 分享: {shareData.Title}"); |
| | | // public async UniTask<bool> ShareAsync(ShareData shareData) |
| | | // { |
| | | // Debug.Log($"[DouyinPlatform] 分享: {shareData.Title}"); |
| | | |
| | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | | TT_ShareAppMessage(shareData.Title, shareData.ImageUrl); |
| | | #endif |
| | | // #if UNITY_WEBGL && !UNITY_EDITOR |
| | | // TT_ShareAppMessage(shareData.Title, shareData.ImageUrl); |
| | | // #endif |
| | | |
| | | await UniTask.Delay(300); |
| | | return true; |
| | | } |
| | | // await UniTask.Delay(300); |
| | | // return true; |
| | | // } |
| | | |
| | | public async UniTask<AdResult> ShowAdAsync(AdType adType) |
| | | { |
| | | Debug.Log($"[DouyinPlatform] 显示广告: {adType}"); |
| | | // public async UniTask<AdResult> ShowAdAsync(AdType adType) |
| | | // { |
| | | // Debug.Log($"[DouyinPlatform] 显示广告: {adType}"); |
| | | |
| | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | | // TODO: 实现抖音广告逻辑 |
| | | #endif |
| | | // #if UNITY_WEBGL && !UNITY_EDITOR |
| | | // // TODO: 实现抖音广告逻辑 |
| | | // #endif |
| | | |
| | | await UniTask.Delay(1000); |
| | | return new AdResult { Success = true, Completed = true }; |
| | | } |
| | | // await UniTask.Delay(1000); |
| | | // return new AdResult { Success = true, Completed = true }; |
| | | // } |
| | | |
| | | public async UniTask<bool> SaveDataAsync(string key, string value) |
| | | { |
| | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | | TT_SetStorageSync(key, value); |
| | | #endif |
| | | await UniTask.Yield(); |
| | | return true; |
| | | } |
| | | // public async UniTask<bool> SaveDataAsync(string key, string value) |
| | | // { |
| | | // #if UNITY_WEBGL && !UNITY_EDITOR |
| | | // TT_SetStorageSync(key, value); |
| | | // #endif |
| | | // await UniTask.Yield(); |
| | | // return true; |
| | | // } |
| | | |
| | | public async UniTask<string> 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<string> 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<string> DownloadFileAsync(string url, string localPath, Action<float> onProgress = null) |
| | | { |
| | | Debug.Log($"[DouyinPlatform] 下载文件: {url}"); |
| | | await UniTask.Delay(500); |
| | | return localPath; |
| | | } |
| | | // public async UniTask<string> DownloadFileAsync(string url, string localPath, Action<float> onProgress = null) |
| | | // { |
| | | // Debug.Log($"[DouyinPlatform] 下载文件: {url}"); |
| | | // await UniTask.Delay(500); |
| | | // return localPath; |
| | | // } |
| | | |
| | | public SystemInfo GetSystemInfo() |
| | | { |
| | | if (_cachedSystemInfo != null) |
| | | return _cachedSystemInfo; |
| | | // public SystemInfo GetSystemInfo() |
| | | // { |
| | | // if (_cachedSystemInfo != null) |
| | | // return _cachedSystemInfo; |
| | | |
| | | #if UNITY_WEBGL && !UNITY_EDITOR |
| | | try |
| | | { |
| | | string jsonInfo = TT_GetSystemInfo(); |
| | | _cachedSystemInfo = JsonUtility.FromJson<SystemInfo>(jsonInfo); |
| | | return _cachedSystemInfo; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Debug.LogError($"[DouyinPlatform] 获取系统信息失败: {e.Message}"); |
| | | return CreateMockSystemInfo(); |
| | | } |
| | | #else |
| | | return CreateMockSystemInfo(); |
| | | #endif |
| | | } |
| | | // #if UNITY_WEBGL && !UNITY_EDITOR |
| | | // try |
| | | // { |
| | | // string jsonInfo = TT_GetSystemInfo(); |
| | | // _cachedSystemInfo = JsonUtility.FromJson<SystemInfo>(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 |
| | | } |
| | | // 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 |
| | | }; |
| | | } |
| | | } |
| | | // 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 |
| | | // }; |
| | | // } |
| | | // } |