Ad.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass("AdConfigBase")
  3. class AdConfigBase {
  4. @property({ displayName: "广告Id" })
  5. codeId: string = "";
  6. };
  7. @ccclass("RewardedVideoAdConfig")
  8. class RewardedVideoAdConfig extends AdConfigBase {
  9. };
  10. @ccclass("BannerAdConfig")
  11. class BannerAdConfig extends AdConfigBase {
  12. @property({ displayName: "宽度" })
  13. width: number = 500;
  14. @property({ displayName: "高度" })
  15. height: number = 300;
  16. };
  17. @ccclass("InterstitialAdConfig")
  18. class InterstitialAdConfig extends AdConfigBase {
  19. };
  20. @ccclass("SplashAdConfig")
  21. class SplashAdConfig extends AdConfigBase {
  22. };
  23. @ccclass("Ad")
  24. export class Ad {
  25. @property({ displayName: "应用appId" })
  26. appId: string = "";
  27. @property({ type: BannerAdConfig, displayName: "Banner广告" })
  28. bannerAdConfig: BannerAdConfig = new BannerAdConfig();
  29. @property({ type: RewardedVideoAdConfig, displayName: "激励视频广告" })
  30. rewardedVideoAdConfig: RewardedVideoAdConfig = new RewardedVideoAdConfig();
  31. @property({ type: SplashAdConfig, displayName: "开屏广告" })
  32. splashAdConfig: SplashAdConfig = new SplashAdConfig();
  33. @property({ type: InterstitialAdConfig, displayName: "插屏广告" })
  34. interstitialAdConfig: InterstitialAdConfig = new InterstitialAdConfig();
  35. private rewardAdCallback: any;
  36. private rewardAdCallbackObj: any;
  37. private static onNativeCallback: any;
  38. private static onNativeCallbackObj: any;
  39. sendToNative(arg0: string, arg1?: string) {
  40. console.warn("sendToNative",arg0,arg1);
  41. // native.bridge.sendToNative(arg0, arg1);
  42. jsb.reflection.callStaticMethod("com/yiyuancoder/ttsdk/TTSdkComponent", "sendToNative", "(Ljava/lang/String;Ljava/lang/String;)V", arg0, arg1);
  43. // jsb.reflection.callStaticMethod("com/example/testchaunshanjia/TTSdkComponent", "sendToNative", "(Ljava/lang/String;Ljava/lang/String;)V", arg0, arg1);
  44. }
  45. public static onNative(arg0: string, arg1: string) {
  46. if (this.onNativeCallback != null) {
  47. this.onNativeCallback.call(this.onNativeCallbackObj, arg0, arg1);
  48. this.onNativeCallback = null;
  49. this.onNativeCallbackObj = null;
  50. }
  51. }
  52. async init() {
  53. return new Promise((resolve, reject) => {
  54. // native.bridge.onNative = (arg0: string, arg1: string): void => {
  55. // if (arg0 == 'onInit') {
  56. // resolve(arg1 == "true");
  57. // }
  58. // }
  59. Ad.onNativeCallbackObj = this;
  60. Ad.onNativeCallback = (arg0: string, arg1: string) => {
  61. if (arg0 == 'onInit') {
  62. resolve(arg1 == "true");
  63. }
  64. }
  65. this.sendToNative("init", JSON.stringify({ appId: this.appId }));
  66. })
  67. }
  68. /**
  69. * 显示Banner广告
  70. */
  71. showBannerAd(show: boolean): void {
  72. let width = this.bannerAdConfig.width;
  73. let height = this.bannerAdConfig.height;
  74. let left = screen.width / 2 - width / 2;
  75. let top = screen.height - height;
  76. this.sendToNative("showBannerAd", JSON.stringify({ isShow: show, codeId: this.bannerAdConfig.codeId, left: left, top: top, width: width, height: height }));
  77. }
  78. /**
  79. * 播放激励视频广告
  80. */
  81. showRewardedVideoAd(rewardAdCallback: Function, rewardAdCallbackObj: any): void {
  82. this.rewardAdCallback = rewardAdCallback;
  83. this.rewardAdCallbackObj = rewardAdCallbackObj;
  84. let that = this;
  85. // native.bridge.onNative = (arg0: string, arg1: string): void => {
  86. // if (arg0 == 'onRewardAdCallback') {
  87. // that.onRewardAdCallback(arg1 == "true");
  88. // }
  89. // }
  90. Ad.onNativeCallbackObj = this;
  91. Ad.onNativeCallback = (arg0: string, arg1: string) => {
  92. if (arg0 == 'onRewardAdCallback') {
  93. //在这里改了,这判断了, 是false ,转bool
  94. if(arg1 == "false"){
  95. that.onRewardAdCallback(false);
  96. }else{
  97. // 这里字符串,走的字符串
  98. that.onRewardAdCallback1(arg1);
  99. }
  100. }
  101. }
  102. console.warn("sendToNative", this.rewardedVideoAdConfig.codeId)
  103. this.sendToNative("showRewardVideoAd", JSON.stringify({ codeId: this.rewardedVideoAdConfig.codeId }));
  104. }
  105. /**
  106. * 激励视频广告回调
  107. */
  108. onRewardAdCallback(success: boolean) {
  109. if (this.rewardAdCallback) {
  110. this.rewardAdCallback.call(this.rewardAdCallbackObj, success);
  111. this.rewardAdCallback = null;
  112. this.rewardAdCallbackObj = null;
  113. }
  114. }
  115. onRewardAdCallback1(success: string) {
  116. if (this.rewardAdCallback) {
  117. this.rewardAdCallback.call(this.rewardAdCallbackObj, success);
  118. this.rewardAdCallback = null;
  119. this.rewardAdCallbackObj = null;
  120. }
  121. }
  122. /**
  123. * 显示开屏广告
  124. */
  125. showSplashAd(): void {
  126. this.sendToNative("showSplashAd", JSON.stringify({ codeId: this.splashAdConfig.codeId, width: screen.width, height: screen.height }));
  127. }
  128. /**
  129. * 显示插屏广告
  130. */
  131. showFullScreenVideoAd() {
  132. // 在适合的场景显示插屏广告
  133. this.sendToNative("showFullScreenVideoAd", JSON.stringify({ codeId: this.interstitialAdConfig.codeId }));
  134. }
  135. };
  136. window.Ad = Ad;