AdMgr.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = 'zh:AdMgr.ts'
  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. public static init() {
  12. if (HTML5) this.ad = H5Ad
  13. this.ad?.init()
  14. }
  15. public static showBanner(): void {
  16. Debug.Log(Tag, 'showBanner PREVIEW='+PREVIEW)
  17. if (PREVIEW) {
  18. Debug.Log(Tag, '预览模式下展示Banner')
  19. return
  20. }
  21. this.ad?.showBanner()
  22. }
  23. public static hideBanner(): void {
  24. Debug.Log(Tag, 'hideBanner PREVIEW='+PREVIEW)
  25. if (PREVIEW) {
  26. Debug.Log(Tag, '预览模式下隐藏Banner')
  27. return
  28. }
  29. this.ad?.hideBanner()
  30. }
  31. /**
  32. * 就直接发奖励
  33. * @param onSuccess
  34. * @param onFail
  35. * @param onError
  36. * @returns
  37. */
  38. public static showRewardedVideo(onSuccess: Function, onFail?: Function, onError?: Function): void {
  39. Debug.Log(Tag, '预览模式下播放激励视频直接发放奖励')
  40. onSuccess && onSuccess(1)
  41. return
  42. //下面是原始的代码
  43. // Debug.Log(Tag, 'showRewardedVideo PREVIEW='+PREVIEW)
  44. // if (PREVIEW) {
  45. // Debug.Log(Tag, '预览模式下播放激励视频直接发放奖励')
  46. // onSuccess && onSuccess(1)
  47. // return
  48. // }
  49. // InputBlock.setActive(2)
  50. // Debug.Log(Tag, 'showRewardedVideo 111')
  51. // this.ad?.showRewardedVideo(onSuccess, onFail, onError)
  52. // Debug.Log(Tag, 'showRewardedVideo 222')
  53. }
  54. public static showInterstitialAd(onSuccess?: Function, onFail?: Function, onError?: Function): void {
  55. Debug.Log(Tag, 'showInterstitialAd PREVIEW='+PREVIEW)
  56. if (PREVIEW) {
  57. Debug.Log(Tag, '预览模式下展示插屏')
  58. this.lastInterAdShowTime = Date.now()
  59. onSuccess && onSuccess()
  60. return
  61. }
  62. if (Date.now() - this.lastInterAdShowTime < Inter_Ad_Cooling_Time) {
  63. Debug.Log(Tag, '插屏冷却中')
  64. return
  65. }
  66. this.lastInterAdShowTime = Date.now()
  67. InputBlock.setActive(2)
  68. if (!onFail) onFail = () => { EventMgr.emit('OpenUI', 'UIToast', { data: '激励视频播放未完成' }) }
  69. if (!onError) onError = () => { EventMgr.emit('OpenUI', 'UIToast', { data: '暂时没有合适的广告' }) }
  70. this.ad?.showInterstitialAd(onSuccess, onFail, onError)
  71. }
  72. public static showPortalAd(): void {
  73. Debug.Log(Tag, 'showPortalAd PREVIEW='+PREVIEW)
  74. if (PREVIEW) {
  75. Debug.Log(Tag, '预览模式下展示互推盒子')
  76. return
  77. }
  78. InputBlock.setActive(2)
  79. this.ad?.showPortalAd()
  80. }
  81. }