AdAgentUC.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 AdAgentUC extends AdAgent {
  11. _videoCallback: Function = null;
  12. _isVideoLoaded: boolean = false;
  13. _videoAd: any = null;
  14. _bannerAd: any = null;
  15. //@ts-ignore
  16. uc = window.uc;
  17. public Init() {
  18. utils.showLog("UC 广告初始化");
  19. this.initVideo();
  20. if (!this._sysInfo) {
  21. this._sysInfo = this.uc.getSystemInfoSync()
  22. if (typeof this._sysInfo === 'string') {
  23. try {
  24. this._sysInfo = JSON.parse(this._sysInfo);
  25. } catch (e) { }
  26. }
  27. }
  28. }
  29. _sysInfo: any = {};
  30. public ShowBanner() {
  31. if (this._bannerAd) {
  32. this._bannerAd.destroy();
  33. this._bannerAd = null;
  34. }
  35. // 0:左上 1:顶部居中 2:右上
  36. // 3:左边垂直居中 4:居中 5:右边垂直居中
  37. // 6:左下 7:底部居中 8:右下 (默认为0)
  38. utils.showLog("uc banner width>>", this._sysInfo.screenWidth, " #height>>", this._sysInfo.screenWidth * 194 / 345)
  39. this._bannerAd = this.uc.createBannerAd({
  40. style: {
  41. gravity: 7,
  42. bottom: 0, // 底部 margin
  43. width: cc.winSize.height < cc.winSize.width ? 250 : this._sysInfo.screenWidth, // 建议最小宽度 250dp,最大宽度为屏幕尺寸
  44. height: this._sysInfo.screenWidth / 4,
  45. }
  46. })
  47. if (this._bannerAd) {
  48. this._bannerAd.show()
  49. this._bannerAd.onError(err => {
  50. utils.showLog("UC平台banner出错" + err);
  51. });
  52. }
  53. }
  54. public ShowInterstitial() {
  55. // if(this.)
  56. utils.showLog("展示插屏广告");
  57. const interstitialAd = this.uc.createInterstitialAd();
  58. interstitialAd.load()
  59. .then()
  60. .catch(err => utils.showLog(`插屏加载异常:${err}`));
  61. interstitialAd.onLoad(() => {
  62. interstitialAd.offLoad(); // 取消 load 事件的监听,不传 callback 的话会取消所有的监听
  63. interstitialAd
  64. .show()
  65. .then()
  66. .catch(err => utils.showLog(`插屏展示异常:${err}`));
  67. utils.showLog('UC插屏广告加载成功');
  68. });
  69. interstitialAd.onError(err => {
  70. utils.showLog(err)
  71. });
  72. }
  73. initVideo() {
  74. this._videoAd = this.uc.createRewardVideoAd();
  75. this._videoAd.onLoad(() => {
  76. utils.showLog('激励视频 广告加载成功');
  77. });
  78. this._videoAd.onError(err => {
  79. utils.showLog("出错了:" + err);
  80. if (this._videoCallback) {
  81. this._videoCallback(false, "暂无视频广告");
  82. this._videoCallback = null;
  83. }
  84. });
  85. this._videoAd.onClose(res => {
  86. utils.showLog("用户关闭视频" + res);
  87. if (res && res.isEnded) {
  88. if (this._videoCallback) {
  89. this._videoCallback(true, "");
  90. this._videoCallback = null;
  91. }
  92. } else {
  93. if (this._videoCallback) {
  94. this._videoCallback(false, "视频播放完毕才能够获取奖励!");
  95. this._videoCallback = null;
  96. }
  97. }
  98. });
  99. }
  100. public ShowVideo(callback: Function) {
  101. if (PlatUtils.ISUC) {
  102. this._videoCallback = callback;
  103. if (!this._videoAd) {
  104. this.initVideo();
  105. } else {
  106. this._videoAd.show();
  107. }
  108. }
  109. }
  110. }