快过年了,分享一个平台判断的代码

最近发布了很多平台,利用cc.sys.platform来做平台判断,其实有很多问题。所以自己总结了一下 。写了平台判断的代码。以下几个平台都是可以正确判断的。

export class PlatformJudge {

  //oppo 小游戏
  static get isOppo(): boolean {
    return window.qg && window.qg.setLoadingProgress && !window.hbs;
  }

  //vivo小游戏
  static get isVivo(): boolean {
    return window.qg && !window.qg.setLoadingProgress && !window.hbs;
  }

  //qq小游戏
  static get isQQ(): boolean {
    return window.qq;
  }

  //微信小游戏
  static get isWX(): boolean {
    return window.wx && !window.qq && !window.tt;
  }

  //头条小游戏
  static get isTT(): boolean {
    return window.tt;
  }

  //华为小游戏
  static get isHW(): boolean {
    return window.hbs;
  }
}
2赞

感觉还是 typeof window[‘qg’]!==“undefined” 更安全。

要的就是快。要啥子安全哦

1赞

有没有试过 构建时常量

import { log } from "cc";
import { WECHAT } from "cc/env";
if (WECHAT) {
   log(`微信`);
}


这个表里不能区分 oppo 和 vivo 平台的吧。

import { OPPO, VIVO } from 'cc/env';
if (OPPO) {}
if (VIVO) {}

这样会有啥问题嘛?

cc.sys.platform出什么问题了?

1赞

哦哦,我明白你的意思了。

哎,小游戏环境一言难尽啊

为啥啊, 你们不能分辨出来平台吗

ios android 平台标识呢