SDK相关的整理
This commit is contained in:
@@ -1,346 +1,188 @@
|
||||
import { _decorator, Component, sys, utils } from "cc";
|
||||
import Utils from "../Common/Utils";
|
||||
import { WXSDK } from "./wxSDK";
|
||||
import { ByteDanceSDK } from "./bdSDK";
|
||||
import { WebSDK } from "./webSDK";
|
||||
import { KSSDK } from "./ksSDK";
|
||||
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";
|
||||
import { IAdSDK } from "./core/IAdSDK";
|
||||
import { IWalletSDK } from "./core/IWalletSDK";
|
||||
import { IPlatformSDK } from "./core/IPlatformSDK";
|
||||
import { IAnalyticsSDK } from "./core/IAnalyticsSDK";
|
||||
import { WxPlatformSDK } from "./platform/WxPlatformSDK";
|
||||
import { WxAdSDK } from "./ads/WxAdSDK";
|
||||
import { LuffaWalletSDK } from "./wallet/LuffaWalletSDK";
|
||||
import { UmaAnalyticsSDK } from "./analytics/UmaAnalyticsSDK";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**运行渠道 */
|
||||
export enum E_SDK_Channel {
|
||||
/**微信 */
|
||||
WX,
|
||||
/**字节跳动 (抖音、头条) */
|
||||
BD,
|
||||
/**网页 */
|
||||
WEB,
|
||||
/**快手 */
|
||||
KS,
|
||||
}
|
||||
|
||||
/**玩家SDK信息 */
|
||||
export interface I_SDK_UserInfo {
|
||||
/**昵称 */
|
||||
nickName: string;
|
||||
/**头像url */
|
||||
avatarUrl: string;
|
||||
}
|
||||
|
||||
/**玩家SDK信息回调 */
|
||||
export interface I_SDK_UserInfo_Callback {
|
||||
(info: I_SDK_UserInfo): void;
|
||||
}
|
||||
|
||||
//渠道管理器
|
||||
// 渠道管理器
|
||||
@ccclass('SDKManager')
|
||||
export class SDKManager { //extends Component
|
||||
export class SDKManager {
|
||||
private static _I: SDKManager = null;
|
||||
public static get I(): SDKManager {
|
||||
if (!SDKManager._I) {
|
||||
SDKManager._I = new SDKManager();
|
||||
SDKManager._I.init();
|
||||
}
|
||||
return SDKManager._I;
|
||||
}
|
||||
// 广告,分享
|
||||
private adSDK: IAdSDK | null = null;
|
||||
// 钱包
|
||||
private walletSDK: IWalletSDK | null = null;
|
||||
// 平台基础
|
||||
private platformSDK: IPlatformSDK | null = null;
|
||||
// 分析统计
|
||||
private analyticsSDK: IAnalyticsSDK | null = null;
|
||||
|
||||
private init(){
|
||||
}
|
||||
initEmpty(){
|
||||
}
|
||||
|
||||
//玩家头像
|
||||
private static _avatarUrl: string = "";
|
||||
public static get avatarUrl(): string {
|
||||
return SDKManager._avatarUrl;
|
||||
}
|
||||
public static set avatarUrl(value: string) {
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
SDKManager._avatarUrl = value;
|
||||
}
|
||||
|
||||
//玩家昵称
|
||||
private static _nickName: string = "";
|
||||
public static get nickName(): string {
|
||||
return SDKManager._nickName;
|
||||
}
|
||||
public static set nickName(value: string) {
|
||||
if (value == null) {
|
||||
value = "游客";
|
||||
}
|
||||
SDKManager._nickName = value;
|
||||
}
|
||||
private constructor() {
|
||||
|
||||
}
|
||||
|
||||
private static sdk_channel:E_SDK_Channel = E_SDK_Channel.WEB //当前SDK渠道
|
||||
private static video_reward_map:any
|
||||
private static share_reward_map:any
|
||||
private static sdk:any
|
||||
// 初始化
|
||||
public init(cb: Function = null) {
|
||||
logger.info("SDKManager 初始化...");
|
||||
// 检测平台
|
||||
if (PlatformHelper.isKuaishou()) {
|
||||
// 快手
|
||||
|
||||
//初始化SDK
|
||||
static init(cb: Function = null) {
|
||||
this.video_reward_map = {}
|
||||
this.share_reward_map = {}
|
||||
|
||||
logger.log("平台值 wx:",window["wx"], typeof window["wx"])
|
||||
logger.log("平台值 ks:",window["ks"], typeof window["ks"])
|
||||
logger.log("平台值 tt:",window["tt"], typeof window["tt"])
|
||||
} else if (PlatformHelper.isWechat()) {
|
||||
// 微信
|
||||
this.adSDK = new WxAdSDK();
|
||||
this.platformSDK = new WxPlatformSDK();
|
||||
} else if (PlatformHelper.isLuffa()) {
|
||||
// Luffa
|
||||
this.walletSDK = new LuffaWalletSDK();
|
||||
} else if (PlatformHelper.isByteDance()) {
|
||||
// 字节跳动
|
||||
|
||||
if (PlatformHelper.isKuaishou()) {
|
||||
this.sdk_channel = E_SDK_Channel.KS
|
||||
this.sdk = new KSSDK() //快手
|
||||
}else if (PlatformHelper.isWechat()) {
|
||||
this.sdk_channel = E_SDK_Channel.WX
|
||||
this.sdk = new luffaSDK() //微信
|
||||
} else if (PlatformHelper.isByteDance()) {
|
||||
this.sdk_channel = E_SDK_Channel.BD
|
||||
this.sdk = new ByteDanceSDK() //抖音(头条)
|
||||
} else {
|
||||
this.sdk_channel = E_SDK_Channel.WEB
|
||||
this.sdk = new WebSDK()
|
||||
}
|
||||
} else {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
this.sdk.init(cb)
|
||||
|
||||
SDKManager.umaInit();
|
||||
}
|
||||
|
||||
|
||||
/**是否是微信平台 */
|
||||
static isWenxin() {
|
||||
return this.sdk_channel === E_SDK_Channel.WX
|
||||
}
|
||||
/**是否是抖音平台 */
|
||||
static isByteDance() {
|
||||
return this.sdk_channel === E_SDK_Channel.BD
|
||||
}
|
||||
/**是否是快手平台 */
|
||||
static isKuaishou() {
|
||||
return this.sdk_channel === E_SDK_Channel.KS
|
||||
}
|
||||
/**是否是web平台 */
|
||||
static isWeb() {
|
||||
return this.sdk_channel === E_SDK_Channel.WEB
|
||||
}
|
||||
|
||||
|
||||
//SDK登录,并返回code
|
||||
static login(cb: Function = null) {
|
||||
this.sdk.login(cb)
|
||||
}
|
||||
|
||||
//检查玩家权限
|
||||
//autoKey 授权类型 : userInfo=用户信息
|
||||
static checkAutoSetting(autoKey:string, cb:Function = null) {
|
||||
this.sdk.checkAutoSetting(autoKey, cb)
|
||||
}
|
||||
|
||||
//获取用户信息
|
||||
static getUserInfo(cb:Function = null) {
|
||||
this.sdk.getUserInfo(cb)
|
||||
}
|
||||
|
||||
//注册视频广告回调
|
||||
static register_video_reward(uuid:string, func:Function) {
|
||||
let video_reward_map = this.video_reward_map || {};
|
||||
video_reward_map[uuid] = func
|
||||
|
||||
this.video_reward_map = video_reward_map;
|
||||
}
|
||||
|
||||
//注册分享回调
|
||||
static register_share_reward(uuid:string, func:Function) {
|
||||
let share_reward_map = this.share_reward_map || {};
|
||||
share_reward_map[uuid] = func
|
||||
|
||||
this.share_reward_map = share_reward_map;
|
||||
}
|
||||
|
||||
//执行视频广告回调
|
||||
private static dispatch_video_reward(uuid:string, tag:number) {
|
||||
let video_reward_map = this.video_reward_map || {};
|
||||
let register_func:Function = video_reward_map[uuid];
|
||||
if (register_func) {
|
||||
register_func(tag)
|
||||
}
|
||||
}
|
||||
|
||||
//执行分享回调
|
||||
private static dispatch_share_reward(uuid:string, tag:number) {
|
||||
let share_reward_map = this.share_reward_map || {};
|
||||
let register_func:Function = share_reward_map[uuid];
|
||||
if (register_func) {
|
||||
register_func(tag)
|
||||
}
|
||||
}
|
||||
|
||||
//视频广告
|
||||
static show_reward_video_ad(uuid:string, tag: number) {
|
||||
SDKManager.video_ad_pre();
|
||||
this.sdk.show_video(()=>{
|
||||
this.dispatch_video_reward(uuid,tag)
|
||||
});
|
||||
}
|
||||
//看视频广告前
|
||||
static video_ad_pre() {
|
||||
logger.log("SDKManager 视频广告前")
|
||||
AudioManager.I.PauseMusic();
|
||||
}
|
||||
//看视频广告后返回游戏
|
||||
static video_ad_back() {
|
||||
logger.log("SDKManager 视频广告后")
|
||||
AudioManager.I.ResumeMusic();
|
||||
}
|
||||
|
||||
//插屏广告
|
||||
static show_interstitial_ad() {
|
||||
this.sdk.show_splash();
|
||||
}
|
||||
|
||||
//展示BANNER
|
||||
static show_banner_ad() {
|
||||
this.sdk.show_banner();
|
||||
}
|
||||
|
||||
//隐藏BANNBER
|
||||
static hide_banner_ad() {
|
||||
this.sdk.hide_banner();
|
||||
}
|
||||
|
||||
//主动拉起分享
|
||||
static show_share() {
|
||||
this.sdk.show_share();
|
||||
}
|
||||
|
||||
//分享得奖励
|
||||
static show_reward_share(uuid:string, tag: number) {
|
||||
this.sdk.show_share(()=>{
|
||||
this.dispatch_share_reward(uuid,tag)
|
||||
});
|
||||
}
|
||||
|
||||
//获取设备分辨率
|
||||
static getWindowSize() {
|
||||
return this.sdk.getWindowSize();
|
||||
}
|
||||
|
||||
/**显示游戏圈
|
||||
* @param leftRatio 左边距占屏幕宽度的比例
|
||||
* @param topRatio 顶部边距占屏幕高度的比例
|
||||
* @param widthRatio 游戏圈宽度占屏幕宽度的比例
|
||||
* @param heightRatio 游戏圈高度占屏幕高度的比例
|
||||
*/
|
||||
static show_gameClub(leftRatio, topRatio, widthRatio, heightRatio) {
|
||||
this.sdk.show_gameClub(leftRatio, topRatio, widthRatio, heightRatio);
|
||||
}
|
||||
//隐藏游戏圈
|
||||
static hide_gameClub() {
|
||||
this.sdk.hide_gameClub();
|
||||
}
|
||||
|
||||
|
||||
/**检查是否支持base64音频播放*/
|
||||
static checkSupportBase64Audio() {
|
||||
return this.sdk.checkSupportBase64Audio();
|
||||
}
|
||||
/**播放base64音频 */
|
||||
static playBase64Audio(base64: string, loop: boolean = false) {
|
||||
this.sdk.playBase64Audio(base64, loop);
|
||||
}
|
||||
|
||||
|
||||
//记录GM日志
|
||||
private static _gmLogString: string = "";
|
||||
private static _gmLogCount: number = 0; //记录日志条数
|
||||
static get GMLogString(): string {
|
||||
return SDKManager._gmLogString;
|
||||
}
|
||||
static ShowGMLog(log: string){
|
||||
Utils.Log(log);
|
||||
if (SDKManager._gmLogCount > 100){
|
||||
SDKManager.ClearGMLog();
|
||||
}
|
||||
SDKManager._gmLogString += log + "\n";
|
||||
SDKManager._gmLogCount++;
|
||||
}
|
||||
static ClearGMLog(){
|
||||
SDKManager._gmLogString = "";
|
||||
SDKManager._gmLogCount = 0;
|
||||
}
|
||||
|
||||
// 默认加载 UMA 分析
|
||||
this.analyticsSDK = new UmaAnalyticsSDK();
|
||||
|
||||
/**是否支持客服功能 */
|
||||
static KefuSupport() {
|
||||
return this.sdk.kefuSupport();
|
||||
}
|
||||
/**打开客服 */
|
||||
static OpenKefu() {
|
||||
this.sdk.openKefu();
|
||||
// 初始化各模块
|
||||
this.adSDK?.init();
|
||||
this.walletSDK?.init();
|
||||
this.platformSDK?.init();
|
||||
this.analyticsSDK?.init();
|
||||
|
||||
// 回调
|
||||
cb && cb();
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁所有 SDK
|
||||
*
|
||||
* - 逐个调用各 SDK 的 `destroy` 方法
|
||||
* - 用于游戏退出、切换账号、切换平台时释放资源
|
||||
* - 调用时机:游戏关闭 / 重新进入大厅 / SDK 切换
|
||||
*/
|
||||
public destroyAll() {
|
||||
this.adSDK?.destroy?.();
|
||||
this.adSDK = null;
|
||||
this.walletSDK?.destroy?.();
|
||||
this.walletSDK = null;
|
||||
this.platformSDK?.destroy?.();
|
||||
this.platformSDK = null;
|
||||
this.analyticsSDK?.destroy?.();
|
||||
this.analyticsSDK = null;
|
||||
logger.log("所有 SDK 已销毁并清空引用");
|
||||
}
|
||||
|
||||
//region 播放背景视频 cbMap = {cbReady, cbEnded}
|
||||
static PlayBgAudio(remoteUrl:string, loop:boolean = false, extra:any = {}) {
|
||||
this.sdk.playBgAudio(remoteUrl, loop, extra);
|
||||
}
|
||||
//添加要播放的表情视频
|
||||
static AddEmoAudio(remoteUrl:string, vtype:VideoRoleType, loop:boolean = false, extra:any = {}) {
|
||||
this.sdk.addEmoAudio(remoteUrl, vtype, loop, extra);
|
||||
/** --------------------------------------------------------- 广告,分享 --------------------------------------------------------- */
|
||||
|
||||
// 展示激励视频广告
|
||||
public showRewardVideo(cb: (ok: boolean) => void): void {
|
||||
if (!this.adSDK) return;
|
||||
this.adSDK.showRewardVideo(cb)
|
||||
}
|
||||
|
||||
// 展示插屏广告
|
||||
public showInterstitial(): void {
|
||||
if (!this.adSDK) return;
|
||||
this.adSDK.showInterstitial()
|
||||
}
|
||||
|
||||
// 展示 Banner 广告
|
||||
public showBanner(): void {
|
||||
if (!this.adSDK) return;
|
||||
this.adSDK.showBanner()
|
||||
}
|
||||
|
||||
// 隐藏 Banner 广告
|
||||
public hideBanner(): void {
|
||||
if (!this.adSDK) return;
|
||||
this.adSDK.hideBanner()
|
||||
}
|
||||
|
||||
// 主动拉起分享
|
||||
public showShare(cb?: Function): void {
|
||||
if (!this.adSDK) return;
|
||||
this.adSDK.showShare(cb)
|
||||
}
|
||||
|
||||
/** --------------------------------------------------------- 钱包 --------------------------------------------------------- */
|
||||
|
||||
// 获取当前钱包地址
|
||||
public getWalletAddress(): string | null {
|
||||
if (!this.walletSDK) return null;
|
||||
return this.walletSDK.getWalletAddress()
|
||||
}
|
||||
|
||||
/** --------------------------------------------------------- 平台基础 --------------------------------------------------------- */
|
||||
|
||||
// 登录
|
||||
public login(cb?: Function): void {
|
||||
if (!this.platformSDK) return;
|
||||
this.platformSDK.login(cb)
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
public getUserInfo(cb: (info: { nickName: string; avatarUrl: string }) => void): void {
|
||||
if (!this.platformSDK) return;
|
||||
this.platformSDK.getUserInfo(cb)
|
||||
}
|
||||
|
||||
// 检查平台权限
|
||||
public checkPermission(scope: string, cb: (granted: boolean) => void): void {
|
||||
if (!this.platformSDK) return;
|
||||
this.platformSDK.checkPermission(scope, cb)
|
||||
}
|
||||
|
||||
|
||||
//region 语音转文字
|
||||
static startRecordRecognition() {
|
||||
if (this.sdk.startRecordRecognition) {
|
||||
this.sdk.startRecordRecognition();
|
||||
} else {
|
||||
Utils.Log("sdk not support startRecordRecognition");
|
||||
}
|
||||
}
|
||||
static stopRecordRecognition() {
|
||||
if (this.sdk.stopRecordRecognition) {
|
||||
this.sdk.stopRecordRecognition();
|
||||
} else {
|
||||
Utils.Log("sdk not support stopRecordRecognition");
|
||||
}
|
||||
// 复制文本到剪贴板
|
||||
public copyToClipboard(text: string, cb?: Function): void {
|
||||
if (!this.platformSDK) return;
|
||||
this.platformSDK.copyToClipboard(text, cb);
|
||||
}
|
||||
|
||||
|
||||
//region 复制到剪贴板
|
||||
/**复制文本到剪贴板 */
|
||||
static copyToClipboard(text: string, cb?: Function) {
|
||||
if (this.sdk.copyToClipboard) {
|
||||
this.sdk.copyToClipboard(text, cb);
|
||||
} else {
|
||||
Utils.Log("当前平台不支持复制功能");
|
||||
cb && cb(false);
|
||||
}
|
||||
// 客服功能是否可用
|
||||
public kefuSupport(): boolean {
|
||||
if (!this.platformSDK) return false;
|
||||
return this.platformSDK.kefuSupport();
|
||||
}
|
||||
|
||||
//region 友盟+
|
||||
static Uma_Event_TalkRound = "_um.talk.round"; //聊天轮次
|
||||
static Uma_Event_LvUnlock = "_um.lv.unlock"; //解锁新关卡
|
||||
/**初始化友盟 */
|
||||
static umaInit() {
|
||||
this.sdk.umaInit();
|
||||
}
|
||||
/**友盟功能是否生效 */
|
||||
static umaSwt() {
|
||||
return this.sdk.umaSwt();
|
||||
}
|
||||
/**友盟事件上报 */
|
||||
static umaEvent(eventName: string, value: any = {}) {
|
||||
this.sdk.umaEvent(eventName, value);
|
||||
}
|
||||
// 打开客服会话
|
||||
public openKefu(): void {
|
||||
if (!this.platformSDK) return;
|
||||
this.platformSDK.openKefu();
|
||||
}
|
||||
|
||||
// 获取设备分辨率
|
||||
public getWindowSize(): { width: number, height: number } {
|
||||
if (!this.platformSDK) return {width: 720, height: 1280};
|
||||
return this.platformSDK.getWindowSize();
|
||||
}
|
||||
|
||||
/** --------------------------------------------------------- 分析统计 --------------------------------------------------------- */
|
||||
|
||||
// 判断统计功能是否可用
|
||||
public isAnalyticsEnabled(): boolean {
|
||||
if (!this.analyticsSDK) return false;
|
||||
return this.analyticsSDK.isAnalyticsEnabled();
|
||||
}
|
||||
|
||||
// 上报事件
|
||||
public trackEvent(eventName: string, value?: any): void {
|
||||
if (!this.analyticsSDK) return;
|
||||
return this.analyticsSDK.trackEvent(eventName, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user