// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html import Utils from "../tools/Utils"; const { ccclass, property } = cc._decorator; @ccclass export default class NavtiveData extends cc.Component { private _utils: Utils; private _nativeCurrentAd = null; private _nativeAd = null private _ready = false; constructor() { super(); this._utils = Utils.instance; } start() { } private enterGmae = false; /** * 创建广告 */ public initAd(config, callbacks?) { this._utils.log("------创建native广告-----"); let posId = config.insertAdCode; this._nativeAd = qg.createNativeAd({ posId: posId, }); this.enterGmae = false; let self = this; if (this._nativeAd) { this._nativeAd.load(); this._utils.log("------创建native广告-this.nativeAd.load(): " + this._nativeAd); this._nativeAd.onLoad(function (res) { self.enterGmae = true; self._utils.log('原生广告加载完成-onload触发 ' + JSON.stringify(res)); if (res && res.adList) { self._nativeCurrentAd = res.adList.pop(); self._ready = true; if (callbacks) { callbacks(true); } } }) this._nativeAd.onError(err => { self._utils.log("原生广告加载异常" + JSON.stringify(err)); self.enterGmae = true; if (err) { if (err.errCode == 30009) { if (callbacks) { callbacks(false); } } else { if (callbacks) { callbacks(false); } } } }); } else { self.enterGmae = true; if (callbacks) { callbacks(false); } } //5秒没反应,直接进入游戏 this.scheduleOnce(() => { if (self.enterGmae) { return; } if (callbacks) { callbacks(false); } }, 5); } /** * 广告展示上报 */ public reportAdShow() { if (this._nativeAd && this._nativeCurrentAd) { this._nativeAd.reportAdShow({ adId: this._nativeCurrentAd.adId.toString() }); } } /** * 广告点击上报 */ public reportAdClick() { if (this._nativeAd && this._nativeCurrentAd) { this._nativeAd.reportAdClick({ adId: this._nativeCurrentAd.adId.toString() }); } } /** * 获取广告数据 * @returns */ public getNativeDate() { return this._nativeCurrentAd; } public isReady() { return this._ready; } }