平台判断辅助工具类:PlatformHelper.ts

This commit is contained in:
chen wei bo
2025-09-25 11:24:56 +08:00
parent b5de68d76b
commit 7896dbacb2
4 changed files with 107 additions and 5 deletions
@@ -0,0 +1,91 @@
import { sys } from "cc";
/**
* 跨平台辅助工具类
* 统一封装常见平台的判断逻辑
*/
export class PlatformHelper {
/** 是否微信小游戏 */
public static isWechat(): boolean {
return sys.platform === sys.Platform.WECHAT_GAME && typeof window["wx"] !== "undefined";
}
/** 是否字节跳动(抖音/头条)小游戏 */
public static isByteDance(): boolean {
return sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && typeof window["tt"] !== "undefined";
}
/** 是否快手小游戏 */
public static isKuaishou(): boolean {
return typeof window["ks"] !== 'undefined';
}
/** 是否百度小游戏 */
public static isBaidu(): boolean {
return sys.platform === sys.Platform.BAIDU_MINI_GAME;
}
/** 是否支付宝/阿里小游戏 */
public static isAlipay(): boolean {
return sys.platform === sys.Platform.ALIPAY_MINI_GAME;
}
/** 是否 Oppo 小游戏 */
public static isOppo(): boolean {
return sys.platform === sys.Platform.OPPO_MINI_GAME;
}
/** 是否 Vivo 小游戏 */
public static isVivo(): boolean {
return sys.platform === sys.Platform.VIVO_MINI_GAME;
}
/** 是否华为快应用 */
public static isHuawei(): boolean {
return sys.platform === sys.Platform.HUAWEI_QUICK_GAME;
}
/** 是否Windows桌面 */
public static isWinDesktop(): boolean {
return sys.platform === sys.Platform.WIN32;
}
/** 是否MacOS桌面 */
public static isMacOSDesktop(): boolean {
return sys.platform === sys.Platform.MACOS;
}
/** 是否桌面(Windows 或 MacOS */
public static isDesktop(): boolean {
return sys.platform === sys.Platform.WIN32 || sys.platform === sys.Platform.MACOS;
}
/** 是否原生 Android */
public static isAndroid(): boolean {
return sys.isNative && sys.os === sys.OS.ANDROID;
}
/** 是否原生 iOS */
public static isIOS(): boolean {
return sys.isNative && sys.os === sys.OS.IOS;
}
/** 获取当前平台名称(调试用) */
public static getPlatformName(): string {
if (this.isWechat()) return "微信小游戏";
if (this.isByteDance()) return "字节跳动小游戏";
if (this.isKuaishou()) return "快手小游戏";
if (this.isBaidu()) return "百度小游戏";
if (this.isAlipay()) return "支付宝小游戏";
if (this.isOppo()) return "Oppo 小游戏";
if (this.isVivo()) return "Vivo 小游戏";
if (this.isHuawei()) return "华为快应用";
if (this.isWinDesktop()) return "Windows 桌面";
if (this.isMacOSDesktop()) return "MacOS 桌面";
if (this.isAndroid()) return "原生 Android";
if (this.isIOS()) return "原生 iOS";
// 兜底
return "未知平台";
}
}
@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "669465b9-541e-4a6e-92ef-c90e11fd463d",
"files": [],
"subMetas": {},
"userData": {}
}
+5 -4
View File
@@ -8,6 +8,7 @@ import AudioManager from "../Manager/AudioManager";
import GlobalValue, { VideoRoleType } from "../Common/GlobalValue";
import { luffaSDK } from "./luffaSDK";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
import { PlatformHelper } from "./PlatformHelper";
const { ccclass, property } = _decorator;
@@ -91,13 +92,13 @@ export class SDKManager { //extends Component
logger.log("平台值 ks:",window["ks"], typeof window["ks"])
logger.log("平台值 tt:",window["tt"], typeof window["tt"])
if (typeof window["ks"] !== 'undefined') {
if (PlatformHelper.isKuaishou()) {
this.sdk_channel = E_SDK_Channel.KS
this.sdk = new KSSDK() //快手
}else if (sys.platform === sys.Platform.WECHAT_GAME && typeof window["wx"] !== 'undefined') {
}else if (PlatformHelper.isWechat()) {
this.sdk_channel = E_SDK_Channel.WX
this.sdk = new luffaSDK() //微信
} else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME && typeof window["tt"] !== 'undefined') {
} else if (PlatformHelper.isByteDance()) {
this.sdk_channel = E_SDK_Channel.BD
this.sdk = new ByteDanceSDK() //抖音(头条)
} else {
@@ -105,7 +106,7 @@ export class SDKManager { //extends Component
this.sdk = new WebSDK()
}
logger.log("当前平台:",sys.platform, this.sdk_channel)
logger.log("当前平台:", PlatformHelper.getPlatformName(), this.sdk_channel)
if (GlobalValue.NpcVideoMod) {
GlobalValue.NpcVideoMod = this.sdk_channel === E_SDK_Channel.WX || this.sdk_channel === E_SDK_Channel.WEB
}
+2 -1
View File
@@ -1,6 +1,7 @@
import { _decorator, assetManager, Component, game, macro, sys } from "cc";
import { SDKManager } from "../Main/Channel/SDKManager";
import PlayerDataManager from "../Main/Manager/PlayerDataManager";
import { PlatformHelper } from "../Main/Channel/PlatformHelper";
const { ccclass, property } = _decorator;
@@ -14,7 +15,7 @@ export class GameEntry extends Component {
assetManager.downloader.maxConcurrency = 20;
assetManager.downloader.maxRequestsPerFrame = 10;
if (sys.platform == sys.Platform.WECHAT_GAME) {
if (PlatformHelper.isWechat()) {
assetManager.cacheManager.cacheEnabled = true;
macro.CLEANUP_IMAGE_CACHE = true;
// DynamicAtlasManager.instance.enabled = false; //关闭动态图集