123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- const { ccclass, property } = cc._decorator;
- @ccclass("AdConfigBase")
- class AdConfigBase {
- @property({ displayName: "广告Id" })
- codeId: string = "";
- };
- @ccclass("RewardedVideoAdConfig")
- class RewardedVideoAdConfig extends AdConfigBase {
- };
- @ccclass("BannerAdConfig")
- class BannerAdConfig extends AdConfigBase {
- @property({ displayName: "宽度" })
- width: number = 500;
- @property({ displayName: "高度" })
- height: number = 300;
- };
- @ccclass("InterstitialAdConfig")
- class InterstitialAdConfig extends AdConfigBase {
- };
- @ccclass("SplashAdConfig")
- class SplashAdConfig extends AdConfigBase {
- };
- @ccclass("Ad")
- export class Ad {
- @property({ displayName: "应用appId" })
- appId: string = "";
- @property({ type: BannerAdConfig, displayName: "Banner广告" })
- bannerAdConfig: BannerAdConfig = new BannerAdConfig();
- @property({ type: RewardedVideoAdConfig, displayName: "激励视频广告" })
- rewardedVideoAdConfig: RewardedVideoAdConfig = new RewardedVideoAdConfig();
- @property({ type: SplashAdConfig, displayName: "开屏广告" })
- splashAdConfig: SplashAdConfig = new SplashAdConfig();
- @property({ type: InterstitialAdConfig, displayName: "插屏广告" })
- interstitialAdConfig: InterstitialAdConfig = new InterstitialAdConfig();
- private rewardAdCallback: any;
- private rewardAdCallbackObj: any;
- private static onNativeCallback: any;
- private static onNativeCallbackObj: any;
- sendToNative(arg0: string, arg1?: string) {
- console.warn("sendToNative",arg0,arg1);
- // native.bridge.sendToNative(arg0, arg1);
- jsb.reflection.callStaticMethod("com/yiyuancoder/ttsdk/TTSdkComponent", "sendToNative", "(Ljava/lang/String;Ljava/lang/String;)V", arg0, arg1);
- // jsb.reflection.callStaticMethod("com/example/testchaunshanjia/TTSdkComponent", "sendToNative", "(Ljava/lang/String;Ljava/lang/String;)V", arg0, arg1);
- }
- public static onNative(arg0: string, arg1: string) {
- if (this.onNativeCallback != null) {
- this.onNativeCallback.call(this.onNativeCallbackObj, arg0, arg1);
- this.onNativeCallback = null;
- this.onNativeCallbackObj = null;
- }
- }
- async init() {
- return new Promise((resolve, reject) => {
- // native.bridge.onNative = (arg0: string, arg1: string): void => {
- // if (arg0 == 'onInit') {
- // resolve(arg1 == "true");
- // }
- // }
- Ad.onNativeCallbackObj = this;
- Ad.onNativeCallback = (arg0: string, arg1: string) => {
- if (arg0 == 'onInit') {
- resolve(arg1 == "true");
- }
- }
- this.sendToNative("init", JSON.stringify({ appId: this.appId }));
- })
- }
- /**
- * 显示Banner广告
- */
- showBannerAd(show: boolean): void {
- let width = this.bannerAdConfig.width;
- let height = this.bannerAdConfig.height;
- let left = screen.width / 2 - width / 2;
- let top = screen.height - height;
- this.sendToNative("showBannerAd", JSON.stringify({ isShow: show, codeId: this.bannerAdConfig.codeId, left: left, top: top, width: width, height: height }));
- }
- /**
- * 播放激励视频广告
- */
- showRewardedVideoAd(rewardAdCallback: Function, rewardAdCallbackObj: any): void {
- this.rewardAdCallback = rewardAdCallback;
- this.rewardAdCallbackObj = rewardAdCallbackObj;
- let that = this;
- // native.bridge.onNative = (arg0: string, arg1: string): void => {
- // if (arg0 == 'onRewardAdCallback') {
- // that.onRewardAdCallback(arg1 == "true");
- // }
- // }
- Ad.onNativeCallbackObj = this;
- Ad.onNativeCallback = (arg0: string, arg1: string) => {
- if (arg0 == 'onRewardAdCallback') {
- //在这里改了,这判断了, 是false ,转bool
- if(arg1 == "false"){
- that.onRewardAdCallback(false);
- }else{
- // 这里字符串,走的字符串
- that.onRewardAdCallback1(arg1);
- }
-
- }
- }
- console.warn("sendToNative", this.rewardedVideoAdConfig.codeId)
- this.sendToNative("showRewardVideoAd", JSON.stringify({ codeId: this.rewardedVideoAdConfig.codeId }));
- }
- /**
- * 激励视频广告回调
- */
- onRewardAdCallback(success: boolean) {
- if (this.rewardAdCallback) {
- this.rewardAdCallback.call(this.rewardAdCallbackObj, success);
- this.rewardAdCallback = null;
- this.rewardAdCallbackObj = null;
- }
- }
- onRewardAdCallback1(success: string) {
- if (this.rewardAdCallback) {
- this.rewardAdCallback.call(this.rewardAdCallbackObj, success);
- this.rewardAdCallback = null;
- this.rewardAdCallbackObj = null;
- }
- }
- /**
- * 显示开屏广告
- */
- showSplashAd(): void {
- this.sendToNative("showSplashAd", JSON.stringify({ codeId: this.splashAdConfig.codeId, width: screen.width, height: screen.height }));
- }
- /**
- * 显示插屏广告
- */
- showFullScreenVideoAd() {
- // 在适合的场景显示插屏广告
- this.sendToNative("showFullScreenVideoAd", JSON.stringify({ codeId: this.interstitialAdConfig.codeId }));
- }
- };
- window.Ad = Ad;
|