// 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 VivioNavtiveDate from "./NavtiveData"; import SDK, { NodeAction } from "../SDK"; import Utils from "../tools/Utils"; import VivoAd from "./VivoAd"; const { ccclass, property } = cc._decorator; export enum NativeType { INSET,//插屏 BANNER,//横幅 ICON,//图标 SPLASH,//Splash BANNER_BUTTON, BANNER_NO_NATIVE, BANNER_LITTLE_BUTTON, ICON_GAME, INSET_NO_NATIVE } /**原生广告 */ @ccclass export default class NativeAdComponent extends cc.Component { @property(cc.Boolean) iconShow: boolean = false; private _closeButton: cc.Node = null;//关闭按钮 private _content: cc.Sprite = null;//内容按钮 private _type = NativeType.INSET;//默认插屏 private _nativeCurrentAd = null;//广告内容 private _config = null;//广告配置 private _adCallBack = null;//banner和icon刷新回调 private _clickCallBack = null;//点击回调 private _showCallBack = null;//展示回调 private _closeCallBack = null;//关闭回调 private _title: cc.Label = null;//广告标题 private _info: cc.Label = null;//广告描述 private _isClick = false;//防止重复点击 private _splashCallBacks = null;//开屏回调 private _labelCall = null;//开屏倒时候任务 private _vivoNativeDate: VivioNavtiveDate = null; start() { Utils.instance.log("-----------------iconShow: " + this.iconShow); if (this.iconShow) { let shareVivoNativeDate = VivoAd.Instance.getVivoNativeData(); if (shareVivoNativeDate == null || !shareVivoNativeDate.isReady()) { this.node.active = false; return; } this.initData(shareVivoNativeDate); let clickCallBack = () => { VivoAd.Instance.creatorNativeData() SDK.Instance.reportLog(NodeAction.NATIVE_CLICK, "原生icon点击"); }; let showCallBack = () => { //缓存下一条广告 SDK.Instance.reportLog(NodeAction.NATIVE_SHOW, "原生icon展示"); }; let closeCallBack = () => { SDK.Instance.reportLog(NodeAction.NATIVE_CLOSE, "原生icon关闭"); }; this.listionCall(showCallBack, clickCallBack, closeCallBack); this.showAd(NativeType.ICON_GAME, VivoAd.Instance.getConfig()); this.node.active = true; } } initButton() { let curContent = this.node.getChildByName("content"); if(curContent) { this._content = curContent.getChildByName("contentAd").getComponent(cc.Sprite); let button = curContent.getChildByName("Button"); if (button&&button.active) { //广告详情按键默认关闭 button.on(cc.Node.EventType.TOUCH_START, this.adClick, this); if (this._type == NativeType.BANNER_BUTTON || this._type == NativeType.BANNER_LITTLE_BUTTON) { button.active = false; if(this._config.insertAdNodeList){//存在插屏列表 let adNodeList = JSON.parse(this._config.insertAdNodeList); //黑夜首节点是否存在 let adNode = adNodeList[0]; if (adNode) { //广告详情是否打开 if (adNode.bntDetails && adNode.bntDetails > 0) { button.active = true; } } } button.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.1), cc.scaleTo(0.5, 1)))); } } curContent.on(cc.Node.EventType.TOUCH_START, this.adClick, this); } } onDisable() { if (this._adCallBack) { this.unschedule(this._adCallBack); } } public initData(vivoNativeDate: VivioNavtiveDate) { this._vivoNativeDate = vivoNativeDate; } private init() { this.node.active = true; let self = this; // let nativeData = { // imgUrlList: "https://pic.cr173.com/up/2019-9/201991783518653750.jpg", // icon: "https://p.e5n.com/up/2019-9/2019917834498180.jpg", // title: "超强马丽", // desc: "这个游戏很不错", // clickBtnTxt: "立即查看" // } this._nativeCurrentAd = this._vivoNativeDate.getNativeDate(); if (this._nativeCurrentAd == null) { console.log("------没有广告数据"); VivoAd.Instance.creatorNativeData((success) => { if (success) { this._vivoNativeDate = VivoAd.Instance.getVivoNativeData(); self.init(); } }); return; } let curContent = this.node.getChildByName("content"); if(curContent){ let adId = curContent.getChildByName("adId"); if (adId) { adId.active = false; adId.y = -50; adId.x = -10; adId.color = new cc.Color(255, 0, 0); let adIdLable = adId.getComponent(cc.Label); adIdLable.string = this._nativeCurrentAd.adId.toString(); } if (this._type == NativeType.SPLASH) { let closeContinu = curContent.getChildByName("closeContinu"); closeContinu.on(cc.Node.EventType.TOUCH_START, this.close, this); let label = closeContinu.getChildByName("cLabel").getComponent(cc.Label); Utils.instance.log("----------splash label: " + label); if (label) { let count = 3; self._labelCall = () => { count--; if (count <= 0) { Utils.instance.log("----------开屏关闭: " + label); self.hide(); } label.string = count + "秒 跳过" Utils.instance.log("----------splash label: " + label.string); } self.schedule(self._labelCall, 3) } } } let url = this._nativeCurrentAd.imgUrlList[0]; //加载插屏图片 if (this._type != NativeType.ICON && this._type != NativeType.BANNER_BUTTON && this._type != NativeType.ICON_GAME) { if(curContent){ let title = curContent.getChildByName("title"); if(title&&title.active){ self._title = title.getComponent(cc.Label); self._title.string = self._nativeCurrentAd.title; } let info = curContent.getChildByName("info"); if( info&&info.active){ self._info = info.getComponent(cc.Label); self._info.string = self._nativeCurrentAd.desc; } url = this._nativeCurrentAd.icon; if (this._type != NativeType.BANNER && this._type != NativeType.BANNER_LITTLE_BUTTON) { let icon_ = curContent.getChildByName("icon"); if( icon_&&icon_.active){ let icon = icon_.getComponent(cc.Sprite); this.getImageSpriteFrame(url).then((frame: cc.SpriteFrame) => { icon.spriteFrame = frame; }).catch((err) => { console.log(err); self.getImageSpriteFrame(url).then((frame: cc.SpriteFrame) => { icon.spriteFrame = frame; }); }); } } } } if (this._type == NativeType.ICON) { url = this._nativeCurrentAd.icon; } else { url = this._nativeCurrentAd.imgUrlList[0]; } this.getImageSpriteFrame(url).then((frame: cc.SpriteFrame) => { self._content.spriteFrame = frame; }).catch((err) => { console.log(err); url = this._nativeCurrentAd.imgUrlList[0]; self.getImageSpriteFrame(url).then((frame: cc.SpriteFrame) => { self._content.spriteFrame = frame; }).catch((err) => { console.log(err); self.getImageSpriteFrame(url).then((frame: cc.SpriteFrame) => { self._content.spriteFrame = frame; }); }); }); } /**广告状态监听 */ public listionCall(showCallBack, clickCallBack, closeCallBack) { this._showCallBack = showCallBack; this._clickCallBack = clickCallBack; this._closeCallBack = closeCallBack; } private closeSize() { if (this._closeButton == null) { return; } if (this._config && this._closeButton) {//控制关闭按键大小 let adNodeList = JSON.parse(this._config.insertAdNodeList); if (adNodeList) { let adNode = adNodeList[0]; if (adNode) { let size = adNode.size; if (size) { if (size.enable) {//关闭按键大小开关 let width = 20; let height = 20; if (size.width == null || size.width == 0) { width = 35; } else if (size.width < 20) { width = 20; } else { width = size.width; } if (size.height == null || size.height == 0) { height = 35; } else if (size.height < 20) { height = 20; } else { height = size.height; } this._closeButton.width = width; this._closeButton.height = height; } } } } } } /**展示广告 * @param */ public showAd(type: NativeType, config: any, x?: number, y?: number, callBacks?) { this._type = type; this._config = config; this.initButton(); let curContent = this.node.getChildByName("content"); if(curContent) { this._closeButton = curContent.getChildByName("close"); this.closeSize(); // this.content.node.on(cc.Node.EventType.TOUCH_START, this.adClick, this); this._closeButton.on(cc.Node.EventType.TOUCH_START, this.close, this); this.node.active = false; this._splashCallBacks = callBacks; this.show(); let adFlushTime = this._config.iconAdFlush; let self = this; if (type == NativeType.BANNER_BUTTON || this._type == NativeType.BANNER_LITTLE_BUTTON) { let bigBannerCloseEnable = this._config.bigBannerCloseEnable; if (bigBannerCloseEnable <= 0) { Utils.instance.log("-----------大banner----------"); this._closeButton.active = false; } } //不是插屏时,要刷新广告 if (type == NativeType.BANNER || type == NativeType.ICON || type == NativeType.BANNER_BUTTON || type == NativeType.ICON_GAME || type == NativeType.BANNER_LITTLE_BUTTON) { this.node.x = x; this.node.y = y; if (adFlushTime <= 0) { Utils.instance.log("-----------不刷新广告----------"); return; } this._adCallBack = () => {//刷新广告 Utils.instance.log("-----------广告刷新----------"); let curContent = this.node.getChildByName("content"); if (curContent) { curContent.active = true; } self._vivoNativeDate = VivoAd.Instance.getVivoNativeData(); self.show(); } this.schedule(this._adCallBack, adFlushTime); } } } private show() { let self = this; this._isClick = false; // if(this.node.active == false){ // this.node.active = true; // } if (self._vivoNativeDate) { self.init(); self._vivoNativeDate.reportAdShow(); if (this._showCallBack) { this._showCallBack(); } } } onDestroy() { Utils.instance.log("-------广告 destroy " + this._type); } /** * 隐藏广告 */ public hide(remove: boolean = true) { if (this.node) { if (remove) { this.unscheduleAllCallbacks(); this.node.removeFromParent(); } else { if (this._type != NativeType.ICON_GAME) { let curContent = this.node.getChildByName("content"); if (curContent) { curContent.active = false; } } else { this.node.active = false; } } } if (this._closeCallBack) { this._closeCallBack(); } if (this._type == NativeType.SPLASH) { if (this._splashCallBacks) { this._splashCallBacks(); } this.unschedule(this._labelCall); } } //全屏点击 public allSceen() { if (this._type == NativeType.INSET) { return; } this.adClick(); // } //点击事件 public adClick() { if (this._isClick) { return; } this._isClick = true; let self = this; if (self._vivoNativeDate) { self._vivoNativeDate.reportAdClick(); if (this._clickCallBack) { this._clickCallBack(); } } //如果是插屏或开屏,点击直接关闭 if (this._type == NativeType.INSET || this._type == NativeType.SPLASH) { this.hide(true); } else {//banner和icon则刷新广告 this._vivoNativeDate = VivoAd.Instance.getVivoNativeData(); if (this._vivoNativeDate != null && this._vivoNativeDate.isReady()) { this.show(); } else { this.hide(false); } } } /** * * @returns 关闭广告 */ public close() { let mistakeRate = this._config.insertAdMistakeRate; this._isClick = true; let random = Utils.instance.getRandomInt(1, 100); //误次数已用完,直接关闭 if (SDK.Instance.limitClick >= this._config.maxClickCount) { console.log("=============数已用完,直接关闭"); this.hide(); return; } // mistakeRate = 0; //满足误点 if (random < mistakeRate) {//关击按键是事能点击广告 SDK.Instance.limitClick++; console.log("------------random: ", random < mistakeRate); console.log("。。。。。。原生广告close adClick。。。。。。"); this.adClick(); return; } if (this._type == NativeType.INSET || this._type == NativeType.SPLASH) { this.hide(); } else { this.hide(false); } } /** * 获取内容图片 * @returns */ public getImageSpriteFrame(url) { let self = this; return new Promise((resolve, reject) => { console.log("图片下载地址:", url); cc.loader.load(url, function (err, texture) { if (err) { console.log("图片下载失败", url, err); reject("图片下载失败: " + url); return; } let frame = new cc.SpriteFrame(texture); resolve(frame); }); }); } }