ATNativeTSSDK.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import { Component } from "cc";
  2. import {ATiOSNativeTS} from "./iOS/ATiOSNativeTS"
  3. import {ATAndroidNativeTS} from "./Android/ATAndroidNativeTS"
  4. var initPlatformBridge = function () {
  5. if (cc.sys.os === cc.sys.OS.IOS) {
  6. return ATiOSNativeTS;
  7. } else if (cc.sys.os === cc.sys.OS.ANDROID) {
  8. return ATAndroidNativeTS;
  9. }
  10. };
  11. var platformBridge = initPlatformBridge();
  12. export const ATNativeTSSDK = {
  13. ATNativeListener: {
  14. developerCallback: null,
  15. onNativeAdLoaded: function (placementId) {
  16. if (this.developerCallback != null && this.developerCallback.onNativeAdLoaded != null && undefined != this.developerCallback.onNativeAdLoaded) {
  17. this.developerCallback.onNativeAdLoaded(placementId);
  18. }
  19. },
  20. onNativeAdLoadFail: function (placementId, errorInfo) {
  21. if (this.developerCallback != null && this.developerCallback.onNativeAdLoadFail != null && undefined != this.developerCallback.onNativeAdLoadFail) {
  22. this.developerCallback.onNativeAdLoadFail(placementId, errorInfo);
  23. }
  24. },
  25. onNativeAdShow: function (placementId, callbackInfo) {
  26. if (this.developerCallback != null && this.developerCallback.onNativeAdShow != null && undefined != this.developerCallback.onNativeAdShow) {
  27. this.developerCallback.onNativeAdShow(placementId, callbackInfo);
  28. }
  29. },
  30. onNativeAdClick: function (placementId, callbackInfo) {
  31. if (this.developerCallback != null && this.developerCallback.onNativeAdClick != null && undefined != this.developerCallback.onNativeAdClick) {
  32. this.developerCallback.onNativeAdClick(placementId, callbackInfo);
  33. }
  34. },
  35. onNativeAdVideoStart: function (placementId) {
  36. if (this.developerCallback != null && this.developerCallback.onNativeAdVideoStart != null && undefined != this.developerCallback.onNativeAdVideoStart) {
  37. this.developerCallback.onNativeAdVideoStart(placementId,);
  38. }
  39. },
  40. onNativeAdVideoEnd: function (placementId) {
  41. if (this.developerCallback != null && this.developerCallback.onNativeAdVideoEnd != null && undefined != this.developerCallback.onNativeAdVideoEnd) {
  42. this.developerCallback.onNativeAdVideoEnd(placementId);
  43. }
  44. },
  45. onNativeAdCloseButtonTapped: function (placementId, callbackInfo) {
  46. if (this.developerCallback != null && this.developerCallback.onNativeAdCloseButtonTapped != null && undefined != this.developerCallback.onNativeAdCloseButtonTapped) {
  47. this.developerCallback.onNativeAdCloseButtonTapped(placementId, callbackInfo);
  48. }
  49. },
  50. //added v5.8.10
  51. onAdSourceBiddingAttempt: function (placementId, callbackInfo) {
  52. if (this.developerCallback != null && this.developerCallback.onAdSourceBiddingAttempt != null && undefined != this.developerCallback.onAdSourceBiddingAttempt) {
  53. this.developerCallback.onAdSourceBiddingAttempt(placementId, callbackInfo);
  54. }
  55. },
  56. onAdSourceBiddingFilled: function (placementId, callbackInfo) {
  57. if (this.developerCallback != null && this.developerCallback.onAdSourceBiddingFilled != null && undefined != this.developerCallback.onAdSourceBiddingFilled) {
  58. this.developerCallback.onAdSourceBiddingFilled(placementId, callbackInfo);
  59. }
  60. },
  61. onAdSourceBiddingFail: function (placementId, errorInfo, callbackInfo) {
  62. if (this.developerCallback != null && this.developerCallback.onAdSourceBiddingFail != null && undefined != this.developerCallback.onAdSourceBiddingFail) {
  63. this.developerCallback.onAdSourceBiddingFail(placementId, errorInfo, callbackInfo);
  64. }
  65. },
  66. onAdSourceAttemp: function (placementId, callbackInfo) {
  67. if (this.developerCallback != null && this.developerCallback.onAdSourceAttemp != null && undefined != this.developerCallback.onAdSourceAttemp) {
  68. this.developerCallback.onAdSourceAttemp(placementId, callbackInfo);
  69. }
  70. },
  71. onAdSourceLoadFilled: function (placementId, callbackInfo) {
  72. if (this.developerCallback != null && this.developerCallback.onAdSourceLoadFilled != null && undefined != this.developerCallback.onAdSourceLoadFilled) {
  73. this.developerCallback.onAdSourceLoadFilled(placementId, callbackInfo);
  74. }
  75. },
  76. onAdSourceLoadFail: function (placementId, errorInfo, callbackInfo) {
  77. if (this.developerCallback != null && this.developerCallback.onAdSourceLoadFail != null && undefined != this.developerCallback.onAdSourceLoadFail) {
  78. this.developerCallback.onAdSourceLoadFail(placementId, errorInfo, callbackInfo);
  79. }
  80. }
  81. },
  82. loadNative: function (placementId, settings = {}) {
  83. if (undefined != platformBridge && platformBridge != null) {
  84. platformBridge.loadNative(placementId, JSON.stringify(settings));
  85. } else {
  86. cc.log("You must run on Android or iOS.");
  87. }
  88. },
  89. setAdListener: function (listener) {
  90. var eventJSON = {};
  91. eventJSON[LoadedCallbackKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdLoaded",
  92. eventJSON[LoadFailCallbackKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdLoadFail",
  93. eventJSON[CloseCallbackKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdCloseButtonTapped",
  94. eventJSON[ClickCallbackKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdClick",
  95. eventJSON[ShowCallbackKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdShow",
  96. eventJSON[VideoStartKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdVideoStart",
  97. eventJSON[VideoEndKey] = "ATNativeTSSDK.ATNativeListener.onNativeAdVideoEnd",
  98. //added v5.8.10
  99. eventJSON[BiddingAttempt] = "ATNativeTSSDK.ATNativeListener.onAdSourceBiddingAttempt",
  100. eventJSON[BiddingFilled] = "ATNativeTSSDK.ATNativeListener.onAdSourceBiddingFilled",
  101. eventJSON[BiddingFail] = "ATNativeTSSDK.ATNativeListener.onAdSourceBiddingFail",
  102. eventJSON[Attemp] = "ATNativeTSSDK.ATNativeListener.onAdSourceAttemp",
  103. eventJSON[LoadFilled] = "ATNativeTSSDK.ATNativeListener.onAdSourceLoadFilled",
  104. eventJSON[LoadFail] = "ATNativeTSSDK.ATNativeListener.onAdSourceLoadFail"
  105. if (undefined != platformBridge && platformBridge != null) {
  106. platformBridge.setAdListener(JSON.stringify(eventJSON));
  107. } else {
  108. cc.log("You must run on Android or iOS.");
  109. }
  110. this.ATNativeListener.developerCallback = listener;
  111. },
  112. hasAdReady: function (placementId) {
  113. if (undefined != platformBridge && platformBridge != null) {
  114. return platformBridge.hasAdReady(placementId);
  115. } else {
  116. cc.log("You must run on Android or iOS.");
  117. }
  118. return false;
  119. },
  120. checkAdStatus: function (placementId) {
  121. if (undefined != platformBridge && platformBridge != null) {
  122. return platformBridge.checkAdStatus(placementId);
  123. } else {
  124. cc.log("You must run on Android or iOS.");
  125. }
  126. return "";
  127. },
  128. showAd: function (placementId, adViewProperty) {
  129. if (undefined != platformBridge && platformBridge != null) {
  130. platformBridge.showAd(placementId, JSON.stringify(adViewProperty.getAdViewProperty()));
  131. } else {
  132. cc.log("You must run on Android or iOS.");
  133. }
  134. },
  135. showAdInScenario: function (placementId, adViewProperty, scenario = "") {
  136. if (undefined != platformBridge && platformBridge != null) {
  137. platformBridge.showAdInScenario(placementId, JSON.stringify(adViewProperty.getAdViewProperty()), scenario);
  138. } else {
  139. cc.log("You must run on Android or iOS.");
  140. }
  141. },
  142. entryAdScenario: function (placementId, scenario = "") {
  143. if (undefined != platformBridge && platformBridge != null) {
  144. platformBridge.entryAdScenario(placementId, scenario);
  145. } else {
  146. cc.log("You must run on Android or iOS.");
  147. }
  148. },
  149. removeAd: function (placementId) {
  150. if (undefined != platformBridge && platformBridge != null) {
  151. platformBridge.removeAd(placementId);
  152. } else {
  153. cc.log("You must run on Android or iOS.");
  154. }
  155. },
  156. createLoadAdSize: function (width, height) {
  157. var loadAdSize = {};
  158. loadAdSize["width"] = width;
  159. loadAdSize["height"] = height;
  160. return loadAdSize;
  161. },
  162. };
  163. export class AdViewProperty extends Component {
  164. parent = null
  165. appIcon = null
  166. mainImage = null
  167. title = null
  168. desc = null
  169. adLogo = null
  170. cta = null
  171. rating = null
  172. dislike = null
  173. elements = null
  174. createItemViewProperty (x, y, width, height, backgroundColor, textColor, textSize, isCustomClick = false) {
  175. var itemProperty = {};
  176. itemProperty["x"] = x;
  177. itemProperty["y"] = y;
  178. itemProperty["width"] = width;
  179. itemProperty["height"] = height;
  180. itemProperty["backgroundColor"] = backgroundColor;
  181. itemProperty["textColor"] = textColor;
  182. itemProperty["textSize"] = textSize;
  183. itemProperty["isCustomClick"] = isCustomClick;
  184. return itemProperty;
  185. }
  186. getAdViewProperty() {
  187. var nativeViewProperty = {};
  188. if (this.parent != null) {
  189. nativeViewProperty["parent"] = this.parent;
  190. }
  191. if (this.appIcon != null) {
  192. nativeViewProperty["icon"] = this.appIcon;
  193. }
  194. if (this.mainImage != null) {
  195. nativeViewProperty["mainImage"] = this.mainImage;
  196. }
  197. if (this.title != null) {
  198. nativeViewProperty["title"] = this.title;
  199. }
  200. if (this.desc != null) {
  201. nativeViewProperty["desc"] = this.desc;
  202. }
  203. if (this.adLogo != null) {
  204. nativeViewProperty["adLogo"] = this.adLogo;
  205. }
  206. if (this.cta != null) {
  207. nativeViewProperty["cta"] = this.cta;
  208. }
  209. if (this.rating != null) {
  210. nativeViewProperty["rating"] = this.rating;
  211. }
  212. if (this.dislike != null) {
  213. nativeViewProperty["dislike"] = this.dislike;
  214. }
  215. if (this.elements != null) {
  216. nativeViewProperty["elements"] = this.elements
  217. }
  218. return nativeViewProperty;
  219. }
  220. }
  221. const LoadedCallbackKey = "NativeLoaded";
  222. const LoadFailCallbackKey = "NativeLoadFail";
  223. const CloseCallbackKey = "NativeCloseButtonTapped";
  224. const ClickCallbackKey = "NativeClick";
  225. const ShowCallbackKey = "NativeShow";
  226. const VideoStartKey = "NativeVideoStart";
  227. const VideoEndKey = "NativeVideoEnd";
  228. const BiddingAttempt = "NativeBiddingAttempt";
  229. const BiddingFilled = "NativeBiddingFilled";
  230. const BiddingFail = "NativeBiddingFail";
  231. const Attemp = "NativeAttemp";
  232. const LoadFilled = "NativeLoadFilled";
  233. const LoadFail = "NativeLoadFail";
  234. window["ATNativeTSSDK"] = ATNativeTSSDK;