AdManager.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { sys } from "cc";
  2. import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
  3. // AdManager.ts
  4. export class AdManager {
  5. private static instance: AdManager;
  6. // 微信和抖音的激励视频广告对象
  7. private rewardVideoAd: any = null;
  8. // 广告单元ID,请替换为你的实际广告单元ID
  9. private adUnitId: string = 'adunit-d8e3c2b4bcfdc32e';
  10. private dyAdUnitId: string = '3620da76lobm18tpb5'
  11. private isCanShow = true;
  12. private constructor() { }
  13. // 获取AdManager的单例对象
  14. public static getInstance(): AdManager {
  15. if (!AdManager.instance) {
  16. AdManager.instance = new AdManager();
  17. }
  18. return AdManager.instance;
  19. }
  20. // 初始化激励视频广告
  21. public initRewardVideoAd() {
  22. if (sys.platform === sys.Platform.WECHAT_GAME) {
  23. // 微信小游戏环境
  24. this.rewardVideoAd = wx.createRewardedVideoAd({ adUnitId: this.adUnitId });
  25. this.rewardVideoAd.onError(err => {
  26. console.error('RewardedVideoAd Error: ', err);
  27. });
  28. } else if (sys.platform === sys.Platform.BYTEDANCE_MINI_GAME) {
  29. // 抖音小游戏环境
  30. this.rewardVideoAd = tt.createRewardedVideoAd({ adUnitId: this.dyAdUnitId });
  31. this.rewardVideoAd.onError(err => {
  32. console.error('RewardedVideoAd Error: ', err);
  33. });
  34. }
  35. }
  36. // 显示激励视频广告
  37. public showRewardVideoAd(successCallback?: () => void, failCallback?: (err: any) => void) {
  38. if (!this.isCanShow) {
  39. oops.gui.toast('点击得太快了哟')
  40. return;
  41. }
  42. this.isCanShow = false;
  43. if (this.rewardVideoAd) {
  44. this.rewardVideoAd.show().then(() => {
  45. console.log('Rewarded video ad displayed!');
  46. // successCallback && successCallback();
  47. }).catch(err => {
  48. console.error('Failed to display rewarded video ad:', err);
  49. this.rewardVideoAd.load().then(() => this.rewardVideoAd.show());
  50. failCallback && failCallback(err);
  51. failCallback = null
  52. oops.audio.resumeAll()
  53. });
  54. // 监听广告关闭事件
  55. this.rewardVideoAd.onClose((res: any) => {
  56. // 用户观看完整广告
  57. if (res && res.isEnded) {
  58. console.log('Rewarded video ad watched till the end');
  59. successCallback && successCallback();
  60. successCallback = null
  61. oops.audio.resumeAll()
  62. } else {
  63. // 用户提前关闭广告
  64. console.log('Rewarded video ad was closed before completion');
  65. failCallback && failCallback(new Error('Ad was closed before completion'));
  66. failCallback = null
  67. oops.audio.resumeAll()
  68. }
  69. });
  70. } else {
  71. console.error('Reward video ad is not initialized.');
  72. failCallback && failCallback(new Error('Reward video ad is not initialized'));
  73. failCallback = null
  74. // successCallback && successCallback();
  75. oops.audio.resumeAll()
  76. }
  77. // 0.5秒后重新开启按钮点击
  78. const timer = setTimeout(() => {
  79. this.isCanShow = true;
  80. clearTimeout(timer)
  81. }, 1500);
  82. }
  83. }