Banner + Singleton in Unity

312 views
Skip to first unread message

Onur Çalışan

unread,
Sep 20, 2021, 5:42:01 PM9/20/21
to Google Mobile Ads SDK Developers
Hello,

I have a game with one scene, I tried to make a gameobject with a banner script + singleton so it doesn't need to reload on everytime i restart the game, but will only load on the first time and use the same banner. 

When I test in editor and play the game for the first time my banner is there without any problem. After character dies and i press the restart game, my banner Ad is gone.(By `restarting` I mean in my game menu not in the editor.)

I tried my Admob script without a singleton and it works fine in the both editor and android device. But if i implement singleton to my Admob script I can see my banner only the first time I start the game in the editor. 
If i press Restart the game the banner is gone but my Admob script is still there under the DontDestroy object in the hierarchy. So simply my script stays after I restart the scene but banners dont.

Also,i tried to build apk with a singleton and tested in on my android device. Banner is still there, so it looks like there is no problem. But I feel something wrong. Can you give me any advice?

Mobile Ads SDK Forum Advisor

unread,
Sep 21, 2021, 4:02:57 AM9/21/21
to onurca...@gmail.com, google-adm...@googlegroups.com

Hello Onur,

Thank you for reaching out to us.

We would recommend that you put your AdMob manager script into a Singleton object that calls DontDestroyOnLoad on Start(). This way, your game will always only have exactly one copy of the above script, and thus should alleviate any issues pertaining to duplicated or otherwise wrongly-timed SDK function calls. Kindly note that the implementation of a Singleton is outside the scope of our support; you may want to check out implementation guides for that on any Unity-centric blog or forum.

Regards,

Google Logo
Princess Pamela Pineda
Mobile Ads SDK Team
 

 



ref:_00D1U1174p._5004Q2OSa7g:ref

Onur Çalışan

unread,
Sep 21, 2021, 5:08:53 AM9/21/21
to Google Mobile Ads SDK Developers
Good day,

I already checked and search almost every forum and could not find the answer. My code is below, so if i use the code like this my banner shows when i start the game for the first time. But if i restart the game in my game menu, banner is gone. As i mentioned before this is a problem only happens in Unity editor. In my Android device banner stays so looks like there is no problem, But I am not sure. 
So i like to ask 2 questions.

1- Is it okay to use my code, since its only Unity Editor problem, and It works as expected in my device(It request only 1 banner and I can use that banner until I quit).
2- From what I understand, my script below makes 1 banner dummy when I start the game for the first time. And in the hiyerarchy my code('GoogleMobileAdsDemoScript ') goes under DontDestroyOnLoad, but banner dummy does not. Thats why when I restart the scene dummy disappears. Dummy created from below script is not persistent , but the script is persistent. So what I am trying to ask, is that dummy ad meant to be persistent in Editor or not ? Lets say if i create 1 dummy banner without singleton and change my scene is it meant to stay in android device even if dummy does not show up in the editor ?


public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private BannerView bannerView;
    public static GoogleMobileAdsDemoScript instance;
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(this);
        }
        else
        {
            Destroy(gameObject);
            return;
        }


    }
    public void Start()
    {
        this.RequestBanner();
    }

    private void RequestBanner()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
            string adUnitId = "unexpected_platform";
#endif

        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();

        // Load the banner with the request.
        bannerView.LoadAd(request);
    }
}

Onur Çalışan

unread,
Sep 21, 2021, 11:46:16 AM9/21/21
to Google Mobile Ads SDK Developers
Hi,

Can you please help me ?

Mobile Ads SDK Forum Advisor

unread,
Sep 21, 2021, 2:15:23 PM9/21/21
to onurca...@gmail.com, google-adm...@googlegroups.com
Hi Onur,

Thank you for reaching out to us. In regards to your questions. There would appear to be nothing wrong with using the singleton to hold the instance of the adView throughout your various scenes. But as to how to implement that singleton within Unity? We would suggest looking at the Unity forums for how to create the singleton. Here is one example of how a person did it. Also the other thing you need to know, is that the singleton is created every time you start the game, so if you want to see the say ad after completely closing the app? This will not do it, the singleton is best used in a multi scene app.

Regards,
Google Logo
William Pescherine
Mobile Ads SDK Team
 


ref:_00D1U1174p._5004Q2OSa7g:ref

Onur Çalışan

unread,
Sep 22, 2021, 5:59:03 AM9/22/21
to Google Mobile Ads SDK Developers
Good day again and thanks for your time,

It takes 10-15 seconds to play the game. When the player dies, he starts playing again with the Restart button that I created. The restart button simply has 1 code that reloads the scene. This way player plays again by reloading the scene. 

But since the game is very short (in some cases, the player can die and restart the game, even before the banner is loaded), I decided to add a singleton to my Admob code. When I add a singleton to my Admob code, banner dummys created everytime I start the game for the first time . So far, no problem.

But if the player dies and restarts the scene inside the game, the banner dummys disappear. Because my code is singleton and I only request the banner once. Banner dummys do not appear under the DontDestroyOnLoad object in the hierarchy, only the Admob code stays there and therefore banner dummies disappear when player restarts the scene. 

But when I test it on Android phone, the banners do not disappear after restarting the scene(I want the banners not to disappear anyway). So it seems to work fine on Android phone without any problem.

So the question is actually about Dummys in the editor, is it enough to call the RequestBanner function for once? Is it normal for Bannerdummys to disappear after scene transitions? Or does it have to show up after transition too? Lets say i have 20 scene game. I have Admob script at only the first scene and i call the requestBanner first scene. And after i change to a scene which does not have admob script should the banner stay both in Editor and Android device?

The problem, as I said, is that banner dummys do not appear under DontDestroyOnLoad. This behaivour only happens in the Unity editor. Maybe on the Android device, it stays at the top of the game because there is a real advertisement(even if its a test or not), and if it is not intervened, it stays until the end of the game, even during the scene transitions.

Mobile Ads SDK Forum Advisor

unread,
Sep 22, 2021, 11:05:54 AM9/22/21
to onurca...@gmail.com, google-adm...@googlegroups.com
Hi Onur,

Thank you for responding and the clarification. Based on what you have mentioned, we do believe that it is normal for the Dummy to stay in the editor. Especially since you are using the singleton. Also know that this is actually more of a Unity thing. When looking over it this is being created by Unity and the editor then it is by our SDK. But with that being said, it seems like you should be fine.
Message has been deleted

Onur Çalışan

unread,
Sep 22, 2021, 11:57:29 AM9/22/21
to Google Mobile Ads SDK Developers
Thank you for your answer.

It is almost crystal clear now except you said dummies should stay in the Editor. But I am saying it does not stay in the editor except the first scene. I think you meant `` it is normal for the Dummy  not to stay in the editor ``. As long as banner stays in the android device. I really hope it is typo. Please correct me if I am wrong. 
Best Regards.

Mobile Ads SDK Forum Advisor

unread,
Sep 22, 2021, 5:21:21 PM9/22/21
to onurca...@gmail.com, google-adm...@googlegroups.com
Hi Onur,

Thank you for the response, my apology about that. You are correct it should NOT. Looks like you can also look at the Coroutine for Unity as it talks more about these things, using this to create the singleton and talks about how the dummy will end when it is no longer needed by the system. Again most of this is actually Unity doing things, and not related to the SDK.

Onur Çalışan

unread,
Sep 23, 2021, 5:39:21 AM9/23/21
to Google Mobile Ads SDK Developers
Hello again,

Thank you very much for your help and time. 

Best regards.

Reply all
Reply to author
Forward
0 new messages