NavtiveData.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 Utils from "../tools/Utils";
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export default class NavtiveData extends cc.Component {
  11. private _utils: Utils;
  12. private _nativeCurrentAd = null;
  13. private _nativeAd = null
  14. private _ready = false;
  15. constructor() {
  16. super();
  17. this._utils = Utils.instance;
  18. }
  19. start() {
  20. }
  21. private enterGmae = false;
  22. /**
  23. * 创建广告
  24. */
  25. public initAd(config, callbacks?) {
  26. this._utils.log("------创建native广告-----");
  27. let posId = config.insertAdCode;
  28. this._nativeAd = qg.createNativeAd({
  29. posId: posId,
  30. });
  31. this.enterGmae = false;
  32. let self = this;
  33. if (this._nativeAd) {
  34. this._nativeAd.load();
  35. this._utils.log("------创建native广告-this.nativeAd.load(): " + this._nativeAd);
  36. this._nativeAd.onLoad(function (res) {
  37. self.enterGmae = true;
  38. self._utils.log('原生广告加载完成-onload触发 ' + JSON.stringify(res));
  39. if (res && res.adList) {
  40. self._nativeCurrentAd = res.adList.pop();
  41. self._ready = true;
  42. if (callbacks) {
  43. callbacks(true);
  44. }
  45. }
  46. })
  47. this._nativeAd.onError(err => {
  48. self._utils.log("原生广告加载异常" + JSON.stringify(err));
  49. self.enterGmae = true;
  50. if (err) {
  51. if (err.errCode == 30009) {
  52. if (callbacks) {
  53. callbacks(false);
  54. }
  55. } else {
  56. if (callbacks) {
  57. callbacks(false);
  58. }
  59. }
  60. }
  61. });
  62. } else {
  63. self.enterGmae = true;
  64. if (callbacks) {
  65. callbacks(false);
  66. }
  67. }
  68. //5秒没反应,直接进入游戏
  69. this.scheduleOnce(() => {
  70. if (self.enterGmae) {
  71. return;
  72. }
  73. if (callbacks) {
  74. callbacks(false);
  75. }
  76. }, 5);
  77. }
  78. /**
  79. * 广告展示上报
  80. */
  81. public reportAdShow() {
  82. if (this._nativeAd && this._nativeCurrentAd) {
  83. this._nativeAd.reportAdShow({ adId: this._nativeCurrentAd.adId.toString() });
  84. }
  85. }
  86. /**
  87. * 广告点击上报
  88. */
  89. public reportAdClick() {
  90. if (this._nativeAd && this._nativeCurrentAd) {
  91. this._nativeAd.reportAdClick({ adId: this._nativeCurrentAd.adId.toString() });
  92. }
  93. }
  94. /**
  95. * 获取广告数据
  96. * @returns
  97. */
  98. public getNativeDate() {
  99. return this._nativeCurrentAd;
  100. }
  101. public isReady() {
  102. return this._ready;
  103. }
  104. }