I have a problem regarding Unity Video Ads:
I have implemented video ads in my android game and first, everything
was working fine. Now, some friends are testing it and they sometimes get the problem,
that they cannot watch more videos although they have a perfect internet connection.
They did not have this fault directly after installing, and also they were able to watch videos again after some day or so.
I have checked the code and found out, that (probably) this lines are responible for the difference (ads are working/are not working):
while (!Advertisement.isReady (zone)) { yield return null; }
Advertisement.Show (zone, options);
In the case, where the ad cannot be watched, ‘Advertisement.isReady(zone)’ always returns false, so the loop is never finished.
But I don’t know, why sometimes the ad is ready (so I can show it) and sometimes not.
I really need to fix this problem, it would be really great, if anyone could help…
Thanks,
you may want to ask in the forum ads section if this doesn’t help, I do not think nikkolai is active on Answers.
This code is untested I am afraid, however the idea behind it is that if the a connection isn’t made in x amount of time, we restart the initialize. Maybe the is broken logic, as we don’t know for sure how long the connection should take for everyone, but it’s something!
void Awake() {
if (Advertisement.isSupported) {
StartCoroutine(AdConnect(<GAMEID>, zone, 10f, 5));
//I chose 10 seconds to retry after, this is arbitrary
//you should base this on what you find to be the average time needed to connect (plus some buffer amount, like multiply the average time by 2)
} else {
Debug.Log("Platform not supported");
}
}
void AdConnect(int ID, string zone = null, float timeOut, int cutoff){
Advertisement.Initialize (<YOUR GAME ID HERE>);
float startTime;
for(int i = 0; i < cutoff; i++){
startTime = Time.realtimeSinceStartup;
while(!Advertisement.IsReady(zone)){
yield return null;
if(Time.realtimeSinceStartup - startTime >= timeOut) break;
}
if(Advertisement.IsReady(zone)){
IsReadyCallback();
return true;
}
}
FailedCallback(cutoff);
}
void IsReadyCallback(){
Debug.Log("ready!");
//start advertising somewhere
}
void FailedCallback(int c){
Debug.Log("failed to connect in "+ c +" tries");
//try again with a larger connection time?
}
Maybe that is useful, though it sounds like it is not the easiest thing to test if it only happens after a while (strange) and occasionally :S
I have the same problem. Ads display in the unity editor but in android sometimes just do not work. This sucks cause I switched over to unity ads. Players watch ads to get coins and now they can’t get any coins.
I was facing some problems with the time Ads takes to load. I made some changes in the @Scribe 's code. Now, If Ads takes too long time to load, I cancel the load and redirect the user to the main Scene. Please, let me know if someone has a better solution.
That’s the code.
private void Awake()
{
if (Advertisement.isSupported)
{
StartCoroutine(AdConnect(gameID, "video", 10f, 5));
//I chose 10 seconds to retry after, this is arbitrary
//you should base this on what you find to be the average time needed to connect (plus some buffer amount, like multiply the average time by 2)
}
else
{
Debug.Log("Platform not supported");
}
}
public void Start()
{
textWait.text = locale.Text.wait;
}
IEnumerator AdConnect(string ID, string zone, float timeOut, int cutoff)
{
Advertisement.Initialize(ID);
float startTime;
for (int i = 0; i < cutoff; i++)
{
startTime = Time.realtimeSinceStartup;
while (!Advertisement.IsReady(zone))
{
yield return null;
if (Time.realtimeSinceStartup - startTime >= timeOut)
{
break;
}
}
if (Advertisement.IsReady(zone))
{
IsReadyCallback();
yield return true;
} else
{
FailedCallback(cutoff);
break;
}
}
}
void IsReadyCallback()
{
Debug.Log("ready!");
ShowOptions options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show("video", options);
}
void FailedCallback(int c)
{
Debug.Log("failed to connect in " + c + " tries");
//try again with a larger connection time?
SceneManager.LoadScene("Main");
}
private void HandleShowResult(ShowResult result)
{
// SceneManager.LoadScene("Main");
switch (result)
{
case ShowResult.Finished:
SceneManager.LoadScene("Main");
// Debug.Log("The ad was successfully shown.");
//
// YOUR CODE TO REWARD THE GAMER
// Give coins etc.
break;
case ShowResult.Skipped:
SceneManager.LoadScene("Main");
// Debug.Log("The ad was skipped before reaching the end.");
break;
case ShowResult.Failed:
SceneManager.LoadScene("Main");
// Debug.LogError("The ad failed to be shown.");
break;
}
}