| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- require("./AnyThinkAds/ATRewardedVideoJSSDK");
- require("./AnyThinkAds/ATJSSDK");
- require("./AnyThinkAds/ATInterstitialJSSDK");
- require("./AnyThinkAds/ATNativeJSSDK");
- var ATNativeJS = cc.Class({
- name: "ATNativeJS",
- /** 代理脚本(TS) */
- proxy: null,
- frameWidth: 0,
- frameHeight: 0,
- nativeIsShow: false,
- nativeType: 0,
- nativeShow: false,
- nativeIsLoad: false,
- nativeIsLoading: false,
- // timeID: 0,
- ctor: function () {
- // console.log("ATNativeJS created! ");
- },
- init: function () {
- ATNativeJSSDK.setAdListener(this);
- },
- /** 广告位Id */
- placementID: function () {
- return this.proxy.placementID();
- },
- /**
- * 加载原生广告
- * @param {*} type 类型
- * @param {*} show 是否显示
- */
- loadNative: function (type = 0, show = false) {
- this.nativeType = type;
- this.nativeShow = show;
- this.nativeIsLoading = true;
- //加载原生广告时需要传入广告展示的宽高
- var frameSize = cc.view.getFrameSize();
- this.frameWidth = frameSize.width;
- this.frameHeight = frameSize.height;
- var per = this.frameWidth / 750
- var sw = per * 656
- var sh = per * 440
- if (this.nativeType == 2) {
- this.destroyNative(false)
- sw *= 0.9
- sh *= 0.8
- }
- else if (this.nativeType == 3) {
- this.destroyNative(false)
- sw *= 0.9
- sh *= 0.75
- }
- else {
- sw *= 0.9
- sh *= 0.75
- }
- ATNativeJSSDK.loadNative(this.placementID(), ATNativeJSSDK.createLoadAdSize(sw, sh));
- },
- /**
- * 销毁原生广告
- * @param {*} ifPreloadNext 是否预加载下一个
- */
- destroyNative: function (ifPreloadNext = true) {
- ATNativeJSSDK.removeAd(this.placementID());
- this.nativeIsLoad = false
- this.nativeIsShow = false
- if (ifPreloadNext) {
- this.loadNative()
- }
- },
- /** 信息流位置
- * @param {*} type 0 弹窗里 1 最底部 2 新车奖励 3 底部小窗口
- */
- showNative: function (type) {
- if (this.nativeIsLoading) {
- this.nativeType = type
- this.nativeShow = true
- return
- }
- if (!this.nativeIsLoad) {
- this.loadNative(type, true)
- return
- }
- if (this.nativeIsShow) {
- return
- }
- this.nativeIsShow = true
- var per = this.frameWidth / 750
- var sw = per * 656
- var sh = per * 440
- var fontSize = 12
- var nativeAdViewProperty = new ATNativeJSSDK.AdViewProperty();
- var posy = 0
- if (type == 0) {
- // posy = this.frameHeight * 0.5 - 100
- posy = this.frameHeight * 0.45
- }
- else if (type == 1) {
- // posy = this.frameHeight * 0.5 + 280
- posy = this.frameHeight * 0.625
- }
- else if (type == 2) {
- // posy = this.frameHeight * 0.5 + 210
- posy = this.frameHeight * 0.595
- sw *= 0.9
- sh *= 0.8
- fontSize = 8
- }
- else if (type == 3) {
- // posy = this.frameHeight * 0.5 + 420
- posy = this.frameHeight * 0.69
- sw *= 0.9
- sh *= 0.75
- fontSize = 8
- }
- else if (type == 4) {
- posy = this.frameHeight * 0.75;
- sw *= 1.1;
- }
- var parx = (this.frameWidth - sw) * 0.5
- nativeAdViewProperty.parent = nativeAdViewProperty.createItemViewProperty(parx, posy, sw, sh, "#ffffff", "", 0);
- nativeAdViewProperty.mainImage = nativeAdViewProperty.createItemViewProperty(0, 0, sw, sh, "#ffffff", "#ffffff", fontSize);
- ATNativeJSSDK.showAd(this.placementID(), nativeAdViewProperty);
- },
- /**
- * 原生广告加载成功
- * @param {*} placementId
- */
- onNativeAdLoaded: function (placementId) {
- this.nativeIsLoad = true
- this.nativeIsLoading = false
- if (this.nativeShow) {
- this.nativeIsShow = false
- this.showNative(this.nativeType)
- }
- },
- /**
- * 原生广告加载失败
- * @param {*} placementId 广告位id
- * @param {*} errorInfo 错误信息
- */
- onNativeAdLoadFail: function (placementId, errorInfo) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdLoadFail(" + placementId + ", " + errorInfo + ")");
- this.nativeIsLoading = false
- this.nativeIsLoad = false
- },
- /**
- * 原生广告显示成功
- * @param {*} placementId 广告位id
- * @param {*} callbackInfo 回调信息
- * @returns
- */
- onNativeAdShow: function (placementId, callbackInfo) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdShow(" + placementId + ", " + callbackInfo + ")");
- if (this.proxy == null) return;
- this.proxy.onNativeAdShow(placementId, callbackInfo);
- },
- /**
- * 原生广告被点击
- * @param {*} placementId 广告位Id
- * @param {*} callbackInfo 回调信息
- */
- onNativeAdClick: function (placementId, callbackInfo) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdClick(" + placementId + ", " + callbackInfo + ")");
- },
- /**
- * 原生广告视频开始播放,部分广告平台有此回调
- * @param {*} placementId 广告位id
- */
- onNativeAdVideoStart: function (placementId) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdVideoStart(" + placementId + ")");
- },
- /**
- * 原生广告视频结束播放,部分广告平台有此回调
- * @param {*} placementId
- */
- onNativeAdVideoEnd: function (placementId) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdVideoEnd(" + placementId + ")");
- },
- /**
- * 广告关闭按钮被点击,部分广告平台有此回调
- * @param {*} placementId
- * @param {*} callbackInfo
- */
- onNativeAdCloseButtonTapped: function (placementId, callbackInfo) {
- // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdCloseButtonTapped(" + placementId + ", " + callbackInfo + ")");
- }
- });
- module.exports = ATNativeJS;
- if (!cc["ATNativeJS"]) {
- cc["ATNativeJS"] = new ATNativeJS();
- }
|