import { _decorator, Component, Enum, Vec2 } from 'cc'; import { platform } from './Data/platform'; const { ccclass, property } = _decorator; export enum ADType { None, Interstutal, FullScreen, Banner, Native } export enum ADShowType { Single, Both } export enum ADDeleyType { Const, Random } @ccclass("ADHelperDeleyData") export class ADHelperDeleyData { @property({ type: Enum(ADDeleyType), tooltip: "延迟类型:\nConst:固定时间\nRandom:随机时间" }) type: ADDeleyType = ADDeleyType.Const; @property({ tooltip: "延迟时间(常量)", visible() { return this.type == ADDeleyType.Const; } }) public deley_const: number = 0; @property({ tooltip: "延迟时间(随机值)", visible() { return this.type == ADDeleyType.Random; } }) public deley_random: Vec2 = new Vec2(); } @ccclass("ADHelperData") export class ADHelperData { @property({ type: Enum(ADType), tooltip: "广告类型:\nNone:不弹广告\nInterstutal:插屏广告\nFullScreen:全屏广告\nBanner:banner广告\nNative:原生信息流广告" }) type: ADType = ADType.None; @property({ tooltip: "广告权重" }) weight: number = 0; @property({ type: ADHelperDeleyData, tooltip: "广告延迟显示时间(秒)" }) deley: ADHelperDeleyData = new ADHelperDeleyData(); } @ccclass('ADHelper') export class ADHelper extends Component { @property({ type: Enum(ADShowType), tooltip: "广告显示方式:\nsingle:只显示单个广告\nboth:可同时显示多个广告" }) showType: ADShowType = ADShowType.Single; @property({ type: [ADHelperData], tooltip: "广告数据集合" }) ads: Array = []; @property({ tooltip: "是否立即显示广告" }) showOnLoad: boolean = true; private totleWeight = 0; start() { for (let i = 0; i < this.ads.length; i++) { this.totleWeight += this.ads[i].weight; } if (this.showOnLoad) { this.show(); } } public show(): void { this.doRandom(); } private doRandom(): void { if (this.showType == ADShowType.Single) { let random = this.random(0, this.totleWeight); let curWight = 0; for (let i = 0; i < this.ads.length; i++) { curWight += this.ads[i].weight; if (random <= curWight) { this.showAD(this.ads[i]); return; } } } else { for (let i = 0; i < this.ads.length; i++) { if (this.random(0, this.totleWeight) <= this.ads[i].weight) { this.showAD(this.ads[i]); } } } } private nativeADIsShow = false; private BannerADIsShow = false; private showAD(data: ADHelperData): void { let deley = 0; if (data.deley.type == ADDeleyType.Const) { deley = data.deley.deley_const; } else if (data.deley.type == ADDeleyType.Random) { deley = this.random(data.deley.deley_random.x, data.deley.deley_random.y); } let self = this; if (deley) { this.scheduleOnce(() => { self._showAD(data); }, deley); } else { self._showAD(data); } } private _showAD(data) { switch (data.type) { case ADType.None: break; case ADType.Native: this.nativeADIsShow = true; platform.showNativeAD(); break; case ADType.Interstutal: platform.showInterstutalAD(); break; case ADType.FullScreen: platform.showFullScreenAD(); break; case ADType.Banner: this.BannerADIsShow = true; platform.showBannerAD(); break; } } private random(min: number, max: number): number { return min + Math.random() * (max - min); } onDestroy() { if (this.nativeADIsShow) { platform.removeNativeAD(); } if (this.BannerADIsShow) { platform.removeBannerAD(); } } }