ATNativeJS.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. require("./AnyThinkAds/ATRewardedVideoJSSDK");
  2. require("./AnyThinkAds/ATJSSDK");
  3. require("./AnyThinkAds/ATInterstitialJSSDK");
  4. require("./AnyThinkAds/ATNativeJSSDK");
  5. var ATNativeJS = cc.Class({
  6. name: "ATNativeJS",
  7. /** 代理脚本(TS) */
  8. proxy: null,
  9. frameWidth: 0,
  10. frameHeight: 0,
  11. nativeIsShow: false,
  12. nativeType: 0,
  13. nativeShow: false,
  14. nativeIsLoad: false,
  15. nativeIsLoading: false,
  16. // timeID: 0,
  17. ctor: function () {
  18. // console.log("ATNativeJS created! ");
  19. },
  20. init: function () {
  21. ATNativeJSSDK.setAdListener(this);
  22. },
  23. /** 广告位Id */
  24. placementID: function () {
  25. return this.proxy.placementID();
  26. },
  27. /**
  28. * 加载原生广告
  29. * @param {*} type 类型
  30. * @param {*} show 是否显示
  31. */
  32. loadNative: function (type = 0, show = false) {
  33. this.nativeType = type;
  34. this.nativeShow = show;
  35. this.nativeIsLoading = true;
  36. //加载原生广告时需要传入广告展示的宽高
  37. var frameSize = cc.view.getFrameSize();
  38. this.frameWidth = frameSize.width;
  39. this.frameHeight = frameSize.height;
  40. var per = this.frameWidth / 750
  41. var sw = per * 656
  42. var sh = per * 440
  43. if (this.nativeType == 2) {
  44. this.destroyNative(false)
  45. sw *= 0.9
  46. sh *= 0.8
  47. }
  48. else if (this.nativeType == 3) {
  49. this.destroyNative(false)
  50. sw *= 0.9
  51. sh *= 0.75
  52. }
  53. else {
  54. sw *= 0.9
  55. sh *= 0.75
  56. }
  57. ATNativeJSSDK.loadNative(this.placementID(), ATNativeJSSDK.createLoadAdSize(sw, sh));
  58. },
  59. /**
  60. * 销毁原生广告
  61. * @param {*} ifPreloadNext 是否预加载下一个
  62. */
  63. destroyNative: function (ifPreloadNext = true) {
  64. ATNativeJSSDK.removeAd(this.placementID());
  65. this.nativeIsLoad = false
  66. this.nativeIsShow = false
  67. if (ifPreloadNext) {
  68. this.loadNative()
  69. }
  70. },
  71. /** 信息流位置
  72. * @param {*} type 0 弹窗里 1 最底部 2 新车奖励 3 底部小窗口
  73. */
  74. showNative: function (type) {
  75. if (this.nativeIsLoading) {
  76. this.nativeType = type
  77. this.nativeShow = true
  78. return
  79. }
  80. if (!this.nativeIsLoad) {
  81. this.loadNative(type, true)
  82. return
  83. }
  84. if (this.nativeIsShow) {
  85. return
  86. }
  87. this.nativeIsShow = true
  88. var per = this.frameWidth / 750
  89. var sw = per * 656
  90. var sh = per * 440
  91. var fontSize = 12
  92. var nativeAdViewProperty = new ATNativeJSSDK.AdViewProperty();
  93. var posy = 0
  94. if (type == 0) {
  95. // posy = this.frameHeight * 0.5 - 100
  96. posy = this.frameHeight * 0.45
  97. }
  98. else if (type == 1) {
  99. // posy = this.frameHeight * 0.5 + 280
  100. posy = this.frameHeight * 0.625
  101. }
  102. else if (type == 2) {
  103. // posy = this.frameHeight * 0.5 + 210
  104. posy = this.frameHeight * 0.595
  105. sw *= 0.9
  106. sh *= 0.8
  107. fontSize = 8
  108. }
  109. else if (type == 3) {
  110. // posy = this.frameHeight * 0.5 + 420
  111. posy = this.frameHeight * 0.69
  112. sw *= 0.9
  113. sh *= 0.75
  114. fontSize = 8
  115. }
  116. else if (type == 4) {
  117. posy = this.frameHeight * 0.75;
  118. sw *= 1.1;
  119. }
  120. var parx = (this.frameWidth - sw) * 0.5
  121. nativeAdViewProperty.parent = nativeAdViewProperty.createItemViewProperty(parx, posy, sw, sh, "#ffffff", "", 0);
  122. nativeAdViewProperty.mainImage = nativeAdViewProperty.createItemViewProperty(0, 0, sw, sh, "#ffffff", "#ffffff", fontSize);
  123. ATNativeJSSDK.showAd(this.placementID(), nativeAdViewProperty);
  124. },
  125. /**
  126. * 原生广告加载成功
  127. * @param {*} placementId
  128. */
  129. onNativeAdLoaded: function (placementId) {
  130. this.nativeIsLoad = true
  131. this.nativeIsLoading = false
  132. if (this.nativeShow) {
  133. this.nativeIsShow = false
  134. this.showNative(this.nativeType)
  135. }
  136. },
  137. /**
  138. * 原生广告加载失败
  139. * @param {*} placementId 广告位id
  140. * @param {*} errorInfo 错误信息
  141. */
  142. onNativeAdLoadFail: function (placementId, errorInfo) {
  143. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdLoadFail(" + placementId + ", " + errorInfo + ")");
  144. this.nativeIsLoading = false
  145. this.nativeIsLoad = false
  146. },
  147. /**
  148. * 原生广告显示成功
  149. * @param {*} placementId 广告位id
  150. * @param {*} callbackInfo 回调信息
  151. * @returns
  152. */
  153. onNativeAdShow: function (placementId, callbackInfo) {
  154. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdShow(" + placementId + ", " + callbackInfo + ")");
  155. if (this.proxy == null) return;
  156. this.proxy.onNativeAdShow(placementId, callbackInfo);
  157. },
  158. /**
  159. * 原生广告被点击
  160. * @param {*} placementId 广告位Id
  161. * @param {*} callbackInfo 回调信息
  162. */
  163. onNativeAdClick: function (placementId, callbackInfo) {
  164. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdClick(" + placementId + ", " + callbackInfo + ")");
  165. },
  166. /**
  167. * 原生广告视频开始播放,部分广告平台有此回调
  168. * @param {*} placementId 广告位id
  169. */
  170. onNativeAdVideoStart: function (placementId) {
  171. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdVideoStart(" + placementId + ")");
  172. },
  173. /**
  174. * 原生广告视频结束播放,部分广告平台有此回调
  175. * @param {*} placementId
  176. */
  177. onNativeAdVideoEnd: function (placementId) {
  178. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdVideoEnd(" + placementId + ")");
  179. },
  180. /**
  181. * 广告关闭按钮被点击,部分广告平台有此回调
  182. * @param {*} placementId
  183. * @param {*} callbackInfo
  184. */
  185. onNativeAdCloseButtonTapped: function (placementId, callbackInfo) {
  186. // ATJSSDK.printLog("AnyThinkNativeDemo::onNativeAdCloseButtonTapped(" + placementId + ", " + callbackInfo + ")");
  187. }
  188. });
  189. module.exports = ATNativeJS;
  190. if (!cc["ATNativeJS"]) {
  191. cc["ATNativeJS"] = new ATNativeJS();
  192. }