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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
| using UnityEngine; using System; using Newtonsoft.Json; using System.IO;
using Cysharp.Threading.Tasks; using System.Reflection; using System.Collections.Generic; #if WEIXINMINIGAME using WeChatWASM; #endif
#if DOUYINMINIGAME using TTSDK; using TTSDK.UNBridgeLib.LitJson; using TTSDK.SimpleJSON; #endif
namespace Common { public static class PlatformUtility { #if DOUYINMINIGAME private static string adId = ""; #else private static string adId = ""; #endif
private static string platformName;
private static string system;
private static string deviceName;
private static Vector2Int screenSize;
private static Vector2Int safeScreenSize;
private static Vector2Int safeScreenLeftTopPoint;
private static Action adSuccess; private static Action adFail;
private static Action<PlatformOnShowData> returnGameCallBack;
private static Action hideOrExitGameCallBack;
public static Vector2Int SafeScreenSize => safeScreenSize; public static Vector2Int ScreenSize => screenSize;
public static void PlatformUtilityInit() { Debug.Log("平台初始化");
#if UNITY_EDITOR platformName = "编辑器"; screenSize = new Vector2Int(Screen.width, Screen.height); safeScreenSize = new Vector2Int((int)Screen.safeArea.width, (int)Screen.safeArea.height); system = UnityEngine.SystemInfo.operatingSystem; deviceName = UnityEngine.SystemInfo.deviceName;
#elif DOUYINMINIGAME
platformName = "抖音"; if (CanIUse.GetSystemInfo) { var systemInfo = TT.GetSystemInfo();
system = systemInfo.system; deviceName = systemInfo.model; screenSize = new Vector2Int(systemInfo.screenWidth, systemInfo.screenHeight); safeScreenSize = new Vector2Int(systemInfo.safeArea.width, systemInfo.safeArea.height); safeScreenLeftTopPoint = new Vector2Int(systemInfo.safeArea.left, systemInfo.safeArea.top); } else { screenSize = new Vector2Int(Screen.width, Screen.height); safeScreenSize = new Vector2Int((int)Screen.safeArea.width, (int)Screen.safeArea.height); Debug.Log("接口无法获取对应信息"); } #elif WEIXINMINIGAME platformName = "微信";
screenSize = new Vector2Int(Screen.width, Screen.height); safeScreenSize = new Vector2Int((int)Screen.safeArea.width, (int)Screen.safeArea.height); system = UnityEngine.SystemInfo.operatingSystem; deviceName = UnityEngine.SystemInfo.deviceName; #else platformName = "其他"; screenSize = new Vector2Int(Screen.width, Screen.height); safeScreenSize = new Vector2Int((int)Screen.safeArea.width, (int)Screen.safeArea.height); system = UnityEngine.SystemInfo.operatingSystem; deviceName = UnityEngine.SystemInfo.deviceName; #endif }
public static string GameVersion { get { Debug.Log("获取游戏版本"); #if UNITY_EDITOR return "编辑器"; #elif DOUYINMINIGAME return TT.GameVersion; #elif WEIXINMINIGAME return WeChatWASM.WX.GetAccountInfoSync().miniProgram.version; #else return "其他"; #endif } }
public static string SystemInfo => deviceName + system;
public static string PlatformName => platformName;
public static void SDKInit(Action success) { #if UNITY_EDITOR success?.Invoke(); #elif DOUYINMINIGAME TTSDK.TT.InitSDK((code, env) => { success?.Invoke(); }); #elif WEIXINMINIGAME WeChatWASM.WX.InitSDK((initResult) => { Debug.Log("微信SDK初始化调用完毕,初始化值为:" + initResult); success?.Invoke();
}); #else success?.Invoke(); #endif }
public static void SetFrameLimit(int frame) {
Debug.Log("帧限制"); #if UNITY_EDITOR Application.targetFrameRate = frame; #elif DOUYINMINIGAME TT.SetPreferredFramesPerSecond(frame); #else Application.targetFrameRate = frame; #endif }
public static void PlatformLogin(Action<PlatformLoginCodeInfo> success,Action<string> fail,bool forceLogin) { #if UNITY_EDITOR success.Invoke(new PlatformLoginCodeInfo() { code = "Test", anonymousCode = "Test" }); #elif DOUYINMINIGAME TT.Login((code, anonymousCode, isLogin) => { PlatformLoginCodeInfo info = new PlatformLoginCodeInfo() { code = code, anonymousCode = anonymousCode }; success.Invoke(info); }, (errMsg) => { fail?.Invoke(errMsg); }, forceLogin); #elif WEIXINMINIGAME WeChatWASM.LoginOption loginOption = new WeChatWASM.LoginOption() { success = (result) => { success?.Invoke(new PlatformLoginCodeInfo() { code = result.code }); }, fail = (result) => { Debug.LogError($"登录失败: code={result.errno}, msg={result.errMsg}"); } }; WeChatWASM.WX.Login(loginOption); #else success.Invoke(new PlatformLoginCodeInfo() { code = "Test", anonymousCode = "Test" }); #endif }
public static void CheckGetUserInfoAuth(Action success, Action<string> reject, Action<string> fail) { Debug.Log("SDK获取用户授权"); #if UNITY_EDITOR success?.Invoke(); #elif DOUYINMINIGAME TT.GetUserInfoAuth( (auth) => { if (auth) success?.Invoke(); else reject?.Invoke("用户不给授权"); }, (errMsg) => { fail?.Invoke(errMsg); }); #elif WEIXINMINIGAME WeChatWASM.GetSettingOption option = new WeChatWASM.GetSettingOption() { success = (res) => { bool isAuthorized = res.authSetting != null && res.authSetting.ContainsKey("scope.userInfo") && res.authSetting["scope.userInfo"];
if (isAuthorized) { success?.Invoke(); } else { reject?.Invoke("用户没给授权"); } }, fail = (err) => { fail?.Invoke(err.errMsg); } }; WeChatWASM.WX.GetSetting(option); #else success?.Invoke(); #endif }
public static void OpenSettingsPanel(Action success, Action<string> reject, Action<string> fail) { Debug.Log("SDK打开授权界面"); #if UNITY_EDITOR success?.Invoke(); #elif DOUYINMINIGAME TT.OpenSettingsPanel( (auth) => { if (auth) success?.Invoke(); else reject?.Invoke("用户不给授权"); }, (errMsg) => { fail?.Invoke(errMsg); }); #elif WEIXINMINIGAME var btn = WeChatWASM.WX.CreateUserInfoButton(0, 0, Screen.width, Screen.height, "zh_CN", false); btn.Show(); btn.OnTap((res) => { if (res.errCode == 0) { success?.Invoke(); } else { reject?.Invoke("用户拒绝"); } btn.Destroy(); }); #else success?.Invoke(); #endif }
public static System.Object ShowExtraGetUserInfoBtn(Vector4 rect, Action success, Action<string> reject) { #if UNITY_EDITOR success?.Invoke(); return null; #elif WEIXINMINIGAME var btn = WeChatWASM.WX.CreateUserInfoButton((int)rect.x, (int)rect.y, (int)rect.z, (int)rect.w, "zh_CN", false); btn.Show(); btn.OnTap((res) => { if (res.errCode == 0) { success?.Invoke(); } else { reject?.Invoke("用户拒绝"); } }); return btn; #else success?.Invoke(); return null; #endif }
public static void GetUserInfo(Action<PlatformUserInfo> success, Action<string> fail) { Debug.Log("SDK获取用户信息"); #if UNITY_EDITOR success.Invoke(new PlatformUserInfo() { nickName = "测试", }); #elif DOUYINMINIGAME TT.GetUserInfo(true,(ref TTSDK.TTUserInfo tTUserInfo)=> { success?.Invoke(PlatformUserInfo.TransiteToInfo(tTUserInfo)); }, (errMsg) => { fail?.Invoke(errMsg); }); #elif WEIXINMINIGAME var option = new WeChatWASM.GetUserInfoOption() { success = (result) => { success?.Invoke(PlatformUserInfo.TransiteToInfo(result.userInfo)); }, fail = (result) => { fail?.Invoke(result.errMsg); } }; WeChatWASM.WX.GetUserInfo(option); #else success.Invoke(new PlatformUserInfo() { nickName = "测试", }); #endif }
public static void ShareGame(Action success,Action fail,Action cancel) { Debug.Log("SDK分享游戏"); #if UNITY_EDITOR success?.Invoke(); #elif DOUYINMINIGAME TT.ShareAppMessage(new JsonData(JsonConvert.SerializeObject(new PlatformShareCommonData())), (x) => { success?.Invoke(); }, (x) => { fail?.Invoke(); }, () => { cancel?.Invoke(); }); #elif WEIXINMINIGAME var option = new WeChatWASM.ShareAppMessageOption() { title = "哇!卡皮巴拉", };
WeChatWASM.WX.ShareAppMessage(option); var shareData = new PlatformShareCommonData(); shareData.success = success; shareData.failure = fail; shareData.complete = OffReturnGameListen; shareData.SetDelayTime(2.5f); AddReturnGameListen(shareData.CallBack); #else success?.Invoke(); #endif }
#if WEIXINMINIGAME private static WeChatWASM.WXRewardedVideoAd rewardedVideoAd;
private static void WxADOnClose(WeChatWASM.WXRewardedVideoAdOnCloseResponse result) { if (result.isEnded) { adSuccess?.Invoke(); } else { adFail?.Invoke(); } }
private static void WxADOnError(WeChatWASM.WXADErrorResponse result) { adFail?.Invoke(); }
public static void WxADOnLoad(WeChatWASM.WXADLoadResponse result) { rewardedVideoAd.Show(); }
#elif DOUYINMINIGAME private static TTSDK.TTRewardedVideoAd douYinAd;
private static void DouYinOnClose(bool result,int flag) { if (result) adSuccess?.Invoke(); else adFail?.Invoke(); }
private static void DouYiOnError(int flag, string errMsg) { adFail?.Invoke(); } #endif public static void PlayRewardAd(Action success,Action fail) { adSuccess = success; adFail = fail;
#if GAMETESTMACRO success?.Invoke(); bool test = true; if(test) return; #endif
#if UNITY_EDITOR Debug.Log("编辑器不播放广告"); success?.Invoke(); #elif DOUYINMINIGAME if(douYinAd == null) { douYinAd = TT.CreateRewardedVideoAd(new CreateRewardedVideoAdParam() { AdUnitId = adId,
});
douYinAd.OnClose += DouYinOnClose; douYinAd.OnError += DouYiOnError; douYinAd.OnLoad += () => { douYinAd.Show(); }; } douYinAd.Load(); #elif WEIXINMINIGAME
if (rewardedVideoAd == null) { var param = new WeChatWASM.WXCreateRewardedVideoAdParam() { adUnitId = adId };
rewardedVideoAd = WeChatWASM.WX.CreateRewardedVideoAd(param);
rewardedVideoAd.OnError(WxADOnError);
rewardedVideoAd.OnClose(WxADOnClose); rewardedVideoAd.OnLoad(WxADOnLoad); }
rewardedVideoAd.Load(); #else Debug.Log("未适配平台"); success?.Invoke(); #endif }
public static void ReportPoint(string eventName, PlatformReportData reportData) { #if UNITY_EDITOR Debug.Log("编辑器不报点"); #elif DOUYINMINIGAME TT.ReportAnalytics(eventName, reportData.CacheData); #elif WEIXINMINIGAME WeChatWASM.WX.ReportEvent(eventName, reportData.TransformData()); #else Debug.Log("未适配平台"); #endif }
public static void Vibrate() { Debug.Log("SDK震动"); #if UNITY_EDITOR Debug.Log("编辑器不震动"); #elif DOUYINMINIGAME TT.VibrateLong(new VibrateLongParam()); #elif WEIXINMINIGAME WeChatWASM.WX.VibrateLong(new WeChatWASM.VibrateLongOption()); #else Handheld.Vibrate(); #endif }
#if WEIXINMINIGAME public static void ReturnGameCallBack(WeChatWASM.OnShowListenerResult result) { returnGameCallBack?.Invoke(PlatformOnShowData.ToData(result)); } #endif
public static void AddReturnGameListen(Action<PlatformOnShowData> action) { #if UNITY_EDITOR Debug.Log("编辑器不存在这个"); bool test = true; if (test) return; #endif returnGameCallBack += action; #if WEIXINMINIGAME WeChatWASM.WX.OffShow(ReturnGameCallBack); WeChatWASM.WX.OnShow(ReturnGameCallBack); #else returnGameCallBack -= action; #endif }
public static void RemoveReturnGameListen(Action<PlatformOnShowData> action) { returnGameCallBack -= action; }
public static void ClearReturnGameListen(Action<PlatformOnShowData> action) { returnGameCallBack = null; }
public static void OffReturnGameListen() { returnGameCallBack = null; #if UNITY_EDITOR Debug.Log("编辑器不存在这个"); #elif WEIXINMINIGAME WeChatWASM.WX.OffShow(ReturnGameCallBack); #else #endif }
#if WEIXINMINIGAME public static void HideOrExitGameCallBack(WeChatWASM.GeneralCallbackResult result) { hideOrExitGameCallBack?.Invoke(); } #endif
#if DOUYINMINIGAME private static bool SetOnBeforeExitAppListener() { hideOrExitGameCallBack?.Invoke(); return true; } #endif
public static void AddExitGameListen(Action action) { #if UNITY_EDITOR Debug.Log("编辑器不存在这个"); bool test = true; if (test) return; #endif hideOrExitGameCallBack += action; #if WEIXINMINIGAME WeChatWASM.WX.OffHide(HideOrExitGameCallBack); WeChatWASM.WX.OnHide(HideOrExitGameCallBack); #elif DOUYINMINIGAME TTSDK.TT.SetOnBeforeExitAppListener(null); TTSDK.TT.SetOnBeforeExitAppListener(SetOnBeforeExitAppListener); #else #endif } }
}
|