123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- import AdAgent from "./AdAgent";
- import { BannerLocation } from "./YZ_Constant";
- import PlatUtils from "./PlatUtils";
- import { utils } from "./Utils";
- const { ccclass, property } = cc._decorator;
- /**
- * uc广告组件
- */
- @ccclass
- export default class AdAgentCocosplay extends AdAgent {
- banner: any = null;
- interstitialAd: any = {};
- videoAd: any = {};
- bannerLoaded = false;
- interstitialAdLoaded = false;
- videoAdLoaded = false;
- _videoCallback: Function = null;
- _isVideoLoaded: boolean = false;
- _videoAd: any = null;
- _bannerAd: any = null;
- //@ts-ignore
- uc = window.uc;
- public Init() {
- utils.registerServerInitEvent(() => {
- this.initBanner();
- this.createInsertAd();
- this.createVideoAd();
- }, this)
- }
- initBanner() {
- let self = this;
- self.bannerLoaded = false;
- //@ts-ignore
- self.banner = AdSDK.createBannerAd("1", utils.config.cocosConfig.bannerId, 2);
- //注册onLoad函数,游戏调用创建banner时,AdSDK通知平台创建广告,当创建成功,会执行该回调函数通知游戏
- self.banner.onLoad(function () {
- self.bannerLoaded = true;
- console.log("banner 创建banner成功,可以调用展示");
- });
- //注册onError函数,游戏调用创建banner时,AdSDK通知平台创建广告,当创建失败,会执行该回调函数通知游戏
- self.banner.onError(function (param) {
- self.destroyBannerAd();
- console.log("banner 创建banner失败,错误码 = ", param.errorCode);
- });
- console.log('banner 游戏创建banner广告');
- }
- ShowBanner() {
- let self = this;
- // self.createBannerAd()
- // self.bannerLoaded = false;
- if (self.bannerLoaded) {//Object.keys(banner).length &&
- self.banner.show().then(function () {
- console.log('banner 广告显示成功')
- }, function (err) {
- console.log('banner 广告显示失败')
- });
- console.log('banner 展示banner广告');
- } else {
- console.log('banner 未加载成功');
- }
- }
- HideBanner() {
- this.banner.hide();
- console.log('banner 隐藏banner广告');
- }
- destroyBannerAd() {
- this.bannerLoaded = false;
- this.banner.destroy();
- this.banner = null;
- console.log('banner 销毁banner广告');
- }
- public get ServerConfig() {
- return utils._tool_Cocosplay.ServerConfig;
- }
- //插屏显示次数
- _insertShowCount: number = 0;
- _insertLastShowTime: number = 0;
- /**
- * 验证插屏是否能展示
- * 1、次数限制 默认每日8次
- * 2、时间限制 默认60秒
- */
- private checkInsertAdShow(): boolean {
- let maxShowCount = this.ServerConfig.intersititial_max_show_count;
- maxShowCount = 0;
- let intervalTime = this.ServerConfig.intersititial_interval_time;
- let curTime: number = new Date().getTime();
- let interval: number = (curTime - this._insertLastShowTime) / 1000;
- utils.showLog("cocos服务器插屏最大显示次数为:" + maxShowCount + ",间隔显示时间为:" + intervalTime + "秒!");
- utils.showLog("cocos插屏当前广告显示次数:" + this._insertShowCount + "次,间隔时间:" + interval + "秒!");
- if (maxShowCount > 0 && this._insertShowCount >= maxShowCount) {
- utils.showLog("cocos插屏广告显示的次数达到" + maxShowCount + "次。插屏不显示");
- return false;
- }
- if (intervalTime > 0 && interval < intervalTime) {
- utils.showLog("cocos插屏广告显示的间隔少于" + intervalTime + "秒。插屏不显示");
- return false;
- }
- return true;
- }
- // 创建插屏广告 展示一次调用一次创建
- // 参数
- // adId: string 广告序号ID 游戏自定义
- // interstitialAdId: string 插屏广告ID 需后台申请
- // style: int 广告类型 1 全屏 2 半屏
- public ShowInterstitial() {
- let self = this
- if (!self.checkInsertAdShow()) return;
- if (self.interstitialAdLoaded) {
- self.interstitialAd.show().then(function () {
- self._insertLastShowTime = new Date().getTime();
- self.destroyInterstitialAd();
- self.createInsertAd();
- }, function (err) {
- console.log("interstitialAd 广告显示失败")
- })
- console.log("interstitialAd 游戏展示插屏广告");
- }
- }
- public createInsertAd() {
- let self = this
- utils.showLog("插屏id为:" + utils.config.cocosConfig.insertId)
- //@ts-ignore
- this.interstitialAd = AdSDK.createInterstitialAd("1", utils.config.cocosConfig.insertId, 2);
- this.interstitialAd.onLoad(function () {
- self.interstitialAdLoaded = true;
- console.log("interstitialAd 创建插屏成功,可以调用展示");
- });
- self.interstitialAd.onError(function (param) {
- console.log("interstitialAd 创建插屏失败,错误码 = ", param.errorCode);
- self.destroyInterstitialAd();
- })
- }
- hideInterstitialAd() {
- this.interstitialAd.hide();
- console.log("interstitialAd 游戏隐藏插屏广告");
- }
- destroyInterstitialAd() {
- this.interstitialAdLoaded = false;
- this.interstitialAd.destroy();
- console.log("interstitialAd 游戏销毁插屏广告");
- }
- // 创建激励视频广告 展示一次创建一次
- // 参数
- // adId: string 广告序号ID 游戏自定义
- // videoAdId: string 视频广告ID 需后台申请
- // screenOrientation: int 广告类型 1 横屏 2 竖屏
- public ShowVideo(callback: Function) {
- let self = this;
- self._videoCallback = callback;
- if (self.videoAdLoaded) {
- self.videoAd.show().then(function () {
- if (self._videoCallback) {
- self._videoCallback(true, "视频播放成功");
- self._videoCallback = null;
- }
- }, function (err) {
- if (self._videoCallback) {
- self._videoCallback(false, "暂无视频广告");
- self._videoCallback = null;
- }
- })
- } else {
- if (self._videoCallback) {
- self._videoCallback(false, "暂无视频广告");
- self._videoCallback = null;
- }
- self.createVideoAd();
- }
- }
- createVideoAd() {
- let self = this;
- console.log("视频id为:" + utils.config.cocosConfig.videoId)
- //@ts-ignore
- self.videoAd = AdSDK.createRewardedVideoAd("1", utils.config.cocosConfig.videoId, 2);
- self.videoAd.onLoad(function () {
- self.videoAdLoaded = true;
- console.log("rewardedvideoAd 创建视频广告成功,可以调用展示")
- });
- self.videoAd.onError(function (param) {
- console.log("rewardedvideoAd 创建视频广告失败,错误码 = ", param.errorCode);
- self.destroyRewardedVideoAd();//加载广告失败,销毁
- if (self._videoCallback) {
- self._videoCallback(false, "暂无视频广告");
- self._videoCallback = null;
- }
- });
- self.videoAd.onClose(function () {
- if (self._videoCallback) {
- self._videoCallback(false, "观看完视频才能获得奖励!");
- self._videoCallback = null;
- }
- self.destroyRewardedVideoAd();//视频广告关闭,销毁
- self.createVideoAd();
- });
- }
- destroyRewardedVideoAd() {
- this.videoAdLoaded = false;
- this.videoAd.destroy();
- console.log("rewardedvideoAd 游戏销毁视频广告");
- }
- }
|