I've got Unity 5.2 and I want to load an ad every time when I load a new scene. I added the Unity ad code into my script that changes the scene when I press a button. Here's the script:
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class UI1 : MonoBehaviour
{
public void ShowAd()
{
if (Advertisement.IsReady())
{
Advertisement.Show();
}
}
public void ChangeToScene(int sceneToChangeTo)
{
Application.LoadLevel(sceneToChangeTo);
}
}
How do I test to see if the script loads ads? I haven't published the app to the Google Play Store but I want to make sure the ads work.
I've tried using logs, but only "Changed Scene" showed when changing scenes.
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class UI1 : MonoBehaviour
{
public void ShowAd()
{
if (Advertisement.IsReady())
Debug.Log("Line 1 of ad script worked!");
{
Advertisement.Show();
Debug.Log("Line 2 of ad script worked, might be showing ads!!");
}
}
public void ChangeToScene(int sceneToChangeTo)
{
Application.LoadLevel(sceneToChangeTo);
Debug.Log("Changed scene!");
}
}
So the problem is that you simply never call Advertisement.Show()
. It is not called automatically on scene load or something, you have to call it. So, for example, you can modify your code a little, something like this:
public class UI1 : MonoBehaviour
{
void Start() {
// We use coroutine and not calling Show() directly because
// it is possible that at this point ads are not initialized yet
StartCoroutine(ShowAds());
}
IEnumerator ShowAds() {
if (Advertisement.IsReady()) {
Advertisement.Show();
yield break;
}
// Ads are not initialized yet, wait a little and try again
yield return new WaitForSeconds(1f);
if (Advertisement.IsReady()) {
Advertisement.Show();
yield break;
}
Debug.LogError("Something wrong");
}
public void ChangeToScene(int sceneToChangeTo) {
Application.LoadLevel(sceneToChangeTo);
}
}
You will also need to place an object of type UI1 in each scene, so that Start() function will be called in each scene.
You can continue from here. Actually there are many different ways to do it, here ads are called on each scene start, but you can also do, for example, before scene loads by modifying ChangeScene() function, or make one indestructible game object monitoring OnLevelWasLoaded() etc.