AdvertMgr.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { _decorator, Node, Prefab, instantiate, Component, Camera, UITransform, v3, game, view, screen, tween, Vec3 } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /** 广告管理*/
  4. @ccclass('AdvertMgr')
  5. export class AdvertMgr {
  6. private static _instance: AdvertMgr | null = null;
  7. public static get instance(): AdvertMgr {
  8. if (!this._instance) this._instance = new AdvertMgr();
  9. return this._instance;
  10. }
  11. adInstance: any = null;
  12. gtag: any = null;
  13. openAd: boolean = true;
  14. initilize(): void {
  15. this.adInstance = (window as any)['adInstance'];
  16. console.log('ad sdk初始化');
  17. console.log(this.adInstance);
  18. }
  19. /** 显示插屏广告*/
  20. showInterstitial(cb?: () => void): void {
  21. console.log('显示插屏广告');
  22. if (!this.adInstance || !this.openAd) {
  23. cb && cb();
  24. return;
  25. }
  26. this.adInstance.interstitialAd({
  27. beforeAd() {
  28. console.log('The ad starts playing');
  29. },
  30. afterAd() {
  31. console.log('The ad ends playing');
  32. },
  33. error(err) {
  34. // console.log('The ad failed to load');
  35. // tgxUITips.show(err);
  36. // AudioMgr.inst.resume();
  37. }
  38. });
  39. }
  40. /** 显示激励广告*/
  41. showReawardVideo(cb?: () => void): void {
  42. if (!this.adInstance || !this.openAd) {
  43. cb && cb();
  44. return;
  45. }
  46. this.adInstance.rewardAd({
  47. beforeAd() {
  48. console.log('The ad starts playing, and the game should pause.');
  49. // AudioMgr.inst.pause();
  50. },
  51. adDismissed() {
  52. console.log('Player dismissed the ad before completion.');
  53. // AudioMgr.inst.resume();
  54. },
  55. adViewed() {
  56. console.log('Ad was viewed and closed.');
  57. if (cb) cb();
  58. // AudioMgr.inst.resume();
  59. },
  60. error(err: any) {
  61. // console.log(`激励广告错误:${err}`);
  62. // tgxUITips.show(err);
  63. // EventDispatcher.instance.emit(ADEvent.REWARD_VIDEO_ERROR);
  64. // AudioMgr.inst.resume();
  65. }
  66. });
  67. }
  68. }