AdAgentCocosplay.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. const { ccclass, property } = cc._decorator;
  6. /**
  7. * uc广告组件
  8. */
  9. @ccclass
  10. export default class AdAgentCocosplay extends AdAgent {
  11. banner: any = null;
  12. interstitialAd: any = {};
  13. videoAd: any = {};
  14. bannerLoaded = false;
  15. interstitialAdLoaded = false;
  16. videoAdLoaded = false;
  17. _videoCallback: Function = null;
  18. _isVideoLoaded: boolean = false;
  19. _videoAd: any = null;
  20. _bannerAd: any = null;
  21. //@ts-ignore
  22. uc = window.uc;
  23. public Init() {
  24. utils.registerServerInitEvent(() => {
  25. this.initBanner();
  26. this.createInsertAd();
  27. this.createVideoAd();
  28. }, this)
  29. }
  30. initBanner() {
  31. let self = this;
  32. self.bannerLoaded = false;
  33. //@ts-ignore
  34. self.banner = AdSDK.createBannerAd("1", utils.config.cocosConfig.bannerId, 2);
  35. //注册onLoad函数,游戏调用创建banner时,AdSDK通知平台创建广告,当创建成功,会执行该回调函数通知游戏
  36. self.banner.onLoad(function () {
  37. self.bannerLoaded = true;
  38. console.log("banner 创建banner成功,可以调用展示");
  39. });
  40. //注册onError函数,游戏调用创建banner时,AdSDK通知平台创建广告,当创建失败,会执行该回调函数通知游戏
  41. self.banner.onError(function (param) {
  42. self.destroyBannerAd();
  43. console.log("banner 创建banner失败,错误码 = ", param.errorCode);
  44. });
  45. console.log('banner 游戏创建banner广告');
  46. }
  47. ShowBanner() {
  48. let self = this;
  49. // self.createBannerAd()
  50. // self.bannerLoaded = false;
  51. if (self.bannerLoaded) {//Object.keys(banner).length &&
  52. self.banner.show().then(function () {
  53. console.log('banner 广告显示成功')
  54. }, function (err) {
  55. console.log('banner 广告显示失败')
  56. });
  57. console.log('banner 展示banner广告');
  58. } else {
  59. console.log('banner 未加载成功');
  60. }
  61. }
  62. HideBanner() {
  63. this.banner.hide();
  64. console.log('banner 隐藏banner广告');
  65. }
  66. destroyBannerAd() {
  67. this.bannerLoaded = false;
  68. this.banner.destroy();
  69. this.banner = null;
  70. console.log('banner 销毁banner广告');
  71. }
  72. public get ServerConfig() {
  73. return utils._tool_Cocosplay.ServerConfig;
  74. }
  75. //插屏显示次数
  76. _insertShowCount: number = 0;
  77. _insertLastShowTime: number = 0;
  78. /**
  79. * 验证插屏是否能展示
  80. * 1、次数限制 默认每日8次
  81. * 2、时间限制 默认60秒
  82. */
  83. private checkInsertAdShow(): boolean {
  84. let maxShowCount = this.ServerConfig.intersititial_max_show_count;
  85. maxShowCount = 0;
  86. let intervalTime = this.ServerConfig.intersititial_interval_time;
  87. let curTime: number = new Date().getTime();
  88. let interval: number = (curTime - this._insertLastShowTime) / 1000;
  89. utils.showLog("cocos服务器插屏最大显示次数为:" + maxShowCount + ",间隔显示时间为:" + intervalTime + "秒!");
  90. utils.showLog("cocos插屏当前广告显示次数:" + this._insertShowCount + "次,间隔时间:" + interval + "秒!");
  91. if (maxShowCount > 0 && this._insertShowCount >= maxShowCount) {
  92. utils.showLog("cocos插屏广告显示的次数达到" + maxShowCount + "次。插屏不显示");
  93. return false;
  94. }
  95. if (intervalTime > 0 && interval < intervalTime) {
  96. utils.showLog("cocos插屏广告显示的间隔少于" + intervalTime + "秒。插屏不显示");
  97. return false;
  98. }
  99. return true;
  100. }
  101. // 创建插屏广告 展示一次调用一次创建
  102. // 参数
  103. // adId: string 广告序号ID 游戏自定义
  104. // interstitialAdId: string 插屏广告ID 需后台申请
  105. // style: int 广告类型 1 全屏 2 半屏
  106. public ShowInterstitial() {
  107. let self = this
  108. if (!self.checkInsertAdShow()) return;
  109. if (self.interstitialAdLoaded) {
  110. self.interstitialAd.show().then(function () {
  111. self._insertLastShowTime = new Date().getTime();
  112. self.destroyInterstitialAd();
  113. self.createInsertAd();
  114. }, function (err) {
  115. console.log("interstitialAd 广告显示失败")
  116. })
  117. console.log("interstitialAd 游戏展示插屏广告");
  118. }
  119. }
  120. public createInsertAd() {
  121. let self = this
  122. utils.showLog("插屏id为:" + utils.config.cocosConfig.insertId)
  123. //@ts-ignore
  124. this.interstitialAd = AdSDK.createInterstitialAd("1", utils.config.cocosConfig.insertId, 2);
  125. this.interstitialAd.onLoad(function () {
  126. self.interstitialAdLoaded = true;
  127. console.log("interstitialAd 创建插屏成功,可以调用展示");
  128. });
  129. self.interstitialAd.onError(function (param) {
  130. console.log("interstitialAd 创建插屏失败,错误码 = ", param.errorCode);
  131. self.destroyInterstitialAd();
  132. })
  133. }
  134. hideInterstitialAd() {
  135. this.interstitialAd.hide();
  136. console.log("interstitialAd 游戏隐藏插屏广告");
  137. }
  138. destroyInterstitialAd() {
  139. this.interstitialAdLoaded = false;
  140. this.interstitialAd.destroy();
  141. console.log("interstitialAd 游戏销毁插屏广告");
  142. }
  143. // 创建激励视频广告 展示一次创建一次
  144. // 参数
  145. // adId: string 广告序号ID 游戏自定义
  146. // videoAdId: string 视频广告ID 需后台申请
  147. // screenOrientation: int 广告类型 1 横屏 2 竖屏
  148. public ShowVideo(callback: Function) {
  149. let self = this;
  150. self._videoCallback = callback;
  151. if (self.videoAdLoaded) {
  152. self.videoAd.show().then(function () {
  153. if (self._videoCallback) {
  154. self._videoCallback(true, "视频播放成功");
  155. self._videoCallback = null;
  156. }
  157. }, function (err) {
  158. if (self._videoCallback) {
  159. self._videoCallback(false, "暂无视频广告");
  160. self._videoCallback = null;
  161. }
  162. })
  163. } else {
  164. if (self._videoCallback) {
  165. self._videoCallback(false, "暂无视频广告");
  166. self._videoCallback = null;
  167. }
  168. self.createVideoAd();
  169. }
  170. }
  171. createVideoAd() {
  172. let self = this;
  173. console.log("视频id为:" + utils.config.cocosConfig.videoId)
  174. //@ts-ignore
  175. self.videoAd = AdSDK.createRewardedVideoAd("1", utils.config.cocosConfig.videoId, 2);
  176. self.videoAd.onLoad(function () {
  177. self.videoAdLoaded = true;
  178. console.log("rewardedvideoAd 创建视频广告成功,可以调用展示")
  179. });
  180. self.videoAd.onError(function (param) {
  181. console.log("rewardedvideoAd 创建视频广告失败,错误码 = ", param.errorCode);
  182. self.destroyRewardedVideoAd();//加载广告失败,销毁
  183. if (self._videoCallback) {
  184. self._videoCallback(false, "暂无视频广告");
  185. self._videoCallback = null;
  186. }
  187. });
  188. self.videoAd.onClose(function () {
  189. if (self._videoCallback) {
  190. self._videoCallback(false, "观看完视频才能获得奖励!");
  191. self._videoCallback = null;
  192. }
  193. self.destroyRewardedVideoAd();//视频广告关闭,销毁
  194. self.createVideoAd();
  195. });
  196. }
  197. destroyRewardedVideoAd() {
  198. this.videoAdLoaded = false;
  199. this.videoAd.destroy();
  200. console.log("rewardedvideoAd 游戏销毁视频广告");
  201. }
  202. }