Interstitial.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import NativeAdComponent, { NativeType } from "./NativeAdComponent";
  8. import SDK, { NodeAction } from "../SDK";
  9. import Utils from "../tools/Utils";
  10. import VivoAd from "./VivoAd";
  11. const { ccclass, property } = cc._decorator;
  12. export enum InterstitialType {
  13. INTERSTITIAL_GAME_OVER = 0, //游戏结束
  14. INTERSTITIAL_GAME_STARE, //游戏开始
  15. INTERSTITIAL_GAME_PAUSE //游戏暂停
  16. }
  17. @ccclass
  18. export default class Interstitial extends cc.Component {
  19. private _config;
  20. private _insetList : cc.Node[]=[];
  21. public init(config){
  22. this._config = config;
  23. Utils.instance.log('创建Interstitial广告')
  24. }
  25. private showBanner(banner) {
  26. // "banner":{"type":0, "x":0,"y":0,"top":false }
  27. if (banner) {//存在插屏节点banner数据
  28. if (banner.enable != 1) {
  29. return;
  30. }
  31. let type = banner.type;//显示banner类型
  32. let bannerType = NativeType.BANNER;//正常banner
  33. if (type == 1) {//大banner
  34. bannerType = NativeType.BANNER_BUTTON;
  35. } else if (type == 2) {//小banner带详情按键
  36. bannerType = NativeType.BANNER_LITTLE_BUTTON;
  37. }
  38. VivoAd.Instance.getBanner().showBanner(bannerType, banner.top, banner.x, banner.y);
  39. }
  40. }
  41. /**
  42. * 展示原生插屏
  43. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  44. * @param id 默认0
  45. * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false
  46. */
  47. public showInterstitial(type: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) {
  48. let self = this;
  49. //显示banner
  50. //广告数据是否存在
  51. if (this._config == null) {
  52. return;
  53. }
  54. let adNodeList = JSON.parse(this._config.insertAdNodeList);
  55. if (this._config.masterEnable != 1) {//所有广告是否打开
  56. return;
  57. }
  58. if (adNodeList == null || adNodeList.length <= 0) {
  59. Utils.instance.log("---------showInterstitial 未配置广告节点adNodeList为空");
  60. return;
  61. }
  62. //当前节点是否打开
  63. let adNode = adNodeList[type];
  64. if (adNode == null) {
  65. Utils.instance.log("---------showInterstitial 节点未配置广告nodeId:" + type);
  66. // isforever = false;
  67. return;
  68. }
  69. //插屏关闭,banner正常展示
  70. if (this._config.insertAdEnable != 1) {//插屏是否打开
  71. this.showBanner(adNode.banner);
  72. return;
  73. }
  74. //插屏节点关闭,banner正常展示
  75. if (adNode && adNode.enable != 1) {
  76. // isforever = false;
  77. this.showBanner(adNode.banner);
  78. return;
  79. }
  80. let nativeDate = VivoAd.Instance.getVivoNativeData();
  81. if (nativeDate == null || !nativeDate.isReady()) {
  82. Utils.instance.log("---------showInterstitial 广告数据不存在-----");
  83. this.showBanner(adNode.banner);
  84. return;
  85. }
  86. let node = cc.director.getScene().children[0];
  87. let insertAd = node.getChildByName("insertAd");
  88. if (insertAd) {
  89. insertAd.removeFromParent();
  90. // return ;
  91. }
  92. let go = () => {
  93. self.show(node, nativeDate, adNode);
  94. }
  95. this.delayShow(go);
  96. }
  97. private show(node, nativeDate, adDate) {
  98. let self = this;
  99. let clickCallBack = () => {
  100. VivoAd.Instance.creatorNativeData();
  101. SDK.Instance.reportLog(NodeAction.NATIVE_CLICK, "原生插屏点击");
  102. };
  103. let showCallBack = () => {
  104. //缓存下一条广告
  105. VivoAd.Instance.hideBanner();
  106. VivoAd.Instance.creatorNativeData();
  107. SDK.Instance.reportLog(NodeAction.NATIVE_SHOW, "原生插屏展示");
  108. };
  109. let closeCallBack = () => {
  110. if (adDate) {//关闭插屏后,展示插屏节点内banner
  111. if (adDate.show_banner && adDate.show_banner > 0) {
  112. // self.showNativeBanner(self.bannerTop, self.bannerType, self.banner_y, self.banner_x);
  113. this.showBanner(adDate.banner);
  114. }
  115. }
  116. //清理所有展示插屏
  117. self.cleanInset();
  118. SDK.Instance.reportLog(NodeAction.NATIVE_CLOSE, "原生插屏关闭");
  119. };
  120. Utils.instance.loadPrefabs("sdk/res/prefabs/NativeInsertAd", (prefabs) => {
  121. let adNode = cc.instantiate(prefabs);
  122. let nativeAd = adNode.addComponent(NativeAdComponent);
  123. nativeAd.initData(nativeDate);
  124. nativeAd.listionCall(showCallBack, clickCallBack, closeCallBack);
  125. // adNode.active = false;
  126. nativeAd.showAd(NativeType.INSET, self._config);
  127. node.addChild(adNode, 1000, "insert")//加节点名,防止重复加广告
  128. self.cacheInset(adNode);
  129. });
  130. }
  131. private cleanInset(){
  132. if (this._insetList&&this._insetList.length>0) {
  133. this._insetList.forEach((node) => {
  134. if(node){
  135. node.removeFromParent();
  136. node.destroy();
  137. }
  138. })
  139. this._insetList = [];
  140. }
  141. }
  142. private cacheInset(adNode) {
  143. this.cleanInset();
  144. if(this._insetList){
  145. this._insetList.push(adNode);
  146. }
  147. }
  148. private delayShow(go) {
  149. let self = this;
  150. //插屏延时展示
  151. if (self._config) {
  152. let dayTime = self._config.insertAdMaxDayTimes;
  153. Utils.instance.log("-------delayTime: " + dayTime);
  154. let mistakeRate = this._config.insertAdDayTimesMistakeRate;
  155. let random = Utils.instance.getRandomInt(1, 100);
  156. Utils.instance.log("-------delayTime random: " + random + " limitClick: " + SDK.Instance.limitClick);
  157. if (SDK.Instance.limitClick >= this._config.maxClickCount || mistakeRate < random) {
  158. dayTime = 0;
  159. }
  160. Utils.instance.log("-------delayTime: " + dayTime);
  161. self.scheduleOnce(() => {
  162. go();
  163. }, dayTime);
  164. }
  165. }
  166. }