ATInterstitialJSSDK.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Import platform-specific interstitial SDKs
  2. import ATiOSInterstitialJS from "./iOS/ATiOSInterstitialJS";
  3. import ATAndroidInterstitialJS from "./Android/ATAndroidInterstitialJS";
  4. // Define platform bridge initialization
  5. const initPlatformBridge = (): any => {
  6. if (cc.sys.os === cc.sys.OS_IOS) {
  7. return ATiOSInterstitialJS;
  8. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  9. return ATAndroidInterstitialJS;
  10. }
  11. };
  12. // Initialize platform bridge
  13. const platformBridge = initPlatformBridge();
  14. // Define listener interface for ad callbacks
  15. interface IAdListener {
  16. onInterstitialAdLoaded?: (placementId: string) => void;
  17. onInterstitialAdLoadFail?: (placementId: string, errorInfo: any) => void;
  18. onInterstitialAdShow?: (placementId: string, callbackInfo: any) => void;
  19. onInterstitialAdStartPlayingVideo?: (placementId: string, callbackInfo: any) => void;
  20. onInterstitialAdEndPlayingVideo?: (placementId: string, callbackInfo: any) => void;
  21. onInterstitialAdFailedToPlayVideo?: (placementId: string, errorInfo: any) => void;
  22. onInterstitialAdFailedToShow?: (placementId: string) => void;
  23. onInterstitialAdClose?: (placementId: string, callbackInfo: any) => void;
  24. onInterstitialAdClick?: (placementId: string, callbackInfo: any) => void;
  25. }
  26. // Define interstitial SDK class
  27. const ATInterstitialSDK = {
  28. UseRewardedVideoAsInterstitial: "UseRewardedVideoAsInterstitial",
  29. ATInterstitialListener: {
  30. developerCallback: null as IAdListener | null,
  31. onInterstitialAdLoaded(placementId: string) {
  32. this.developerCallback?.onInterstitialAdLoaded?.(placementId);
  33. },
  34. onInterstitialAdLoadFail(placementId: string, errorInfo: any) {
  35. this.developerCallback?.onInterstitialAdLoadFail?.(placementId, errorInfo);
  36. },
  37. onInterstitialAdShow(placementId: string, callbackInfo: any) {
  38. this.developerCallback?.onInterstitialAdShow?.(placementId, callbackInfo);
  39. },
  40. onInterstitialAdStartPlayingVideo(placementId: string, callbackInfo: any) {
  41. this.developerCallback?.onInterstitialAdStartPlayingVideo?.(placementId, callbackInfo);
  42. },
  43. onInterstitialAdEndPlayingVideo(placementId: string, callbackInfo: any) {
  44. this.developerCallback?.onInterstitialAdEndPlayingVideo?.(placementId, callbackInfo);
  45. },
  46. onInterstitialAdFailedToPlayVideo(placementId: string, errorInfo: any) {
  47. this.developerCallback?.onInterstitialAdFailedToPlayVideo?.(placementId, errorInfo);
  48. },
  49. onInterstitialAdFailedToShow(placementId: string) {
  50. this.developerCallback?.onInterstitialAdFailedToShow?.(placementId);
  51. },
  52. onInterstitialAdClose(placementId: string, callbackInfo: any) {
  53. this.developerCallback?.onInterstitialAdClose?.(placementId, callbackInfo);
  54. },
  55. onInterstitialAdClick(placementId: string, callbackInfo: any) {
  56. this.developerCallback?.onInterstitialAdClick?.(placementId, callbackInfo);
  57. }
  58. },
  59. loadInterstitial(placementId: string, settings: Record<string, any> = {}) {
  60. if (platformBridge) {
  61. platformBridge.loadInterstitial(placementId, JSON.stringify(settings));
  62. } else {
  63. cc.log("You must run on Android or iOS.");
  64. }
  65. },
  66. setAdListener(listener: IAdListener) {
  67. const eventJSON: Record<string, string> = {};
  68. eventJSON[LoadedCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdLoaded";
  69. eventJSON[LoadFailCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdLoadFail";
  70. eventJSON[PlayStartCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdStartPlayingVideo";
  71. eventJSON[PlayEndCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdEndPlayingVideo";
  72. eventJSON[PlayFailCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdFailedToPlayVideo";
  73. eventJSON[CloseCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdClose";
  74. eventJSON[ClickCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdClick";
  75. eventJSON[ShowCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdShow";
  76. eventJSON[ShowFailCallbackKey] = "ATInterstitialJSSDK.ATInterstitialListener.onInterstitialAdFailedToShow";
  77. if (platformBridge) {
  78. platformBridge.setAdListener(JSON.stringify(eventJSON));
  79. } else {
  80. cc.log("You must run on Android or iOS.");
  81. }
  82. this.ATInterstitialListener.developerCallback = listener;
  83. },
  84. hasAdReady(placementId: string): boolean {
  85. if (platformBridge) {
  86. return platformBridge.hasAdReady(placementId);
  87. } else {
  88. cc.log("You must run on Android or iOS.");
  89. }
  90. return false;
  91. },
  92. checkAdStatus(placementId: string): string {
  93. if (platformBridge) {
  94. return platformBridge.checkAdStatus(placementId);
  95. } else {
  96. cc.log("You must run on Android or iOS.");
  97. }
  98. return "";
  99. },
  100. showAd(placementId: string) {
  101. if (platformBridge) {
  102. platformBridge.showAd(placementId);
  103. } else {
  104. cc.log("You must run on Android or iOS.");
  105. }
  106. },
  107. showAdInScenario(placementId: string, scenario: string = "") {
  108. if (platformBridge) {
  109. platformBridge.showAdInScenario(placementId, scenario);
  110. } else {
  111. cc.log("You must run on Android or iOS.");
  112. }
  113. }
  114. };
  115. // Define callback keys
  116. const LoadedCallbackKey = "InterstitialLoaded";
  117. const LoadFailCallbackKey = "InterstitialLoadFail";
  118. const PlayStartCallbackKey = "InterstitialPlayStart";
  119. const PlayEndCallbackKey = "InterstitialPlayEnd";
  120. const PlayFailCallbackKey = "InterstitialPlayFail";
  121. const CloseCallbackKey = "InterstitialClose";
  122. const ClickCallbackKey = "InterstitialClick";
  123. const ShowCallbackKey = "InterstitialAdShow";
  124. const ShowFailCallbackKey = "InterstitialAdShowFail";
  125. // Export the SDK to the global window object
  126. (window as any).ATInterstitialJSSDK = ATInterstitialSDK;
  127. export default ATInterstitialSDK;