I plan to upload an Android application soon, and i’m using Unity Ads for the first time with Unity 5.3.1(f1).
Integration was really easy, but I have some issues when it comes to test on my Android device (Zenfone 2, Android 5.0). With “Test mode” enabled no problem, the Unity Ads is showing if my data connection is active.
Without “Test mode”, I have two kinds of problems for now :
I got the ad if my datas connection was active. But if it wasn’t on the first request, even if I activate my data connection after that, Advertisement.IsReady(id) always return false. I had to close and restart the game to get an ad ! This is I think the most critical problem.
(Edit : this second point is now working after 6~7h, don’t know why) : created two more video placement on the admin panel (“videoMute” and “rewardedVideoMute”). I don’t know if it’s because of that but Advertisement.IsReady(id) always return false for now, with or without data connection activated (and even with a placement that was working yesterday) !
I don’t think I made a mistake in the code :
public void ShowAd()
{
//audioAdsActivated : boolean controlled by button switch
if (audioAdsActivated && Advertisement.IsReady("rewardedVideo"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show("rewardedVideo", options);
}
else if (!audioAdsActivated && Advertisement.IsReady("rewardedVideoMute"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show("rewardedVideoMute", options);
}
else
{
debugTextAds.color = Color.red;
debugTextAds.text = "The ad isn't ready.\n Check your datas connection !";
}
}
private void HandleShowResult(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
debugTextAds.color = new Color32(20,150,20,255);
debugTextAds.text = "The ad was successfully shown.";
rewardUser();
break;
case ShowResult.Skipped:
debugTextAds.color = Color.red;
debugTextAds.text = "The ad was skipped before reaching the end.";
break;
case ShowResult.Failed:
debugTextAds.color = Color.red;
debugTextAds.text = "The ad failed to be shown.";
break;
}
}
Placement ID checked (“rewardedVideo”, “rewardedVideoMute”), Android GameID checked on both side (Unity and admin panel). Working with “Test mode”.
Before initializing Unity Ads, check for Internet connectivity in e.g. a coroutine and manually Initialize Unity Ads after verifying an Internet connection exists.
For the Services window -based integrations: To disable the initialization of Unity Ads on startup, create a new C# script called UnityAdsBuildProcessor, and place it within the Editor folder in your project. Then copy the following code into the script:
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.Advertisements;
public class UnityAdsBuildProcessor : Editor
{
[PostProcessScene]
public static void OnPostprocessScene ()
{
AdvertisementSettings.enabled = true;
AdvertisementSettings.initializeOnStartup = false;
}
}
Thank you for your help in private messages mikaisomaa !
So yes the problem is that UnityAdsService failed to initialize properly at the start of the application if there is no internet connection.
My workaround isn’t really diffcult to implement:
first I have to disable the initialization of Unity Ads on startup (check previous post from mikaisomaa for this part)
I launched at start a coroutine which check for “google.com” every two seconds while it’s not connected, and I manually start the initialization of UnityAds with “Advertisement.Initialize(gameId);” when a connection is detected. Here is the code :
private string androidGameID = "##SECRET##";
private string iosGameID = "##SECRET##";
private bool unityAdsInitialized = false;//can be used for informing the user about the status
public IEnumerator checkInternetConnection()
{
float timeCheck = 2.0f;//will check google.com every two seconds
float t1;
float t2;
while(!unityAdsInitialized)
{
WWW www = new WWW("http://google.com");
t1 = Time.fixedTime;
yield return www;
if (www.error == null)
{
#if UNITY_ANDROID
Advertisement.Initialize(androidGameID); // initialize Unity Ads.
#elif UNITY_IOS
Advertisement.Initialize(iosGameID); // initialize Unity Ads.
#endif
unityAdsInitialized = true;
break;//will end the coroutine
}
t2 = Time.fixedTime - t1;
if(t2 < timeCheck)
yield return new WaitForSeconds(timeCheck - t2);
}
}
Ads are now working with an internet connection even if the internet connection isn’t active at the start of the application !
Note : you can’t only try to check for internet connection and initialize UnityAds when you want to do your “Advertisement.IsReady()”, because it will take some time to request the ad, and it will not be ready.
Hi, this either doesnt work for me, or i must be misunderstanding something
Ive put UnityAdsBuildProcessor.cs with the above code inside Assets/Editor
Ive created empty GameObject, added it to scene, attached simple script that checks Advertisement.isInitialized on Start() - it traces TRUE.
Also console traces: UnityAdsEditor: Initialize({ProjectId}, True);
Dragonic89 where did you launch your coroutine to check for google.com? did you make a new script for it?
I made a new script and I tried your code, but when I tested it on my mobile device Advertisement.Initialize(gameID); is not returning to true or not Initialize even if I’m already connected to an internet.
Thank you in advance for your help.
btw, I’m using Unity 5.4.2f2.
That was one year ago. I don’t know if this script can still be used like that. Since this time UnityAds has evolved !
I launched this coroutine from my main script in my game. But this could be done in any script as long as the script with the coroutine is attached to an active Gameobject I think.
I manage to Initialize the Unity Ads but the problem now is that, the Advertisement.isReady() was not going to true.
Here’s the script I’m using. I don’t think made some mistake.
Thanks in advance.
void Update () {
if (coolDownEffect) {
Invoke ("ResetCooldown", 20);
}
if (thePlayer.deathCheck) { // to check if the player's character is dead
ShowAd ();
}
}
public void ShowAd(){
if (buttonCooldown == false) {
if (Advertisement.IsReady ()) {
theButton.SetActive (true); //button to watch the ads
}
}
}
public void WatchAds(){
if (buttonCooldown == false) {
Advertisement.Show ("rewardedVideo", new ShowOptions (){ resultCallback = HandleResult });
theButton.SetActive (false);
coolDownEffect = true;
buttonCooldown = true;
}
}
void ResetCooldown(){
buttonCooldown = false;
}
private void HandleResult(ShowResult result){
switch (result)
{
case ShowResult.Finished:
Debug.Log ("Player gains +30 coins");
break;
case ShowResult.Skipped:
Debug.Log ("Player Skipped the Ads");
break;
case ShowResult.Failed:
Debug.Log ("Player failed to launch the ad ?Internet?");
break;
}
}