Initializing Ads on the Main Menu
Allocating the Unity Ads Initialization within the Menu Scene
Following up with the previous article, we have developed the two scripts that are required to launch and run the Unity Ads during the shop UI. During that example, I placed both the Ad Initializer and Ad Manager within the same component. The issue that persisted was that whenever the scene gets reloaded (aka. Player Death and you hit the retry button to reload the scene) The Unity Ads gets stuck in a loop and doesn’t load. The reason it doesn’t load is because the script initializer is already loaded into the scene and since there can be only one initializer, the rest of the script doesn’t follow suit.
To solve this issue, we will need to place the Ad Initializer script within the Main menu scene so all the Ads are prepped and ready. Then in the game scene, we will Load the actual Ad. This way whenever the player does a scene reset, the Initializer is already completed and stable, we just need to reload a new ad.
Preparing the Main Menu:
For now, we are just going to create a make shift scene that contains a play button that will load into our game scene. Inside this main menu we are going to create an new empty game object that will contain the Initialized Ads script.
Next we are going to create a UI Canvas that contains a button. Following that we will create a new C# script that will contain the ability to load the game scene.
We are going to apply that start button script into our start button, and in the On Click event, we will set the parameters to be the public method on the script.
Finally we are going to add the Main Menu scene to our builds settings and make sure that the Menu scene is top of the list.
Fixing the Initialized Ads script:
Since we are now loading the Ads within our Game scene only, we need to focus on Initializing the Ads only. Therefore within the Initialized Ad script, we need to get rid of all the Load Ad methods, and get rid of any references that require the Ad Manager script.
Adjusting the Ad Manager script:
In the Ad Manager script, we need to now toggle the Load Ad method within the Awake method since the Initialized Ad script is no longer triggering that method.
Testing the Game:
Now when we test the game starting from our Menu scene. We will noticed that during the Menu scene all the Ads would be initialized, then within the Game scene, the Ads get loaded in.
When the scene gets restarted or reset, The Ad initialization doesn’t happen again, instead we just get the Ad being reloaded in again.
By splitting up the Initialization and Loading, we now have a more streamline process that doesn’t hinder on performance. If we create another new game scene, we can just add another Ad Manager without the Initialization.