// 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 SDK, { NodeAction } from "../SDK"; import Utils from "../tools/Utils"; import VivoAd from "./VivoAd"; import NativeAdComponent, { NativeType } from "./NativeAdComponent"; const { ccclass, property } = cc._decorator; @ccclass export default class Banner extends cc.Component { private _bannerTemplate = null; //模板banner private _config; //banner数据 private _bannerCode; private _isBanner; private _bannerList: cc.Node[] = []; private _nativeBanner; private _bannerType; //初始化 public init(config) { this._config = config; this._isBanner = config.bannerAdEnable > 0; this._bannerCode = config.bannerAdCode; Utils.instance.log('创建banner广告') this.initBannerTemplate(); } //初始化模板banner private initBannerTemplate() { let self = this; var info = qg.getSystemInfoSync(); Utils.instance.log(info); var targetBannerAdWidth = info.screenWidth > info.screenHeight ? (info.screenWidth <= 720 ? info.screenWidth / 2 : info.screenWidth / 2 - 30) : info.screenWidth; Utils.instance.log("bannertargetBannerAdWidth : " + targetBannerAdWidth); this._bannerTemplate = qg.createBannerAd({ posId: self._bannerCode, style: { top: info.screenHeight - 170, // 置于屏幕底部 left: (info.screenWidth - targetBannerAdWidth) / 2, // 居中 } }) this._bannerTemplate.onSize(function (size) { Utils.instance.log('banner 宽度:' + size.width + ', banner 高度:' + size.height) }) this._bannerTemplate.onLoad(function () { Utils.instance.log('banner 广告加载成功') }) this._bannerTemplate.onError(function (err) { Utils.instance.log("banner 广告加载错误:" + JSON.stringify(err)) }) } //展示模板banner public showBannerTemplate() { if (this._isBanner) { if (this._bannerTemplate) { this._bannerTemplate.show(); } else { this.initBannerTemplate(); } } } //隐藏模板banner public hideBannerTemplate() { if (this._bannerTemplate) { this._bannerTemplate.hide(); } } /** * 展示原生banner * @param type 默认原生普通banner */ public showBanner(type: NativeType = NativeType.BANNER, top: boolean = false, y: number = 0, x: number = 0) { this._bannerType = type; Utils.instance.log("***********showNativeBanner type :" + type); if (type == NativeType.BANNER_NO_NATIVE) { this.showBannerTemplate(); return; } // this.showToast("banner显示了"); let self = this; if (this._config == null) { return; } if (this._config.masterEnable != 1) {//插屏是否打开 return; } if (this._config.bannerAdEnable != 1) { return; } let nativeData = VivoAd.Instance.getVivoNativeData(); if (nativeData == null || !nativeData.isReady()) { Utils.instance.log("************showNativeBanner 广告数据不存在"); return; } let clickCallBack = () => { VivoAd.Instance.creatorNativeData(); SDK.Instance.reportLog(NodeAction.NATIVE_CLICK, "原生Banner点击"); }; let showCallBack = () => { VivoAd.Instance.creatorNativeData(); //缓存下一条广告 SDK.Instance.reportLog(NodeAction.NATIVE_SHOW, "原生Banner展示"); }; let closeCallBack = () => { // this.getVivoNativeData(); self.cleanBanner(); SDK.Instance.reportLog(NodeAction.NATIVE_CLOSE, "原生Banner关闭"); }; let node = cc.director.getScene().children[0]; let banner = node.getChildByName("banner"); if (banner) { banner.removeFromParent(); banner.destroy(); } let winSize = cc.view.getVisibleSize(); if (y == 0) { if (top) { y = winSize.height / 2 - 150 / 2; } else { y = -winSize.height / 2 + 150 / 2; } } Utils.instance.log("-------bannerType: " + this._bannerType); let prefabsRes = "sdk/res/prefabs/NativeBannerAd"; if (type == NativeType.BANNER_BUTTON) { prefabsRes = "sdk/res/prefabs/NativeBannerAddButtonAd" if (top) { y = winSize.height / 2 - 400 / 2; } else { y = -winSize.height / 2 + 400 / 2; } } else if (type == NativeType.BANNER_LITTLE_BUTTON) { if (top) { y = winSize.height / 2 - 150 / 2; } else { y = -winSize.height / 2 + 150 / 2; } prefabsRes = "sdk/res/prefabs/NativeBannerLittleBUttonAd" } Utils.instance.loadPrefabs(prefabsRes, (prefabs) => { let adNode = cc.instantiate(prefabs); self._nativeBanner = adNode.addComponent(NativeAdComponent); self._nativeBanner.initData(nativeData); self._nativeBanner.listionCall(showCallBack, clickCallBack, closeCallBack); adNode.width = winSize.width; self._nativeBanner.showAd(type, self._config, x, y); node.addChild(adNode, 1000, "banner") self.cacheBanner(adNode); }); } private cacheBanner(node) { let self = this; self.cleanBanner(); if(self._bannerList){ self._bannerList.push(node); } } private cleanBanner() { if (this._bannerList) { if (this._bannerList.length > 0) { this._bannerList.forEach((node) => { node.removeFromParent(); node.destroy(); }); this._bannerList = []; } } } /** * 隐藏b原生banner */ public hideBanner(hide: boolean = true) { Utils.instance.log("------hideNativeBanner: " + hide); if (hide) { this._bannerType = null; } if (this._bannerType == NativeType.BANNER_NO_NATIVE) { this.hideBannerTemplate(); return; } if (this._nativeBanner) { this._nativeBanner.hide(true); this._nativeBanner = null; } } }