Banner.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 SDK, { NodeAction } from "../SDK";
  8. import Utils from "../tools/Utils";
  9. import VivoAd from "./VivoAd";
  10. import NativeAdComponent, { NativeType } from "./NativeAdComponent";
  11. const { ccclass, property } = cc._decorator;
  12. @ccclass
  13. export default class Banner extends cc.Component {
  14. private _bannerTemplate = null; //模板banner
  15. private _config; //banner数据
  16. private _bannerCode;
  17. private _isBanner;
  18. private _bannerList: cc.Node[] = [];
  19. private _nativeBanner;
  20. private _bannerType;
  21. //初始化
  22. public init(config) {
  23. this._config = config;
  24. this._isBanner = config.bannerAdEnable > 0;
  25. this._bannerCode = config.bannerAdCode;
  26. Utils.instance.log('创建banner广告')
  27. this.initBannerTemplate();
  28. }
  29. //初始化模板banner
  30. private initBannerTemplate() {
  31. let self = this;
  32. var info = qg.getSystemInfoSync();
  33. Utils.instance.log(info);
  34. var targetBannerAdWidth = info.screenWidth > info.screenHeight ? (info.screenWidth <= 720 ? info.screenWidth / 2 : info.screenWidth / 2 - 30) : info.screenWidth;
  35. Utils.instance.log("bannertargetBannerAdWidth : " + targetBannerAdWidth);
  36. this._bannerTemplate = qg.createBannerAd({
  37. posId: self._bannerCode,
  38. style: {
  39. top: info.screenHeight - 170, // 置于屏幕底部
  40. left: (info.screenWidth - targetBannerAdWidth) / 2, // 居中
  41. }
  42. })
  43. this._bannerTemplate.onSize(function (size) {
  44. Utils.instance.log('banner 宽度:' + size.width + ', banner 高度:' + size.height)
  45. })
  46. this._bannerTemplate.onLoad(function () {
  47. Utils.instance.log('banner 广告加载成功')
  48. })
  49. this._bannerTemplate.onError(function (err) {
  50. Utils.instance.log("banner 广告加载错误:" + JSON.stringify(err))
  51. })
  52. }
  53. //展示模板banner
  54. public showBannerTemplate() {
  55. if (this._isBanner) {
  56. if (this._bannerTemplate) {
  57. this._bannerTemplate.show();
  58. } else {
  59. this.initBannerTemplate();
  60. }
  61. }
  62. }
  63. //隐藏模板banner
  64. public hideBannerTemplate() {
  65. if (this._bannerTemplate) {
  66. this._bannerTemplate.hide();
  67. }
  68. }
  69. /**
  70. * 展示原生banner
  71. * @param type 默认原生普通banner
  72. */
  73. public showBanner(type: NativeType = NativeType.BANNER, top: boolean = false, y: number = 0, x: number = 0) {
  74. this._bannerType = type;
  75. Utils.instance.log("***********showNativeBanner type :" + type);
  76. if (type == NativeType.BANNER_NO_NATIVE) {
  77. this.showBannerTemplate();
  78. return;
  79. }
  80. // this.showToast("banner显示了");
  81. let self = this;
  82. if (this._config == null) {
  83. return;
  84. }
  85. if (this._config.masterEnable != 1) {//插屏是否打开
  86. return;
  87. }
  88. if (this._config.bannerAdEnable != 1) {
  89. return;
  90. }
  91. let nativeData = VivoAd.Instance.getVivoNativeData();
  92. if (nativeData == null || !nativeData.isReady()) {
  93. Utils.instance.log("************showNativeBanner 广告数据不存在");
  94. return;
  95. }
  96. let clickCallBack = () => {
  97. VivoAd.Instance.creatorNativeData();
  98. SDK.Instance.reportLog(NodeAction.NATIVE_CLICK, "原生Banner点击");
  99. };
  100. let showCallBack = () => {
  101. VivoAd.Instance.creatorNativeData();
  102. //缓存下一条广告
  103. SDK.Instance.reportLog(NodeAction.NATIVE_SHOW, "原生Banner展示");
  104. };
  105. let closeCallBack = () => {
  106. // this.getVivoNativeData();
  107. self.cleanBanner();
  108. SDK.Instance.reportLog(NodeAction.NATIVE_CLOSE, "原生Banner关闭");
  109. };
  110. let node = cc.director.getScene().children[0];
  111. let banner = node.getChildByName("banner");
  112. if (banner) {
  113. banner.removeFromParent();
  114. banner.destroy();
  115. }
  116. let winSize = cc.view.getVisibleSize();
  117. if (y == 0) {
  118. if (top) {
  119. y = winSize.height / 2 - 150 / 2;
  120. } else {
  121. y = -winSize.height / 2 + 150 / 2;
  122. }
  123. }
  124. Utils.instance.log("-------bannerType: " + this._bannerType);
  125. let prefabsRes = "sdk/res/prefabs/NativeBannerAd";
  126. if (type == NativeType.BANNER_BUTTON) {
  127. prefabsRes = "sdk/res/prefabs/NativeBannerAddButtonAd"
  128. if (top) {
  129. y = winSize.height / 2 - 400 / 2;
  130. } else {
  131. y = -winSize.height / 2 + 400 / 2;
  132. }
  133. } else if (type == NativeType.BANNER_LITTLE_BUTTON) {
  134. if (top) {
  135. y = winSize.height / 2 - 150 / 2;
  136. } else {
  137. y = -winSize.height / 2 + 150 / 2;
  138. }
  139. prefabsRes = "sdk/res/prefabs/NativeBannerLittleBUttonAd"
  140. }
  141. Utils.instance.loadPrefabs(prefabsRes, (prefabs) => {
  142. let adNode = cc.instantiate(prefabs);
  143. self._nativeBanner = adNode.addComponent(NativeAdComponent);
  144. self._nativeBanner.initData(nativeData);
  145. self._nativeBanner.listionCall(showCallBack, clickCallBack, closeCallBack);
  146. adNode.width = winSize.width;
  147. self._nativeBanner.showAd(type, self._config, x, y);
  148. node.addChild(adNode, 1000, "banner")
  149. self.cacheBanner(adNode);
  150. });
  151. }
  152. private cacheBanner(node) {
  153. let self = this;
  154. self.cleanBanner();
  155. if(self._bannerList){
  156. self._bannerList.push(node);
  157. }
  158. }
  159. private cleanBanner() {
  160. if (this._bannerList) {
  161. if (this._bannerList.length > 0) {
  162. this._bannerList.forEach((node) => {
  163. node.removeFromParent();
  164. node.destroy();
  165. });
  166. this._bannerList = [];
  167. }
  168. }
  169. }
  170. /**
  171. * 隐藏b原生banner
  172. */
  173. public hideBanner(hide: boolean = true) {
  174. Utils.instance.log("------hideNativeBanner: " + hide);
  175. if (hide) {
  176. this._bannerType = null;
  177. }
  178. if (this._bannerType == NativeType.BANNER_NO_NATIVE) {
  179. this.hideBannerTemplate();
  180. return;
  181. }
  182. if (this._nativeBanner) {
  183. this._nativeBanner.hide(true);
  184. this._nativeBanner = null;
  185. }
  186. }
  187. }