From 333f9a26195e8932f8e5c2d41ba6b012a1d8bb7f Mon Sep 17 00:00:00 2001 From: huanghaipeng Date: Tue, 21 Oct 2025 19:52:28 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=84=E5=8A=A0=E8=BD=BD=E5=B9=BF=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/Scene/HomeScene.fire | 38 ++-- assets/Script/JiaZai.ts | 9 + assets/Script/Map.ts | 9 + assets/Script/Sdk/MiniGameSdk.ts | 288 ++++++++++++++++++++++---- assets/Script/module/Tool/GameTool.ts | 177 ++++++++++++---- 5 files changed, 415 insertions(+), 106 deletions(-) diff --git a/assets/Scene/HomeScene.fire b/assets/Scene/HomeScene.fire index c2d17a8..b4bb10b 100644 --- a/assets/Scene/HomeScene.fire +++ b/assets/Scene/HomeScene.fire @@ -173,61 +173,61 @@ "__uuid__": "998a24bd-ffa0-4f96-bc39-a9789f42bd9b" }, "adhesive": { - "__uuid__": "e0f2c22c-d8dd-4dad-9fa2-7b73d15a5518" + "__uuid__": "df157ae8-c751-4be5-882b-a7ac2ef6a8b7" }, "freezeBlock": { - "__uuid__": "9a056601-12c6-45dd-abde-a847d6f15247" + "__uuid__": "9a40b6dd-fe63-4c9b-889b-a4c47ed46cbb" }, "freezeDoor": { - "__uuid__": "20367986-5fa0-4558-9c73-861dc7bc1293" + "__uuid__": "8a308761-8192-4597-8315-65591ef9b4db" }, "hammer": { - "__uuid__": "a25b43cf-deba-4061-a5da-e87aa343269d" + "__uuid__": "4a1713d2-c65c-448a-8aba-a29368062363" }, "lockBlock1": { - "__uuid__": "227cb730-4de8-41e2-93d1-f9e8cfdffd11" + "__uuid__": "c21b69c0-ab69-472e-9881-72bf2dbb9069" }, "lockBlock2": { - "__uuid__": "2cfcd2ad-bb5d-47c0-be0a-3a8135d89c1f" + "__uuid__": "9dc0d948-aa37-4930-b35e-adf8e89cfb81" }, "lockDoor": { - "__uuid__": "25ca59d5-b522-472d-83e4-de16fd1cdd5c" + "__uuid__": "8294e950-449c-403e-a350-740a4d80fb3e" }, "magic1": { - "__uuid__": "b8ec297a-cc2d-4a82-ac2c-345db075ce84" + "__uuid__": "def9458b-4365-40fe-bb61-c7ef3383316f" }, "magic2": { - "__uuid__": "e09b4699-ba82-4ac8-9894-d8b88640d041" + "__uuid__": "8629071d-cb1a-42db-b45a-682c1c16ab51" }, "simpleColor": { - "__uuid__": "6f213771-d0ef-45ba-b87d-e878f291886b" + "__uuid__": "a5b58c9e-74be-4c0f-bc1e-10155ecc43eb" }, "stacking": { - "__uuid__": "2c418573-4beb-49f8-bafe-6a9ae7906195" + "__uuid__": "3aee0a86-116d-46eb-97ed-bbdb6d62e325" }, "starBlock": { - "__uuid__": "643d1ba5-fd6f-45ab-928e-c271afddbcef" + "__uuid__": "c18157da-f131-4808-afc9-dd4b93da0921" }, "timePause1": { - "__uuid__": "f5c8c01d-aa75-4518-a7c6-82599ad31e58" + "__uuid__": "e12beab1-71a4-43cc-a83c-0728fd6b54e0" }, "timePause2": { - "__uuid__": "37c7f2a9-e0d2-4f3e-9ed6-1ad2ef1862c0" + "__uuid__": "fe65680a-ea0e-40df-83ce-9f6d9ff33ea6" }, "hit1": { - "__uuid__": "e16fd77e-9bbb-4bd1-b149-e1dd5a409c8f" + "__uuid__": "73cf4586-058b-46e0-8b76-fa6503225426" }, "hit2": { - "__uuid__": "bab2f84c-f988-4355-8fc7-2c55a09cc089" + "__uuid__": "4d23463f-c6fd-471f-ae81-06887ae7ebf9" }, "hit3": { - "__uuid__": "e06aa433-8f1d-4787-8e6d-7fe6a29ca6f5" + "__uuid__": "79b137f1-a2ff-441d-9f71-f82c811d264d" }, "hit4": { - "__uuid__": "ed4cf450-ae51-46ce-997a-50dd75d9a519" + "__uuid__": "c803eb49-bc7b-4b51-9761-998ceee3601a" }, "hit5": { - "__uuid__": "b27d353c-e4fe-46de-b7c9-75d2b96363d3" + "__uuid__": "a2a1098f-bac9-4bb0-b35f-a41bcee2766d" }, "_id": "24lN1LYRdNia3ZOiLsINIp" }, diff --git a/assets/Script/JiaZai.ts b/assets/Script/JiaZai.ts index c4d6f98..1283e8c 100644 --- a/assets/Script/JiaZai.ts +++ b/assets/Script/JiaZai.ts @@ -343,6 +343,15 @@ export default class JiaZai extends cc.Component { let top = this.node.getChildByName("Load").getChildByName("Top"); // top.getChildByName("hammer").active = false; } + + // 加载广告 + cc.fx.GameTool.preloadRewardedVideoAd().then((success) => { + if (success) { + console.log("HomeScene广告预加载初始化成功"); + } else { + console.log("HomeScene广告预加载初始化失败"); + } + }); } checkDailyQuests() { diff --git a/assets/Script/Map.ts b/assets/Script/Map.ts index 71e3561..21c0346 100644 --- a/assets/Script/Map.ts +++ b/assets/Script/Map.ts @@ -234,6 +234,15 @@ export default class MapConroler extends cc.Component { this.Block_Color = GameManager._instance.Block_Color; // this.particleEffects = GameManager._instance.particleEffects; this.initMap(); + + // 加载广告 + cc.fx.GameTool.preloadRewardedVideoAd().then((success) => { + if (success) { + console.log("GameScene广告预加载初始化成功"); + } else { + console.log("GameScene广告预加载初始化失败"); + } + }); } //道具数量 setPropNum() { diff --git a/assets/Script/Sdk/MiniGameSdk.ts b/assets/Script/Sdk/MiniGameSdk.ts index f63a658..8b08c74 100644 --- a/assets/Script/Sdk/MiniGameSdk.ts +++ b/assets/Script/Sdk/MiniGameSdk.ts @@ -264,6 +264,8 @@ export namespace MiniGameSdk { class ADVideo { private _adUid: string; private _adVideo: any = null; + private _isLoaded: boolean = false; + private _isLoading: boolean = false; get aduid() { return this._adUid; @@ -273,6 +275,120 @@ export namespace MiniGameSdk { this._adUid = adUid; } + /** + * 预加载广告内容 + */ + preload(): Promise { + return new Promise((resolve, reject) => { + if (!isWechat() && !isBytedance()) { + resolve(); + return; + } + + // 每次预加载都创建新的实例 + this._cleanup(); + + // 标记为正在加载 + this._isLoading = true; + this._isLoaded = false; + + let adInstance; + // 创建广告实例 + if (isWechat()) { + try { + // @ts-ignore + adInstance = wx.createRewardedVideoAd({ + adUnitId: this._adUid + }); + } catch (e) { + console.error('Failed to create rewarded video ad:', e); + this._isLoading = false; + reject(e); + return; + } + } else if (isBytedance()) { + // @ts-ignore + adInstance = tt.createRewardedVideoAd({ + adUnitId: this._adUid, + multiton: true, + multitonRewardMsg: ['多1次奖励', '再多一次奖励', '再多一次奖励'], + multitonRewardTimes: 3, + }); + } else { + adInstance = null; + } + + if (!adInstance) { + this._isLoading = false; + reject(new Error('Failed to create ad video instance')); + return; + } + + // 设置超时处理 + let loadTimeout: any = null; + const clearTimer = () => { + if (loadTimeout) { + clearTimeout(loadTimeout); + loadTimeout = null; + } + }; + loadTimeout = setTimeout(() => { + console.log('Ad preload timeout'); + this._isLoaded = false; + this._isLoading = false; + try { + adInstance.offLoad?.(onLoadHandler); + adInstance.offError?.(onErrorHandler); + } catch (e) { + console.log('Error removing listeners:', e); + } + this._cleanup(); + resolve(); + }, 5000); // 5秒加载超时 + + // 设置加载监听 + const onLoadHandler = () => { + clearTimer(); + console.log('Ad preload success'); + this._adVideo = adInstance; + this._isLoaded = true; + this._isLoading = false; + try { + adInstance.offLoad?.(onLoadHandler); + } catch (e) { + console.log('Error removing load listener:', e); + } + resolve(); + }; + + const onErrorHandler = (err: { errMsg: string, errCode: number }) => { + clearTimer(); + console.log('Ad preload error:', err); + this._isLoaded = false; + this._isLoading = false; + try { + adInstance.offError?.(onErrorHandler); + } catch (e) { + console.log('Error removing error listener:', e); + } + this._cleanup(); + resolve(); + }; + + adInstance.onLoad(onLoadHandler); + adInstance.onError(onErrorHandler); + + // 开始加载广告 + adInstance.load().catch((err: any) => { + clearTimer(); + console.log('Ad load error:', err); + this._isLoaded = false; + this._isLoading = false; + this._cleanup(); + resolve(); + }); + }); + } /** * 由于微信和抖音视频广告机制不同,微信可以看的视频广告个数只有0和1个,抖音平台则可以看0~maxVideoCount * @param onResult 两个参数:第一个res是EAdVideoResult定义,第二count是用户看了多少个视频广告。 @@ -284,12 +400,6 @@ export namespace MiniGameSdk { console.log("调用广告显示"); let callback = (state: EAdVideoResult, count: number) => { // 确保广告实例被清理 - if (this._adVideo) { - this._adVideo.offLoad?.(); - this._adVideo.offError?.(); - this._adVideo.offClose?.(); - this._adVideo = null; - } onResult?.call(target, state, count); } @@ -298,29 +408,42 @@ export namespace MiniGameSdk { return; } - // 总是创建新的广告实例 - if (this._adVideo) { - this._adVideo.offLoad?.(); - this._adVideo.offError?.(); - this._adVideo.offClose?.(); - this._adVideo = null; - } + // 每次调用都创建新的广告实例以确保可以重复调用 + this._cleanup(); let onAdVideoClosed = (res: any) => { + if (this._adVideo) { + try { + this._adVideo.offClose?.(onAdVideoClosed); + } catch (e) { + console.log('Error removing close listener:', e); + } + } + let result: EAdVideoResult; + let count: number; + if (isWechat()) { if (res && res.isEnded || res === undefined) { - callback(EAdVideoResult.ACCEPT, 1); + result = EAdVideoResult.ACCEPT; + count = 1; } else { - callback(EAdVideoResult.REJECT, 0); + result = EAdVideoResult.REJECT; + count = 0; } } else if (isBytedance()) { let resConverted = res as { isEnded: boolean, count: number }; if (resConverted && resConverted.count > 0) { - callback(EAdVideoResult.ACCEPT, resConverted.count); + result = EAdVideoResult.ACCEPT; + count = resConverted.count; } else { - callback(EAdVideoResult.REJECT, 0); + result = EAdVideoResult.REJECT; + count = 0; } } + + // 确保在回调之前清理广告实例 + this._cleanup(); + callback(result, count); } let responseData = { ad_type: "激励视频", //广告类型 @@ -331,6 +454,7 @@ export namespace MiniGameSdk { error_detail_code: "", //错误码 is_filled_success: false //是否完整播放 } + if (isWechat()) { try { // @ts-ignore @@ -345,6 +469,7 @@ export namespace MiniGameSdk { responseData.error_detail_info = e.toString(); responseData.error_detail_code = e.errCode?.toString() || ""; cc.fx.GameTool.shushu_Track("ad_response", responseData); + this._cleanup(); callback(EAdVideoResult.ERROR, 0); return; } @@ -362,6 +487,7 @@ export namespace MiniGameSdk { // 确保广告对象已创建 if (!this._adVideo) { + this._cleanup(); callback(EAdVideoResult.ERROR, 0); return; } @@ -372,6 +498,13 @@ export namespace MiniGameSdk { this._adVideo.onError((err: { errMsg: string, errCode: number }) => { console.log('Ad video error:', err); + if (this._adVideo) { + try { + this._adVideo.offError?.(); + } catch (e) { + console.log('Error removing error listener:', e); + } + } responseData.is_filled_success = false; responseData.error_detail_info = err.toString(); responseData.error_detail_code = err.errCode?.toString() || ""; @@ -379,13 +512,12 @@ export namespace MiniGameSdk { if (err.errCode) { console.log('Ad video error code:', err.errCode); } + // 出错后清理实例 + this._cleanup(); callback(EAdVideoResult.ERROR, 0); + }); - // const onCloseHandler = onAdVideoClosed; - - // this._adVideo.onLoad(onLoadHandler); - // this._adVideo.onError(onErrorHandler); this._adVideo.onClose(onAdVideoClosed); let showData = { @@ -401,33 +533,51 @@ export namespace MiniGameSdk { this._adVideo.show().catch((err: { errMsg: string, errCode: number }) => { console.log('Show video ad error:', err); // 尝试加载广告后再显示 - this._adVideo.load().then(() => { - showData.is_show_success = true; - cc.fx.GameTool.shushu_Track("ad_show", showData); - cc.fx.GameConfig.GM_INFO.videoTime = Math.floor(Date.now() / 1000); - return this._adVideo.show(); - }).catch((loadErr: { errMsg: string, errCode: number }) => { - showData.is_show_success = false; - showData.error_detail_info = loadErr.errMsg; - showData.error_detail_code = loadErr.errCode.toString(); - cc.fx.GameTool.shushu_Track("ad_show", showData); - console.log('Load and show video ad error:', loadErr); - if (loadErr.errCode) { - console.log('Load error code:', loadErr.errCode); - } + if (this._adVideo) { + this._adVideo.load().then(() => { + showData.is_show_success = true; + cc.fx.GameTool.shushu_Track("ad_show", showData); + cc.fx.GameConfig.GM_INFO.videoTime = Math.floor(Date.now() / 1000); + return this._adVideo?.show(); + }).catch((loadErr: { errMsg: string, errCode: number }) => { + showData.is_show_success = false; + showData.error_detail_info = loadErr.errMsg; + showData.error_detail_code = loadErr.errCode.toString(); + cc.fx.GameTool.shushu_Track("ad_show", showData); + console.log('Load and show video ad error:', loadErr); + if (loadErr.errCode) { + console.log('Load error code:', loadErr.errCode); + } + // 出错后清理实例 + this._cleanup(); + callback(EAdVideoResult.ERROR, 0); + + }); + } else { + this._cleanup(); callback(EAdVideoResult.ERROR, 0); - }); + } }); } - destory() { + private _cleanup() { if (this._adVideo) { - this._adVideo.offLoad?.(); - this._adVideo.offError?.(); - this._adVideo.offClose?.(); - this._adVideo.destroy?.(); - this._adVideo = null; + try { + // 移除所有事件监听器 + this._adVideo.offLoad?.(); + this._adVideo.offError?.(); + this._adVideo.offClose?.(); + } catch (e) { + console.log('Error removing ad event listeners:', e); + } } + this._adVideo = null; + this._isLoaded = false; + this._isLoading = false; + } + + destory() { + this._cleanup(); } } @@ -458,10 +608,40 @@ export namespace MiniGameSdk { private _interstitial: ADInterstitial; private _banner: ADBanner; private _customs: Record = {}; + private _preloadPromise: Promise | null = null; // 用于跟踪预加载状态 private constructor() { } + /** + * 预加载视频广告 + * @param adUid 广告ID + * @returns Promise + */ + public preloadVideo(adUid: string): Promise { + // 如果已有相同广告ID的实例,先销毁 + if (this._video && this._video.aduid !== adUid) { + this._video.destory(); + this._video = null; + } + + // 创建新实例(如果需要) + if (!this._video) { + this._video = new ADVideo(adUid); + } + + // 执行预加载 + this._preloadPromise = this._video.preload(); + return this._preloadPromise; + } + /** + * 检查广告是否已预加载完成 + * @returns Promise + */ + public isPreloadReady(): Promise { + console.log("加载广告中...", this._preloadPromise) + return this._preloadPromise || Promise.resolve(); + } /** * 预加载横幅广告,不会显示。只有你在调用showBanner时才会显示。 @@ -569,8 +749,14 @@ export namespace MiniGameSdk { * @param maxVideoCount 最大视频个数。默认是3,仅对抖音平台生效。微信平台看完视频count的结果永远是1或0 */ public showVideo(adUid: string, video_data, onVideoResult: (res: EAdVideoResult, count: number) => void, target?: any, maxVideoCount: number = 3) { - this._video?.destory(); - this._video = new ADVideo(adUid); + // 只有在广告ID不匹配时才重新创建实例 + if (this._video && this._video.aduid !== adUid) { + this._video.destory(); + this._video = new ADVideo(adUid); + } else if (!this._video) { + this._video = new ADVideo(adUid); + } + this._video.show(onVideoResult, target, maxVideoCount, video_data); } @@ -1543,7 +1729,19 @@ export namespace MiniGameSdk { console.log("版本:", version); } } - + static yinli_FinishiStage() { + //@ts-ignore + if (typeof wx !== 'undefined' && wx !== null) { + if (API._ge) { + // 假设 track 方法返回一个 Promise + API._ge.track("$CompleteSection", { + $main_section_no: (cc.fx.GameConfig.GM_INFO.level), + $section_sum: (cc.fx.GameConfig.GM_INFO.level), + $section_type: "0" + }); + } + } + } /** * 上报广告展示事件 参数如下 * @param adType 广告类型 (取值为:reward、banner、native、interstitial、video_feed、video_begin,分别对应激励视频广告、Banner广告、原生模板广告、插屏广告、视频广告、视频贴片广告) diff --git a/assets/Script/module/Tool/GameTool.ts b/assets/Script/module/Tool/GameTool.ts index 68f44b1..c029a96 100644 --- a/assets/Script/module/Tool/GameTool.ts +++ b/assets/Script/module/Tool/GameTool.ts @@ -1031,6 +1031,9 @@ var GameTool = { console.log(eventData); // MiniGameSdk.API.showToast("准备上报完成游戏"); } + if (name == "finish_stage") { + MiniGameSdk.API.yinli_FinishiStage(); + } // MiniGameSdk.API.shushu_Track(name,eventData); MiniGameSdk.API.shushu_Track(name, eventData); }, @@ -1483,7 +1486,7 @@ var GameTool = { }, onShowVideo(callback: Function, videoData: any) { - console.log("调用视频广告"); + console.log("调用广告"); if (typeof wx !== 'undefined' && wx !== null) { let scene = null; if (videoData.current_page == "HomeScene") { @@ -1514,51 +1517,62 @@ var GameTool = { cc.fx.GameConfig.GM_INFO.videoTime = Math.floor(Date.now() / 1000); // 添加超时处理 const timeoutId = setTimeout(() => { - console.log('广告加载超时'); if (scene) scene.closeLoad(); + console.log("激励视频超时"); MiniGameSdk.API.showToast('广告加载超时,请稍后再试'); // 调用回调并传入错误状态 callback(false); }, 6000); // 6秒超时 - MiniGameSdk.AdvertManager.instance.showVideo('adunit-32a23bc2b2f4ee0a', videoData, (res: MiniGameSdk.EAdVideoResult, count: number) => { - if (timeoutId) clearTimeout(timeoutId); - console.log('用户看的视频广告个数是:', count); - console.log('用户观看视频广告结果:', res); - if (scene) scene.closeLoad(); - let duration = Math.floor(Date.now() / 1000) - cc.fx.GameConfig.GM_INFO.videoTime; - if (duration <= 0) duration = 0; - let data = { - ad_type: "激励视频", //广告类型 - ad_placement_name: videoData.ad_placement_name, //内部广告位名称 //2000复活 道具 2001 2002 2003 - ad_placement_id: "adunit-32a23bc2b2f4ee0a", //内部广告位ID - current_page: videoData.current_page, //所在页面 - ad_duration: duration, //广告展示时长 - ad_play_duration: 30, //广告播放时长 - is_complete_play: false //是否完整播放 - } - switch (res) { - case MiniGameSdk.EAdVideoResult.ACCEPT: - data.is_complete_play = true; - MiniGameSdk.API.showToast('用户看完广告,可以奖励'); - cc.fx.GameTool.shushu_Track("ad_close", data); - callback(true); - break; - case MiniGameSdk.EAdVideoResult.REJECT: - data.is_complete_play = false; - MiniGameSdk.API.showToast('广告中断,未获得奖励'); - cc.fx.GameTool.shushu_Track("ad_close", data); - callback(false); - break; - case MiniGameSdk.EAdVideoResult.ERROR: - MiniGameSdk.API.showToast('广告加载失败,请稍后再试'); - // cc.fx.GameTool.shushu_Track("ad_close", data); - callback(false); - break; - default: - // 其他情况,不作处理 - break; - } - }); + // 等待预加载完成后再显示广告,提高显示速度 + cc.fx.GameTool.waitForAdPreload().then(() => { + console.log("开始显示激励视频"); + MiniGameSdk.AdvertManager.instance.showVideo('adunit-32a23bc2b2f4ee0a', videoData, (res: MiniGameSdk.EAdVideoResult, count: number) => { + if (timeoutId) clearTimeout(timeoutId); + console.log('用户看的视频广告个数是:', count); + console.log('用户观看视频广告结果:', res); + if (scene) scene.closeLoad(); + let duration = Math.floor(Date.now() / 1000) - cc.fx.GameConfig.GM_INFO.videoTime; + if (duration <= 0) duration = 0; + let data = { + ad_type: "激励视频", //广告类型 + ad_placement_name: videoData.ad_placement_name, //内部广告位名称 //2000复活 道具 2001 2002 2003 + ad_placement_id: "adunit-32a23bc2b2f4ee0a", //内部广告位ID + current_page: videoData.current_page, //所在页面 + ad_duration: duration, //广告展示时长 + ad_play_duration: 30, //广告播放时长 + is_complete_play: false //是否完整播放 + } + switch (res) { + case MiniGameSdk.EAdVideoResult.ACCEPT: + data.is_complete_play = true; + MiniGameSdk.API.showToast('用户看完广告,可以奖励'); + cc.fx.GameTool.shushu_Track("ad_close", data); + callback(true); + // 广告观看成功后,立即开始下一次预加载 + cc.fx.GameTool.preloadRewardedVideoAd(true); + break; + case MiniGameSdk.EAdVideoResult.REJECT: + data.is_complete_play = false; + MiniGameSdk.API.showToast('广告中断,未获得奖励'); + cc.fx.GameTool.shushu_Track("ad_close", data); + callback(false); + // 广告观看失败后,也重新预加载 + cc.fx.GameTool.preloadRewardedVideoAd(true); + break; + case MiniGameSdk.EAdVideoResult.ERROR: + MiniGameSdk.API.showToast('广告加载失败,请稍后再试'); + // cc.fx.GameTool.shushu_Track("ad_close", data); + callback(false); + // 广告加载错误后,也重新预加载 + cc.fx.GameTool.preloadRewardedVideoAd(true); + + break; + default: + // 其他情况,不作处理 + break; + } + }); + }) } }, @@ -1653,7 +1667,86 @@ var GameTool = { // 递归处理子节点 node.children.forEach(child => this.setGray(child, gray)); - } + }, + + /** + * 预加载激励视频广告 + * 提前加载广告以提高显示速度 + */ + preloadRewardedVideoAd(force: boolean = false): Promise { + console.log("开始预加载激励视频广告, force:", force); + + // 如果不是强制重新加载,检查是否已经有预加载正在进行或已完成 + if (!force && this._lastPreloadTime) { + const now = Date.now(); + // 如果距离上次预加载不到5分钟,不重复预加载 + if (now - this._lastPreloadTime < 5 * 60 * 1000) { + console.log("广告预加载间隔时间不足,跳过本次预加载"); + return Promise.resolve(true); + } + } + + this._lastPreloadTime = Date.now(); + + return new Promise((resolve) => { + // 使用 setTimeout 确保在场景加载完成后再预加载广告 + setTimeout(() => { + try { + // 预加载激励视频广告 + const preloadPromise = MiniGameSdk.AdvertManager.instance.preloadVideo('adunit-32a23bc2b2f4ee0a'); + + // 添加超时处理,避免预加载Promise永远处于pending状态 + const preloadTimeout = setTimeout(() => { + console.log('预加载激励视频广告超时'); + resolve(true); + }, 5000);// 5秒预加载超时 + + preloadPromise + .then(() => { + clearTimeout(preloadTimeout); + console.log('激励视频广告预加载成功'); + resolve(true); + }) + .catch((error) => { + clearTimeout(preloadTimeout); + console.error('预加载激励视频广告失败:', error); + resolve(true); + }); + + console.log('激励视频广告预加载已启动'); + } catch (error) { + console.error('预加载激励视频广告失败:', error); + resolve(true); + } + }, 1000); // 延迟1秒预加载,确保场景加载完成 + }); + }, + + /** + * 等待广告预加载完成 + * @returns Promise + */ + waitForAdPreload(): Promise { + console.log("等待广告预加载完成..."); + return new Promise((resolve) => { + const waitTimeout = setTimeout(() => { + console.log("等待广告预加载超时,继续执行"); + resolve(); + }, 5000); // 5秒等待超时 + + MiniGameSdk.AdvertManager.instance.isPreloadReady() + .then(() => { + clearTimeout(waitTimeout); + console.log("广告预加载已完成"); + resolve(); + }) + .catch(() => { + clearTimeout(waitTimeout); + console.log("广告预加载出错,继续执行"); + resolve(); + }); + }); + }, }; export { GameTool }; \ No newline at end of file