googleads / googleads-mobile-unity

Official Unity Plugin for the Google Mobile Ads SDK
https://developers.google.com/admob/unity
Apache License 2.0
1.34k stars 1.09k forks source link

[BUG] AdMob causes game's Time Scale to be 0 #3155

Closed kiraio-moe closed 3 months ago

kiraio-moe commented 4 months ago

[REQUIRED] Step 1: Describe your environment

[REQUIRED] Step 2: Describe the problem

For unknown reasons, the game Time Scale is set to 0 when you initialized the ads. So, your game seems like "freezed" because of ANR while in reality isn't. To fix this, just set the game time scale to 1 with Time.timeScale.

Steps to reproduce:

* I have attached the sample project. Make sure to comment the code to set the time scale to 1 in Assets/Scripts/Manager/ManageScene.cs.

Attachment(s): zero-time-scale-bug.zip

NVentimiglia commented 4 months ago

I tried looking at ManageScene and could not see any reference to GoogleMobile Ads. Can you replicate this in the HelloWorld sample, or only the custom implementation?

Looking at the code, I see we do pause the game, but only on the UnityEditor (here). Are you seeing this on Android / iOS ?

kiraio-moe commented 4 months ago

I tried looking at ManageScene and could not see any reference to GoogleMobile Ads. Can you replicate this in the HelloWorld sample, or only the custom implementation?

All the code for ads implementation are inside Assets/Scripts/Ads folder. You can see I initialize the ad in AdsManager.cs.

Looking at the code, I see we do pause the game, but only on the UnityEditor (here). Are you seeing this on Android / iOS ?

I see both on Editor & Android.

NVentimiglia commented 4 months ago

@kiraio-moe

Looking at the source code I see two references to Time.TimeScale, both reside within the Unity platform.

https://github.com/search?q=repo%3Agoogleads%2Fgoogleads-mobile-unity%20Time.timeScale&type=code

That said, the Android interstitial ad is a different Activity which could cause Unity to pause on its own if you disable the Run In Background option. I think this is what you are seeing.

https://docs.unity3d.com/ScriptReference/Application-runInBackground.html

dqnhust commented 4 months ago

Hey @kiraio-moe I think the scenario goes like this:

When initializing AdMob or UMP, the Unity Editor will display an UMP popup or interstitial, or reward ad and pause the game (timeScale=0). Then, the developer will do something that causes the popup to be destroyed (such as switching scenes). Of course, this happens very quickly (within a few frames), so the developer may not realize that their game logic has destroyed the popup that AdMob is showing. Meanwhile, AdMob thinks it needs to pause the game for the user to click the close button. That’s the assumption I’m considering, and I believe it might help you.

NVentimiglia commented 4 months ago

@kiraio-moe let me know if you need further help. As @dqnhust pointed out the logic which we use to run on the editor (for all platforms) is not the same logic used on device.

kiraio-moe commented 3 months ago

When initializing AdMob or UMP, the Unity Editor will display an UMP popup or interstitial, or reward ad and pause the game (timeScale=0). Then, the developer will do something that causes the popup to be destroyed (such as switching scenes).

I'd switching to another scene and yes, the AdMob ad get destroyed quickly before the scene is switched. Thanks for explanation. I know what to do now.