ATNativeJSSDK.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import ATAndroidNativeJS from "./Android/ATAndroidNativeJS";
  2. import ATiOSNativeJS from "./iOS/ATiOSNativeJS";
  3. type PlatformBridge = {
  4. loadNative: (placementId: string, settings: string) => void;
  5. setAdListener: (eventJSON: string) => void;
  6. hasAdReady: (placementId: string) => boolean;
  7. checkAdStatus: (placementId: string) => string;
  8. showAd: (placementId: string, adViewProperty: string) => void;
  9. showAdInScenario: (placementId: string, adViewProperty: string, scenario?: string) => void;
  10. removeAd: (placementId: string) => void;
  11. };
  12. let platformBridge: PlatformBridge | null = null;
  13. const initPlatformBridge = (): PlatformBridge | null => {
  14. if (cc.sys.os === cc.sys.OS_IOS) {
  15. return null;
  16. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  17. return ATAndroidNativeJS;
  18. }
  19. return null;
  20. };
  21. platformBridge = initPlatformBridge();
  22. interface DeveloperCallback {
  23. onNativeAdLoaded?: (placementId: string) => void;
  24. onNativeAdLoadFail?: (placementId: string, errorInfo: any) => void;
  25. onNativeAdShow?: (placementId: string, callbackInfo: any) => void;
  26. onNativeAdClick?: (placementId: string, callbackInfo: any) => void;
  27. onNativeAdVideoStart?: (placementId: string) => void;
  28. onNativeAdVideoEnd?: (placementId: string) => void;
  29. onNativeAdCloseButtonTapped?: (placementId: string, callbackInfo: any) => void;
  30. }
  31. class ATNativeListener {
  32. developerCallback: DeveloperCallback | null = null;
  33. onNativeAdLoaded(placementId: string) {
  34. this.developerCallback?.onNativeAdLoaded?.(placementId);
  35. }
  36. onNativeAdLoadFail(placementId: string, errorInfo: any) {
  37. this.developerCallback?.onNativeAdLoadFail?.(placementId, errorInfo);
  38. }
  39. onNativeAdShow(placementId: string, callbackInfo: any) {
  40. this.developerCallback?.onNativeAdShow?.(placementId, callbackInfo);
  41. }
  42. onNativeAdClick(placementId: string, callbackInfo: any) {
  43. this.developerCallback?.onNativeAdClick?.(placementId, callbackInfo);
  44. }
  45. onNativeAdVideoStart(placementId: string) {
  46. this.developerCallback?.onNativeAdVideoStart?.(placementId);
  47. }
  48. onNativeAdVideoEnd(placementId: string) {
  49. this.developerCallback?.onNativeAdVideoEnd?.(placementId);
  50. }
  51. onNativeAdCloseButtonTapped(placementId: string, callbackInfo: any) {
  52. this.developerCallback?.onNativeAdCloseButtonTapped?.(placementId, callbackInfo);
  53. }
  54. }
  55. const LoadedCallbackKey = "NativeLoaded";
  56. const LoadFailCallbackKey = "NativeLoadFail";
  57. const CloseCallbackKey = "NativeCloseButtonTapped";
  58. const ClickCallbackKey = "NativeClick";
  59. const ShowCallbackKey = "NativeShow";
  60. const VideoStartKey = "NativeVideoStart";
  61. const VideoEndKey = "NativeVideoEnd";
  62. class ATNativeSDK {
  63. ATNativeListener: ATNativeListener = new ATNativeListener();
  64. loadNative(placementId: string, settings: Record<string, any> = {}) {
  65. if (platformBridge) {
  66. platformBridge.loadNative(placementId, JSON.stringify(settings));
  67. } else {
  68. cc.log("You must run on Android or iOS.");
  69. }
  70. }
  71. setAdListener(listener: DeveloperCallback) {
  72. const eventJSON: Record<string, string> = {
  73. [LoadedCallbackKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdLoaded",
  74. [LoadFailCallbackKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdLoadFail",
  75. [CloseCallbackKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdCloseButtonTapped",
  76. [ClickCallbackKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdClick",
  77. [ShowCallbackKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdShow",
  78. [VideoStartKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdVideoStart",
  79. [VideoEndKey]: "ATNativeJSSDK.ATNativeListener.onNativeAdVideoEnd"
  80. };
  81. if (platformBridge) {
  82. platformBridge.setAdListener(JSON.stringify(eventJSON));
  83. } else {
  84. cc.log("You must run on Android or iOS.");
  85. }
  86. this.ATNativeListener.developerCallback = listener;
  87. }
  88. hasAdReady(placementId: string): boolean {
  89. if (platformBridge) {
  90. return platformBridge.hasAdReady(placementId);
  91. } else {
  92. cc.log("You must run on Android or iOS.");
  93. }
  94. return false;
  95. }
  96. checkAdStatus(placementId: string): string {
  97. if (platformBridge) {
  98. return platformBridge.checkAdStatus(placementId);
  99. } else {
  100. cc.log("You must run on Android or iOS.");
  101. }
  102. return "";
  103. }
  104. showAd(placementId: string, adViewProperty: AdViewProperty) {
  105. if (platformBridge) {
  106. platformBridge.showAd(placementId, JSON.stringify(adViewProperty.getAdViewProperty()));
  107. } else {
  108. cc.log("You must run on Android or iOS.");
  109. }
  110. }
  111. showAdInScenario(placementId: string, adViewProperty: AdViewProperty, scenario: string = "") {
  112. if (platformBridge) {
  113. platformBridge.showAdInScenario(placementId, JSON.stringify(adViewProperty.getAdViewProperty()), scenario);
  114. } else {
  115. cc.log("You must run on Android or iOS.");
  116. }
  117. }
  118. removeAd(placementId: string) {
  119. if (platformBridge) {
  120. platformBridge.removeAd(placementId);
  121. } else {
  122. cc.log("You must run on Android or iOS.");
  123. }
  124. }
  125. createLoadAdSize(width: number, height: number): Record<string, number> {
  126. return { width, height };
  127. }
  128. }
  129. class AdViewProperty {
  130. parent: string | null = null;
  131. appIcon: string | null = null;
  132. mainImage: string | null = null;
  133. title: string | null = null;
  134. desc: string | null = null;
  135. adLogo: string | null = null;
  136. cta: string | null = null;
  137. rating: number | null = null;
  138. dislike: number | null = null;
  139. createItemViewProperty(
  140. x: number,
  141. y: number,
  142. width: number,
  143. height: number,
  144. backgroundColor: string,
  145. textColor: string,
  146. textSize: number,
  147. isCustomClick: boolean = false
  148. ): Record<string, any> {
  149. return {
  150. x,
  151. y,
  152. width,
  153. height,
  154. backgroundColor,
  155. textColor,
  156. textSize,
  157. isCustomClick
  158. };
  159. }
  160. getAdViewProperty(): Record<string, any> {
  161. const nativeViewProperty: Record<string, any> = {};
  162. if (this.parent != null) {
  163. nativeViewProperty["parent"] = this.parent;
  164. }
  165. if (this.appIcon != null) {
  166. nativeViewProperty["icon"] = this.appIcon;
  167. }
  168. if (this.mainImage != null) {
  169. nativeViewProperty["mainImage"] = this.mainImage;
  170. }
  171. if (this.title != null) {
  172. nativeViewProperty["title"] = this.title;
  173. }
  174. if (this.desc != null) {
  175. nativeViewProperty["desc"] = this.desc;
  176. }
  177. if (this.adLogo != null) {
  178. nativeViewProperty["adLogo"] = this.adLogo;
  179. }
  180. if (this.cta != null) {
  181. nativeViewProperty["cta"] = this.cta;
  182. }
  183. if (this.rating != null) {
  184. nativeViewProperty["rating"] = this.rating;
  185. }
  186. if (this.dislike != null) {
  187. nativeViewProperty["dislike"] = this.dislike;
  188. }
  189. return nativeViewProperty;
  190. }
  191. }
  192. (window as any).ATNativeJSSDK = new ATNativeSDK();
  193. export default ATNativeSDK;