AdvertMgr.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. if (!this.adInstance || !this.openAd) {
  22. cb && cb();
  23. return;
  24. }
  25. this.adInstance.interstitialAd({
  26. beforeAd() {
  27. console.log('The ad starts playing');
  28. },
  29. afterAd() {
  30. console.log('The ad ends playing');
  31. },
  32. error(err) {
  33. // console.log('The ad failed to load');
  34. // tgxUITips.show(err);
  35. // AudioMgr.inst.resume();
  36. }
  37. });
  38. }
  39. /** 显示激励广告*/
  40. showReawardVideo(cb?: () => void): void {
  41. if (!this.adInstance || !this.openAd) {
  42. cb && cb();
  43. return;
  44. }
  45. this.adInstance.rewardAd({
  46. beforeAd() {
  47. console.log('The ad starts playing, and the game should pause.');
  48. // AudioMgr.inst.pause();
  49. },
  50. adDismissed() {
  51. console.log('Player dismissed the ad before completion.');
  52. // AudioMgr.inst.resume();
  53. },
  54. adViewed() {
  55. console.log('Ad was viewed and closed.');
  56. if (cb) cb();
  57. // AudioMgr.inst.resume();
  58. },
  59. error(err: any) {
  60. // console.log(`激励广告错误:${err}`);
  61. // tgxUITips.show(err);
  62. // EventDispatcher.instance.emit(ADEvent.REWARD_VIDEO_ERROR);
  63. // AudioMgr.inst.resume();
  64. }
  65. });
  66. }
  67. }