I managed to test a simple app and the ads (banner and interstitial) work as expected (the test ones, because the real ones don't, idk why).
The problem is I want to integrate the ads into my main application. I did it for the banner and it works but don't know how to handle the interstitial. For the test app, I called a function when a button was pressed, but here in my app I don't want a button to be pressed in order to show an interstitial, I want this to happen every time the player dies. And I don't know how to do this.
using System;
using UnityEngine;
using GoogleMobileAds.Api;
public class ads : MonoBehaviour
{
private BannerView bannerView;
private InterstitialAd interstitial;
private void RequestBanner()...
private void RequestInterstitial()
{
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
this.interstitial = new InterstitialAd(adUnitId);
AdRequest request = new AdRequest.Builder().Build();
this.interstitial.LoadAd(request);
}
public void GameOverSoShowInterstitial()
{
if (this.interstitial.IsLoaded())
{
this.interstitial.Show();
}
}
void Start()
{
MobileAds.Initialize(initStatus => { });
this.RequestBanner();
this.RequestInterstitial();
}
}
^this is my ads script but it is located in the second scene named Menu, but since the interstitial should appear when the player dies, I should move that GameObject in the other scene that is the actual game, because it's impossible to die in the 'menu' section. And also, for checking when the ad has to appear I should use the GameManager, right? which is in the game scene, too.
public void GameOver()
{
gameOver = true;
gameScore = score.GetComponent<Score>().getScore();
score.SetActive(false);
Invoke("ActivateGameOverCanvas", 1);
pauseBtn.SetActive(false);
}
^this is the part with the gameover from gamemanager, so I think here I should introduce somehow an instruction that would generate the interstitial ad, but I don't have a clue. I tried to add the 'ads' script to gamemanager too, so at the end of the execution of GameOver function from gamemanager to call the GameOverSoShowInterstitial from ads, but it didn't work.
Any ideas? :(
1. Using objects from another Scene
When it comes to using scripts/objects from once Scene to another we generally put that object into a MonoBehaviour
built-in method DontDestroyOnLoad(object);
. By Using this method we can make sure that object
will not be destroyed when you change the scenes. Assuming that Menu your script is in Menu Scene you can Put that object in DontDestroyOnLoad() method and it will not get destroyed throughout the game.
2. Using the Method from Add script
For this we have multiple options. Like you can find the Object Using GameObject.FindObjectOfType()
or GameObject.FindGameObjectWithTag()
, etc. But using Gameobject.FindXXX() method can be heavy on performance side. The battre way is to use singleton pattern. In this you basically create an object when your Game Starts(When you need the object). And there will be no other object of the same type (it mean that there will be no other object with the same script attached to the object).
3. See both of the points in Action Your Ad script you don't have to change any thing just have to add the Singleton pattern.
using System;
using UnityEngine;
using GoogleMobileAds.Api;
public class ads : MonoBehaviour
{
//This is a static instance of this class so that you can access it from anywhere from you game, Without creating any creating a new Object.(Point #2)
public static ads instance {
get; //These are the getter and setter methods of native C# find the reference below
private set;
}
private Awake()
{
//Check if the instance is already assigned(if there is already an object with this same script attached)
if(instance == null)
{
instance = this; //If instance is empty the assign this object to the instance
DoNotDestroyOnLoad(this.gameObject); //This line will stop Unity from destroying the object when you load new scene.(Point #1)
}
else
{
Destroy(this.gameObject); //since there is already an object of with script destroy this object so that there will be no conflicts between multiple instances of the same class.
}
}
void Start()
{
MobileAds.Initialize(initStatus => { });
this.RequestBanner();
this.RequestInterstitial();
}
public void ShowInterstitialAd()
{
... Show your Ad
}
}
Game Manager Script
public void PlayerDie() //assuming that you have a method for when your Player dies
{
...
ads.instance.ShowInterstitialAd(); //Call the ShowInterstitialAd() method because the object will not be destroyed when you load another scene.
}
I'm basically done here. I hope this helps you. here are some of the reference that might be helpful to understand this further.