AdMgr.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { InputBlock } from "../misc/InputBlock";
  2. import { HTML5, PREVIEW } from "cc/env";
  3. import { EventMgr } from "./EventMgr";
  4. import { Debug } from "../util/Debug";
  5. import { H5Ad } from "../sdk/h5/H5Ad";
  6. const Tag: string = 'AdMgr'
  7. const Inter_Ad_Cooling_Time: number = 30000
  8. export class AdMgr {
  9. private static ad: any = null
  10. private static lastInterAdShowTime: number = 0
  11. private _openAd: boolean = true;
  12. public get openAd(): boolean {
  13. return this._openAd;
  14. }
  15. public set openAd(value: boolean) {
  16. this._openAd = value;
  17. }
  18. public static init() {
  19. if (HTML5) this.ad = H5Ad
  20. this.ad?.init()
  21. }
  22. public static showBanner(): void {
  23. if (PREVIEW) {
  24. Debug.Log(Tag, '预览模式下展示Banner')
  25. return
  26. }
  27. this.ad?.showBanner()
  28. }
  29. public static hideBanner(): void {
  30. if (PREVIEW) {
  31. Debug.Log(Tag, '预览模式下隐藏Banner')
  32. return
  33. }
  34. this.ad?.hideBanner()
  35. }
  36. public static showRewardedVideo(onSuccess: Function, onFail?: Function, onError?: Function): void {
  37. if (PREVIEW) {
  38. Debug.Log(Tag, '预览模式下播放激励视频直接发放奖励')
  39. onSuccess && onSuccess(1)
  40. return
  41. }
  42. InputBlock.setActive(2)
  43. this.ad?.showRewardedVideo(onSuccess, onFail, onError)
  44. }
  45. public static showInterstitialAd(onSuccess?: Function, onFail?: Function, onError?: Function): void {
  46. if (PREVIEW) {
  47. Debug.Log(Tag, '预览模式下展示插屏')
  48. this.lastInterAdShowTime = Date.now()
  49. onSuccess && onSuccess()
  50. return
  51. }
  52. if (Date.now() - this.lastInterAdShowTime < Inter_Ad_Cooling_Time) {
  53. Debug.Log(Tag, '插屏冷却中')
  54. return
  55. }
  56. this.lastInterAdShowTime = Date.now()
  57. InputBlock.setActive(2)
  58. if (!onFail) onFail = () => { EventMgr.emit('OpenUI', 'UIToast', { data: '激励视频播放未完成' }) }
  59. if (!onError) onError = () => { EventMgr.emit('OpenUI', 'UIToast', { data: '暂时没有合适的广告' }) }
  60. this.ad?.showInterstitialAd(onSuccess, onFail, onError)
  61. }
  62. public static showPortalAd(): void {
  63. if (PREVIEW) {
  64. Debug.Log(Tag, '预览模式下展示互推盒子')
  65. return
  66. }
  67. InputBlock.setActive(2)
  68. this.ad?.showPortalAd()
  69. }
  70. }