123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- // 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 NativeAdComponent, { NativeType } from "./NativeAdComponent";
- import SDK, { NodeAction } from "../SDK";
- import Utils from "../tools/Utils";
- import VivoAd from "./VivoAd";
- const { ccclass, property } = cc._decorator;
- export enum InterstitialType {
- INTERSTITIAL_GAME_OVER = 0, //游戏结束
- INTERSTITIAL_GAME_STARE, //游戏开始
- INTERSTITIAL_GAME_PAUSE //游戏暂停
- }
- @ccclass
- export default class Interstitial extends cc.Component {
- private _config;
- private _insetList : cc.Node[]=[];
- public init(config){
- this._config = config;
- Utils.instance.log('创建Interstitial广告')
- }
- private showBanner(banner) {
- // "banner":{"type":0, "x":0,"y":0,"top":false }
- if (banner) {//存在插屏节点banner数据
- if (banner.enable != 1) {
- return;
- }
- let type = banner.type;//显示banner类型
- let bannerType = NativeType.BANNER;//正常banner
- if (type == 1) {//大banner
- bannerType = NativeType.BANNER_BUTTON;
- } else if (type == 2) {//小banner带详情按键
- bannerType = NativeType.BANNER_LITTLE_BUTTON;
- }
- VivoAd.Instance.getBanner().showBanner(bannerType, banner.top, banner.x, banner.y);
- }
- }
- /**
- * 展示原生插屏
- * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
- * @param id 默认0
- * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false
- */
- public showInterstitial(type: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) {
- let self = this;
- //显示banner
- //广告数据是否存在
- if (this._config == null) {
- return;
- }
- let adNodeList = JSON.parse(this._config.insertAdNodeList);
- if (this._config.masterEnable != 1) {//所有广告是否打开
- return;
- }
- if (adNodeList == null || adNodeList.length <= 0) {
- Utils.instance.log("---------showInterstitial 未配置广告节点adNodeList为空");
- return;
- }
- //当前节点是否打开
- let adNode = adNodeList[type];
- if (adNode == null) {
- Utils.instance.log("---------showInterstitial 节点未配置广告nodeId:" + type);
- // isforever = false;
- return;
- }
- //插屏关闭,banner正常展示
- if (this._config.insertAdEnable != 1) {//插屏是否打开
- this.showBanner(adNode.banner);
- return;
- }
- //插屏节点关闭,banner正常展示
- if (adNode && adNode.enable != 1) {
- // isforever = false;
- this.showBanner(adNode.banner);
- return;
- }
- let nativeDate = VivoAd.Instance.getVivoNativeData();
- if (nativeDate == null || !nativeDate.isReady()) {
- Utils.instance.log("---------showInterstitial 广告数据不存在-----");
- this.showBanner(adNode.banner);
- return;
- }
- let node = cc.director.getScene().children[0];
- let insertAd = node.getChildByName("insertAd");
- if (insertAd) {
- insertAd.removeFromParent();
- // return ;
- }
- let go = () => {
- self.show(node, nativeDate, adNode);
- }
- this.delayShow(go);
- }
- private show(node, nativeDate, adDate) {
- let self = this;
- let clickCallBack = () => {
- VivoAd.Instance.creatorNativeData();
- SDK.Instance.reportLog(NodeAction.NATIVE_CLICK, "原生插屏点击");
- };
- let showCallBack = () => {
- //缓存下一条广告
- VivoAd.Instance.hideBanner();
- VivoAd.Instance.creatorNativeData();
- SDK.Instance.reportLog(NodeAction.NATIVE_SHOW, "原生插屏展示");
- };
- let closeCallBack = () => {
- if (adDate) {//关闭插屏后,展示插屏节点内banner
- if (adDate.show_banner && adDate.show_banner > 0) {
- // self.showNativeBanner(self.bannerTop, self.bannerType, self.banner_y, self.banner_x);
- this.showBanner(adDate.banner);
- }
- }
- //清理所有展示插屏
- self.cleanInset();
- SDK.Instance.reportLog(NodeAction.NATIVE_CLOSE, "原生插屏关闭");
- };
- Utils.instance.loadPrefabs("sdk/res/prefabs/NativeInsertAd", (prefabs) => {
- let adNode = cc.instantiate(prefabs);
- let nativeAd = adNode.addComponent(NativeAdComponent);
- nativeAd.initData(nativeDate);
- nativeAd.listionCall(showCallBack, clickCallBack, closeCallBack);
- // adNode.active = false;
- nativeAd.showAd(NativeType.INSET, self._config);
- node.addChild(adNode, 1000, "insert")//加节点名,防止重复加广告
- self.cacheInset(adNode);
- });
- }
- private cleanInset(){
- if (this._insetList&&this._insetList.length>0) {
- this._insetList.forEach((node) => {
- if(node){
- node.removeFromParent();
- node.destroy();
- }
- })
- this._insetList = [];
- }
- }
- private cacheInset(adNode) {
- this.cleanInset();
- if(this._insetList){
- this._insetList.push(adNode);
- }
-
- }
- private delayShow(go) {
- let self = this;
- //插屏延时展示
- if (self._config) {
- let dayTime = self._config.insertAdMaxDayTimes;
- Utils.instance.log("-------delayTime: " + dayTime);
- let mistakeRate = this._config.insertAdDayTimesMistakeRate;
- let random = Utils.instance.getRandomInt(1, 100);
- Utils.instance.log("-------delayTime random: " + random + " limitClick: " + SDK.Instance.limitClick);
- if (SDK.Instance.limitClick >= this._config.maxClickCount || mistakeRate < random) {
- dayTime = 0;
- }
- Utils.instance.log("-------delayTime: " + dayTime);
- self.scheduleOnce(() => {
- go();
- }, dayTime);
- }
- }
- }
|