123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- import { _decorator, Component, director, Enum, Node } from 'cc';
- import { ch } from '../ch';
- import { loadType } from '../audio/audio';
- import { ch_sdk_comp } from './ch_sdk_comp';
- const { ccclass, property } = _decorator;
- /**包id*/
- export enum pack {
- id0 = 0,
- id1 = 1,
- id2 = 2,
- id3 = 3,
- id4 = 4,
- id5 = 5,
- id6 = 6,
- id7 = 7,
- id8 = 8,
- id9 = 9,
- }
- @ccclass('ch_start_pack')
- export class ch_start_pack extends Component {
- @property({ visible: false })
- private _gname: string[] = [];
- @property({ visible: false })
- private _gid: string[] = [];
- @property({ visible: false })
- private _is_local: boolean[] = [];
- @property({ visible: false })
- private _server: chsdk.serverType[] = [chsdk.serverType.dev];
- @property({ visible: false })
- private _report: chsdk.reportType[] = [chsdk.reportType.off];
- @property({ visible: false })
- private _log: chsdk.loglevel[] = [];
- @property({ visible: false })
- private _ttad: string[] = [];
- @property({ visible: false })
- private _ttiad: string[] = [];
- @property({ visible: false })
- private _tttmpid: string[] = [];
- @property({ visible: false })
- private _wxad: string[] = [];
- @property({ visible: false })
- private _wxiad: string[] = [];
- @property({ visible: false })
- private _wxtmpid: string[] = [];
- @property({ visible: false })
- private _serverIP: string[] = [];
- @property({ visible: false })
- private _pid: pack = pack.id0;
- @property({ type: Enum(pack), displayName: '包id', group: { name: '选包', id: 'pack', displayOrder: 0 } })
- get pid() { return this._pid; }
- set pid(val) {
- this._pid = val;
- for (let i = 0; i <= this._pid; i++) {
- this._gname[i] ??= '';
- this._is_local[i] ??= this._is_local[i - 1] ?? true;
- this._gid[i] ??= this._gid[i - 1] ?? '0';
- this._server[i] ??= this._server[i - 1] ?? chsdk.serverType.online;
- this._report[i] ??= this._report[i - 1] ?? chsdk.reportType.off;
- this._log[i] ??= this._log[i - 1] ?? chsdk.loglevel.DEBUG;
- this._ttad[i] ??= '';
- this._ttiad[i] ??= '';
- this._tttmpid[i] ??= '';
- this._wxad[i] ??= '';
- this._wxiad[i] ??= '';
- this._wxtmpid[i] ??= '';
- this._serverIP[i] ??= 'http://192.168.1.120:8787/v1';
- }
- }
- @property({ displayName: '游戏名', group: { name: '选包', id: 'pack', displayOrder: 0 } })
- get gname() { return this._gname[this._pid] ?? ''; }
- set gname(val) { this._gname[this._pid] = val; }
- @property({ displayName: '游戏id', group: { name: '选包', id: 'pack', displayOrder: 0 } })
- get gid() { return this._gid[this._pid]; }
- set gid(val) { this._gid[this._pid] = val; }
- //
- @property({ displayName: '是否单机', group: { name: 'ch.sdk模块', id: 'sdk', displayOrder: 0 } })
- get is_local() { return this._is_local[this._pid] }
- set is_local(val) { this._is_local[this._pid] = val; }
- //
- @property({ type: Enum(chsdk.serverType), displayName: '服务器地址', visible: function () { return !this.is_local; }, group: { name: 'ch.sdk模块', id: 'sdk', displayOrder: 0 }, tooltip: "local 局域网 \n dev 测试服 \n online 正式服" })
- get server() { return this._server[this._pid]; }
- set server(val) { this._server[this._pid] = val; }
- @property({ displayName: '本地服测试ip', visible: function () { return this.server == chsdk.serverType.test }, group: { name: 'ch.sdk模块', id: 'sdk', displayOrder: 0 } })
- get serverIP() { return this._serverIP[this._pid]; }
- set serverIP(val) { this._serverIP[this._pid] = val; }
- @property({ type: Enum(chsdk.reportType), displayName: '上报类型', group: { name: '基本参数', id: 'sdk', displayOrder: 0 }, tooltip: "off 不上报 \n ch 使用ch服务器(需要在ch后台配置事件) \n platform 使用微信/抖音平台(需要在平台后台设置事件)\nch__platflorm所有平台自定义事件都上报" })
- get report() { return this._report[this._pid]; }
- set report(val) { this._report[this._pid] = val; }
- @property({ type: Enum(chsdk.loglevel), displayName: '日志等级', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get log() { return this._log[this._pid]; }
- set log(val) { this._log[this._pid] = val; }
- //
- @property({ displayName: '抖音奖励广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get ttad() { return this._ttad[this._pid]; }
- set ttad(val) { this._ttad[this._pid] = val; }
- @property({ displayName: '抖音插屏广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get ttiad() { return this._ttiad[this._pid]; }
- set ttiad(val) { this._ttiad[this._pid] = val; }
- @property({ displayName: '抖音订阅id(,隔开)', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get tttmpid() { return this._tttmpid[this._pid]; }
- set tttmpid(val) { this._tttmpid[this._pid] = val; }
- @property({ displayName: '微信奖励广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get wxad() { return this._wxad[this._pid]; }
- set wxad(val) { this._wxad[this._pid] = val; }
- @property({ displayName: '微信插屏广告id', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get wxiad() { return this._wxiad[this._pid]; }
- set wxiad(val) { this._wxiad[this._pid] = val; }
- @property({ displayName: '微信订阅id(,隔开)', group: { name: '基本参数', id: 'sdk', displayOrder: 0 } })
- get wxtmpid() { return this._wxtmpid[this._pid]; }
- set wxtmpid(val) { this._wxtmpid[this._pid] = val; }
- //
- @property({ visible: false })
- private _use_ch_audio: boolean = false;
- @property({ type: Enum(loadType), visible: false })
- private _ch_audio_type: loadType = loadType.bundle;
- @property({ displayName: '是否使用ch.audio模块', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 } })
- get use_ch_audio() { return this._use_ch_audio; }
- set use_ch_audio(val) { this._use_ch_audio = val; }
- @property({ type: Enum(loadType), displayName: '音频资源加载模式', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 }, visible: function () { return this._use_ch_audio; } })
- get sount_load_type() { return this._ch_audio_type; }
- set sount_load_type(val) { this._ch_audio_type = val; }
- @property({ displayName: '音频资源默认bundle名', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.bundle } })
- private sound_bundle: string = 'res';
- @property({ displayName: '音频资源远程地址', group: { name: 'ch.audio模块', id: 'audio', displayOrder: 1 }, visible: function () { return this._use_ch_audio && this._ch_audio_type == loadType.remote } })
- private sound_url: string = '';
- protected onLoad(): void {
- console.log('包Id:' + this.pid, '游戏名:' + this.gname, '游戏Id:' + this.gid, '单机:' + this.is_local, '服务器:' + chsdk.serverType[this.server]);
- }
- /**初始化*/
- async init(): Promise<boolean> {
- chsdk.setConf(chsdk.pf.tt, { adUnitId: this.ttad, multiton: false, inster_unitId: this.ttiad, tmplIds: this.tttmpid ? this.tttmpid.split(',') : null });
- chsdk.setConf(chsdk.pf.wx, { adUnitId: this.wxad, multiton: false, inster_unitId: this.wxiad, tmplIds: this.wxtmpid ? this.wxtmpid.split(',') : null });
- let ret = await chsdk.init_inside(this.gid, this.log, this.server == chsdk.serverType.test ? this.serverIP : this.server, this.is_local, this.report);
- if (ret.code == chsdk.code.success) {
- if (this.use_ch_audio) {
- ch.audio.init(this.sount_load_type, this.sound_bundle, this.sound_url);
- }
- if (chsdk.get_pf() === chsdk.pf.web) {
- const sdk = new Node();
- sdk.name = '__ch_sdk__';
- sdk.addComponent(ch_sdk_comp);
- director.getScene().addChild(sdk);
- director.addPersistRootNode(sdk);
- }
- return true;
- } else {
- chsdk.log.error(ret.err);
- return false;
- }
- }
- /**初化始并在遇到问题时以单机模式进入
- * @param try_count 重试次数
- * @param try_interval 重试间隔时间(MS)
- */
- async try_init(try_count: number = 5, try_interval: number = 2000): Promise<void> {
- let ret = await this.init();
- if (ret) return;
- try_count--;
- if (try_count < 0) {
- this.is_local = true;
- await this.init();
- return;
- } else {
- await new Promise(resolve => setTimeout(resolve, try_interval));
- return this.try_init(try_count, try_interval);
- }
- }
- }
|