Unity 2022 Mobile Game Development - Third Edition by John P. Doran

Unity 2022 Mobile Game Development - Third Edition by John P. Doran

Author:John P. Doran
Language: eng
Format: epub
Publisher: Packt
Published: 2023-10-15T00:00:00+00:00


Utilizing ad callback methods

The code we wrote for the LoadLevel function works perfectly fine when we go to the main menu of the game; however, if we dive into the game itself from the main menu, the game will still be going on in the background with the ad blocking the player from playing the game.

When running your app on an actual mobile device, the Unity project will pause while Unity Ads are shown. However, if you are testing in the Unity Editor, the game is not paused while the placeholder ads are shown. However, we can simulate that behavior ourselves using the Advertisement.ShowOptions class.

We will pause the game when an ad is shown and then resume the game once the ad is finished. To do so, perform the following steps:

Let’s first open up the UnityAdController class and add the following variable and update the Start function to the following:/// <summary> /// A static reference to this object /// </summary> public static UnityAdController instance; /// <summary> /// Unity Ads must be initialized or else ads will not /// work properly /// </summary> private void Start() { /* No need to initialize if it already is done */ if (!Advertisement.isInitialized) { instance = this; // Use the functions provided by this to allow // custom Advertisement.Initialize(gameId, testMode); } }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.