Przeglądaj źródła

广告调用 最新方式

woso_javan 2 miesięcy temu
rodzic
commit
1b35e3d513

+ 25 - 47
assets/core_tgx/base/ad/AdvertMgr.ts

@@ -20,62 +20,40 @@ export class AdvertMgr {
 
     initilize(): void {
         this.adInstance = (window as any)['adInstance'];
-        console.log('ad sdk初始化');
-        console.log(this.adInstance);
+        // console.log('ad sdk初始化');
+        // console.log(this.adInstance);
     }
 
     /** 显示插屏广告*/
     showInterstitial(cb?: () => void): void {
-        if (!this.adInstance) {
-            cb && cb();
-            return;
-        }
+        try {
+            window['showAd']('Interstitial').then(() => {
 
-        this.adInstance.interstitialAd({
-            beforeAd() {
-                console.log('The ad starts playing');
-            },
-            afterAd() {
-                console.log('The ad ends playing');
-            },
-            error(err) {
-                // console.log('The ad failed to load');
-                tgxUITips.show(err)
-            }
-        });
+            }).catch(() => {
+                // GtagMgr.inst.doGameDot(GtagType.ad_error);
+            });
+        } catch (error) {
+            // tgxUITips.show('The ad failed to load')
+        }
     }
 
     /** 显示激励广告*/
     showReawardVideo(cb?: () => void): void {
-        if (!this.adInstance) {
-            cb && cb();
-            return;
-        }
+        try {
+            window["showAd"]('Reward')
+                .then(() => {
+                    if (cb) cb();
+                })
+                .catch(() => {
+                    tgxUITips.show('The ad failed to load');
+                    //上报信息
+                    // GtagMgr.inst.doGameDot(GtagType.ad_error);
+                    // cb && cb();
+                })
+            return
+        } catch (error) {
 
-        this.adInstance.rewardAd({
-            beforeAd() {
-                console.log('The ad starts playing, and the game should pause.');
-                EventDispatcher.instance.emit(ADEvent.REWARD_VIDEO_PLAY);
-                AudioMgr.inst.pause();
-            },
-            adDismissed() {
-                console.log('Player dismissed the ad before completion.');
-                EventDispatcher.instance.emit(ADEvent.REWARD_VIDEO_DISMISSED);
-                AudioMgr.inst.resume();
-            },
-            adViewed() {
-                console.log('Ad was viewed and closed.');
-                if (cb) cb();
-                EventDispatcher.instance.emit(ADEvent.REWARD_VIDEO_CLOSEED);
-                AudioMgr.inst.resume();
-            },
-            error(err: any) {
-                // console.log(`激励广告错误:${err}`);
-                tgxUITips.show(err);
-                EventDispatcher.instance.emit(ADEvent.REWARD_VIDEO_ERROR);
-                AudioMgr.inst.resume();
-                GtagMgr.inst.doGameDot(GtagType.ad_error);
-            }
-        });
+        }
+        tgxUITips.show('The ads failed to load');
     }
 }

+ 15 - 15
assets/scripts/typings/global.d.ts

@@ -1,20 +1,20 @@
 interface RoosterMoveCar {
-    // 声音控制
-    SoundMr: {
-        pauseAll(): void;
-        resumeAll(): void;
-    };
-    // 广告开关
-    TargetedAds: {
-        open(): void;
-        clos(): void;
-    };
-    //Fps设置
-    Fps: {
-        setfps(value: number): void;
-    };
+    // // 声音控制
+    // SoundMr: {
+    //     pauseAll(): void;
+    //     resumeAll(): void;
+    // };
+    // // 广告开关
+    // TargetedAds: {
+    //     open(): void;
+    //     clos(): void;
+    // };
+    // //Fps设置
+    // Fps: {
+    //     setfps(value: number): void;
+    // };
 }
 
 declare interface Window {
-    __woso: RoosterMoveCar;
+    // __woso: RoosterMoveCar;
 }

+ 5 - 0
assets/start/Start.ts

@@ -59,6 +59,11 @@ export class Start extends Component {
         // GlobalMgr.instance.timeTest();
 
         AdvertMgr.instance.initilize();
+        try {
+            window["__woso"]?.["TopCallback"]?.();
+        } catch (error) {
+
+        }
         if (!GlobalConfig.isDebug) {
             AdvertMgr.instance.showInterstitial();
         }

+ 23 - 31
build-templates/web-mobile/index.html

@@ -61,41 +61,33 @@
     System.import('./index.js').catch(function (err) { console.error(err); })
   </script> -->
 
+  <script src=""></script>
   <script>
-    // 动态加载外部脚本的方法
-    function loadScript(url) {
+    // 外部广告调用
+    window.showAd = function (type) {
       return new Promise((resolve, reject) => {
-        const script = document.createElement('script');
-        script.src = url;
-        script.async = true;
-        script.onload = () => resolve();
-        script.onerror = () => reject(new Error(`Failed to load script: ${url}`));
-        document.head.appendChild(script);
+        window.__woso.showAd(type)
+          .then((res) => {
+            // 为游戏返回广告结果
+            console.log(res);
+            resolve(res);
+          })
+          .catch((err) => {
+            console.log(err);
+            reject(err);
+          });
       });
+    };
+
+    // 回调测试
+    function TopCallback() {
+      //sound
+      if (window.__woso.SoundManager.isSound) {
+        window.youxi.SoundManager.resumeAll();
+      } else {
+        window.youxi.SoundManager.pauseAll();
+      }
     }
-
-    // 加载 PRESDK3.0.1.js,然后启动项目
-    loadScript('https://www.cpsense.com/public/PRESDK3.0.1.js')
-      .then(() => {
-        console.log('PRESDK3.0.1.js loaded successfully');
-        // 初始化广告 SDK
-        var adSdkConfig = {
-          el: document.querySelector("GameDiv"),
-          client: 'cpsense',
-          is_test: true
-        };
-        var adInstance = new adSdk(adSdkConfig);
-        window.adInstance = adInstance;
-
-        // 加载项目主入口
-        return System.import('./index.js');
-      })
-      .then(() => {
-        console.log('Game initialized successfully');
-      })
-      .catch(err => {
-        console.error('Error during initialization:', err);
-      });
   </script>
 
 </body>