YZ_NativeItem.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import PlatUtils from "./PlatUtils";
  2. import { utils } from "./Utils";
  3. import YZ_NativeAdObject from "./YZ_NativeAdObject";
  4. import YZ_Constant, { BannerLocation } from "./YZ_Constant";
  5. import CompatibleTool from "./CompatibleTool";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class YZ_NativeItem extends cc.Component {
  9. private _nativeAdTiltle: cc.Label = null;
  10. private _nativeAdIcon: cc.Sprite = null;
  11. private _nativeAdDesc: cc.Label = null;
  12. private _nativeAdImg: cc.Sprite = null;
  13. private _closeBtn: cc.Node = null;
  14. private _downBtn: cc.Node = null;
  15. private _noImageView: cc.Node = null;
  16. _nativeAd: YZ_NativeAdObject = null;
  17. isShow: boolean = false;
  18. showType: number = 1;
  19. params?: any = null;
  20. content: cc.Node = null;
  21. onLoad() {
  22. this.content = this.node.children[0];
  23. this._noImageView = this.content.getChildByName("NoImageView");
  24. this._nativeAdTiltle = this._noImageView.getChildByName("title").getComponent(cc.Label);
  25. this._nativeAdIcon = this._noImageView.getChildByName("icon").getComponent(cc.Sprite);
  26. this._nativeAdDesc = this._noImageView.getChildByName("desc").getComponent(cc.Label);
  27. this._nativeAdImg = this.content.getChildByName("image").getComponent(cc.Sprite);
  28. // this._downBtn = this.content.getChildByName("Btn_Download");
  29. this._closeBtn = this.content.getChildByName("closeBtn");
  30. if (this.params) {
  31. if (this.params.parent) {
  32. this.node.width = this.node.parent.width;
  33. this.node.height = this.node.parent.height;
  34. }
  35. } else {
  36. if (utils.ServerConfig.st_native_ad_height) {
  37. this.node.height = utils.ServerConfig.st_native_ad_height;
  38. }
  39. }
  40. this.content.active = false;
  41. cc.game.on(YZ_Constant.YZ_NativeAdClick, () => {
  42. this.reportAdClick();
  43. }, this);
  44. // this._closeBtn.active = utils.ServerConfig.st_banner_close_but_show ? (utils.ServerConfig.st_banner_close_but_show == "true") : false;
  45. // this._downBtn.active = utils.ServerConfig.show_statement_nativeAd_closeBtn ? (utils.ServerConfig.show_statement_nativeAd_closeBtn == "true") : false;
  46. }
  47. onDisable() {
  48. cc.game.targetOff(this);
  49. this.node.destroy();
  50. }
  51. update() {
  52. if (this._nativeAd && !this.isShow) {
  53. this.isShow = true;
  54. this.showNativeAd();
  55. }
  56. }
  57. init(nativeObj: YZ_NativeAdObject) {
  58. utils.showLog("初始化单个原生广告>>>");
  59. this._nativeAd = nativeObj;
  60. }
  61. showNativeAd() {
  62. if (utils.ServerConfig.st_native_ad_is_hide_banner && utils.ServerConfig.st_native_ad_is_hide_banner == "true") {
  63. utils.showLog("服务器配置显示结算原生广告后隐藏banner >>>");
  64. utils.adManager.HideBanner(BannerLocation.Game);
  65. }
  66. if (utils.ServerConfig.st_native_ad_show_rec_banner && utils.ServerConfig.st_native_ad_show_rec_banner == "true") {
  67. utils.showRecBanner();
  68. }
  69. let nativeData = this._nativeAd.data;
  70. let title = nativeData.title;
  71. let desc = nativeData.desc;
  72. if (title.length > 6) {
  73. title = title.slice(0, 6);
  74. title += "...";
  75. }
  76. if (desc.length > 18) {
  77. desc = desc.slice(0, 17);
  78. desc += "...";
  79. }
  80. this._nativeAdTiltle.string = title;
  81. this._nativeAdDesc.string = desc;
  82. if (nativeData.imgUrlList && nativeData.imgUrlList.length > 0) {
  83. // 有图片,优先显示图片
  84. // this._titleLabel.node.active = true;
  85. // this._icon.node.active = false;
  86. // this._img.node.active = true;
  87. // this._desLabel.node.active = true;
  88. this._noImageView.active = false;
  89. this._nativeAdImg.node.active = true;
  90. CompatibleTool.LoadRes(nativeData.imgUrlList[0], (err, res) => {
  91. if (!err && cc.isValid(this) && this._nativeAdImg) {
  92. this._nativeAdImg.spriteFrame = new cc.SpriteFrame(res);
  93. this._nativeAdImg.node.active = true;
  94. this.content.active = true;
  95. }
  96. });
  97. } else if (PlatUtils.IsOPPO && nativeData.iconUrlList && nativeData.iconUrlList.length > 0) {
  98. // 有icon
  99. this._nativeAdImg.node.active = false;
  100. this._noImageView.active = true;
  101. CompatibleTool.LoadRes(nativeData.iconUrlList[0], (err, res) => {
  102. if (!err && cc.isValid(this) && this._nativeAdIcon) {
  103. this._nativeAdIcon.spriteFrame = new cc.SpriteFrame(res);
  104. this.content.active = true;
  105. }
  106. });
  107. } else if (PlatUtils.IsVIVO && nativeData.icon) {
  108. // 有icon
  109. this._nativeAdImg.node.active = false;
  110. this._noImageView.active = true;
  111. CompatibleTool.LoadRes(nativeData.icon, (err, res) => {
  112. if (!err && cc.isValid(this) && this._nativeAdIcon) {
  113. this._nativeAdIcon.spriteFrame = new cc.SpriteFrame(res);
  114. this.content.active = true;
  115. }
  116. });
  117. }
  118. this.node.active = true;
  119. this.reportAdShow();
  120. if (this.params) {
  121. this.params.callBack && this.params.callBack();
  122. }
  123. }
  124. // onEnable() {
  125. // // if (PlatUtils.IsOPPO || PlatUtils.IsVIVO) {
  126. // // if (!this._closeBtn.active) {
  127. // // this.node.on(cc.Node.EventType.TOUCH_START, (event: cc.Event) => {
  128. // // this._reportAdClick();
  129. // // }, this);
  130. // // }
  131. // // }
  132. // }
  133. // onDisable() {
  134. // if (PlatUtils.IsOPPO || PlatUtils.IsVIVO) {
  135. // this.node.targetOff(this);
  136. // }
  137. // }
  138. onBtnClickHandler(event: cc.Event, data: any) {
  139. switch (event.target.name) {
  140. case "closeBtn": {
  141. this.node.active = false;
  142. break;
  143. }
  144. case "Btn_Download": {
  145. this.reportAdClick();
  146. break;
  147. }
  148. }
  149. }
  150. reportAdShow() {
  151. utils.showLog("reportAdShow");
  152. if (this._nativeAd) {
  153. this._nativeAd.reportAdShow();
  154. }
  155. }
  156. reportAdClick() {
  157. if (this._nativeAd) {
  158. this._nativeAd.reportAdClick();
  159. } else {
  160. utils.showLog("广告加载失败!");
  161. }
  162. }
  163. }