From 7b1abe437fafa2bdb501373f81a43cc8ffb3d7d8 Mon Sep 17 00:00:00 2001 From: KarimAkra Date: Mon, 14 Apr 2025 21:17:14 +0300 Subject: [PATCH] Add proper configuration for AdMob ads Co-Authored-By: sector-a <82838084+sector-a@users.noreply.github.com> Co-Authored-By: Lily --- project.hxp | 1 - source/funkin/mobile/util/AdMobUtil.hx | 19 ++++++++++++------- source/funkin/play/PauseSubState.hx | 15 +++++++++++++++ source/funkin/play/PlayState.hx | 19 ++----------------- source/funkin/play/ResultState.hx | 8 ++++---- source/funkin/util/Constants.hx | 4 ++-- 6 files changed, 35 insertions(+), 31 deletions(-) diff --git a/project.hxp b/project.hxp index ac2b30905..079bb9c05 100644 --- a/project.hxp +++ b/project.hxp @@ -859,7 +859,6 @@ class Project extends HXProject { if (isIOS()) { - // This ID is not from us, it's a test ID provided by AdMob. not really sure if it works. setEnv("ADMOB_APPID", ""); } } diff --git a/source/funkin/mobile/util/AdMobUtil.hx b/source/funkin/mobile/util/AdMobUtil.hx index cbfbfe158..e7d20884c 100644 --- a/source/funkin/mobile/util/AdMobUtil.hx +++ b/source/funkin/mobile/util/AdMobUtil.hx @@ -1,7 +1,7 @@ package funkin.mobile.util; -import extension.admob.Admob; -import extension.admob.AdmobEvent; +import admob.Admob; +import admob.util.AdmobEvent; /** * Utility class for managing AdMob advertisements in a mobile application. @@ -15,12 +15,17 @@ class AdMobUtil * Production ad unit IDs (to be set for actual ad campaigns). */ #if !TESTING_ADS - private static final BANNER_AD_UNIT_ID:String = ""; - private static final INTERSTITIAL_AD_UNIT_ID:String = ""; + private static final BANNER_AD_UNIT_ID:String = #if android "" #elseif ios "" #else "" #end; + private static final INTERSTITIAL_AD_UNIT_ID:String = #if android "" #elseif ios "" #else "" #end; private static final INTERSTITIAL_VIDEO_AD_UNIT_ID:String = ""; private static final REWARDED_AD_UNIT_ID:String = ""; - #else + #else + /** + * Test ad unit IDs for development and testing purposes. + * These IDs are provided by Google AdMob for testing ads without incurring costs. + * They should not be used in production applications. + */ /** * Test ad unit ID for banner ads, used during development. */ @@ -58,9 +63,9 @@ class AdMobUtil * it loads either a video or a standard interstitial ad. * @param forceVideo If true, forces loading an interstitial video ad. Otherwise, it uses a 50% chance. */ - public static inline function loadInterstitial(forceVideo:Bool = false):Void + public static inline function loadInterstitial():Void { - Admob.loadInterstitial((forceVideo || FlxG.random.bool(50)) ? AdMobUtil.INTERSTITIAL_VIDEO_AD_UNIT_ID : AdMobUtil.INTERSTITIAL_AD_UNIT_ID); + Admob.loadInterstitial(#if TESTING_ADS FlxG.random.bool(50) ? AdMobUtil.INTERSTITIAL_VIDEO_AD_UNIT_ID : #end AdMobUtil.INTERSTITIAL_AD_UNIT_ID); } /** diff --git a/source/funkin/play/PauseSubState.hx b/source/funkin/play/PauseSubState.hx index da7b85dab..5e0e79825 100644 --- a/source/funkin/play/PauseSubState.hx +++ b/source/funkin/play/PauseSubState.hx @@ -21,6 +21,9 @@ import funkin.ui.MusicBeatSubState; import funkin.ui.transition.StickerSubState; import funkin.mobile.util.SwipeUtil; import funkin.mobile.util.TouchUtil; +#if NO_DISABLE_ADMOB_ADS +import funkin.mobile.util.AdMobUtil; +#end /** * Parameters for initializing the PauseSubState. @@ -193,6 +196,11 @@ class PauseSubState extends MusicBeatSubState { super(); this.currentMode = params?.mode ?? Standard; + + // Add banner ad when game is paused. + #if NO_DISABLE_ADMOB_ADS + AdMobUtil.addBanner(admob.Admob.BANNER_SIZE_BANNER, admob.Admob.BANNER_ALIGN_TOP); + #end } // =============== @@ -427,6 +435,13 @@ class PauseSubState extends MusicBeatSubState if (accept && !justOpened) { + // Remove banner ad when game is unpaused. + #if NO_DISABLE_ADMOB_ADS + if (currentMenuEntries[currentEntry].text != "Change Difficulty" + && currentMenuEntries[currentEntry].text != "Enable Practice Mode" + && currentMenuEntries[currentEntry].text != "Back") AdMobUtil.removeBanner(); + #end + currentMenuEntries[currentEntry].callback(this); } else if (controls.PAUSE && !justOpened) diff --git a/source/funkin/play/PlayState.hx b/source/funkin/play/PlayState.hx index 150ea2e5d..6ca7b1e00 100644 --- a/source/funkin/play/PlayState.hx +++ b/source/funkin/play/PlayState.hx @@ -990,11 +990,6 @@ class PlayState extends MusicBeatSubState // Attempt to pause the game. if ((controls.PAUSE || androidPause || pauseButtonCheck) && isInCountdown && mayPauseGame && !justUnpaused) { - // Add banner ad when game is paused. - #if NO_DISABLE_ADMOB_ADS - AdMobUtil.addBanner(extension.admob.Admob.BANNER_SIZE_BANNER, extension.admob.Admob.BANNER_ALIGN_TOP); - #end - #if mobile pauseButton.alpha = 0; #end @@ -1181,10 +1176,10 @@ class PlayState extends MusicBeatSubState function moveToGameOver():Void { - // Shows a video ad on mobile devices each 3 blueballs + // Shows an interstital ad on mobile devices each 3 blueballs #if NO_DISABLE_ADMOB_ADS Constants.GLOBAL_BLUEBALL_COUNTER++; - if (Constants.GLOBAL_BLUEBALL_COUNTER > 0 && Constants.GLOBAL_BLUEBALL_COUNTER % 3 == 0) AdMobUtil.loadInterstitial(true); + if (Constants.GLOBAL_BLUEBALL_COUNTER > 0 && Constants.GLOBAL_BLUEBALL_COUNTER % 3 == 0) AdMobUtil.loadInterstitial(); #end // Reset and update a bunch of values in advance for the transition back from the game over substate. @@ -1337,11 +1332,6 @@ class PlayState extends MusicBeatSubState */ public override function closeSubState():Void { - // Remove banner ad when game is unpaused. - #if NO_DISABLE_ADMOB_ADS - AdMobUtil.removeBanner(); - #end - if (Std.isOfType(subState, PauseSubState)) { var event:ScriptEvent = new ScriptEvent(RESUME, true); @@ -1624,11 +1614,6 @@ class PlayState extends MusicBeatSubState { performCleanup(); - // Remove banner ad when playstate is destroyed. - #if NO_DISABLE_ADMOB_ADS - AdMobUtil.removeBanner(); - #end - super.destroy(); } diff --git a/source/funkin/play/ResultState.hx b/source/funkin/play/ResultState.hx index c95c57e75..03dcc6e68 100644 --- a/source/funkin/play/ResultState.hx +++ b/source/funkin/play/ResultState.hx @@ -799,9 +799,9 @@ class ResultState extends MusicBeatSubState if (params.storyMode) { - // Shows a video ad on mobile devices each week victory. + // Shows a interstital ad on mobile devices each week victory. #if NO_DISABLE_ADMOB_ADS - if (PlayStatePlaylist.isStoryMode) AdMobUtil.loadInterstitial(true); + if (PlayStatePlaylist.isStoryMode) AdMobUtil.loadInterstitial(); #end if (PlayerRegistry.instance.hasNewCharacter()) @@ -837,11 +837,11 @@ class ResultState extends MusicBeatSubState var isPersonalBest = rank > Scoring.calculateRank(params?.prevScoreData); - // Shows a video ad on mobile devices each 3 victories on freeplay. + // Shows a interstital ad on mobile devices each 3 victories on freeplay. #if NO_DISABLE_ADMOB_ADS Constants.GLOBAL_FREEPLAY_VICTORY_COUNTER++; if ((Constants.GLOBAL_FREEPLAY_VICTORY_COUNTER > 0 - && Constants.GLOBAL_FREEPLAY_VICTORY_COUNTER % 3 == 0)) AdMobUtil.loadInterstitial(true); + && Constants.GLOBAL_FREEPLAY_VICTORY_COUNTER % 3 == 0)) AdMobUtil.loadInterstitial(); #end if (isScoreValid && isPersonalBest) diff --git a/source/funkin/util/Constants.hx b/source/funkin/util/Constants.hx index e58c012a6..4acb58582 100644 --- a/source/funkin/util/Constants.hx +++ b/source/funkin/util/Constants.hx @@ -591,13 +591,13 @@ class Constants #if NO_DISABLE_ADMOB_ADS /** * A Counter that is increased by 1 each blueball. - * Only used for video ads each 3 blueballs on mobile. + * Only used for interstital ads each 3 blueballs on mobile. */ public static var GLOBAL_BLUEBALL_COUNTER:Int = 0; /** * A Counter that is increased by 1 each victory only in freep;ay songs. - * Only used for video ads each 3 victories on mobile. + * Only used for interstital ads each 3 victories on mobile. */ public static var GLOBAL_FREEPLAY_VICTORY_COUNTER:Int = 0; #end