- Home /
Unity Ads Some Users Cannot watch videos sometimes
Hello Community,
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,
jep7
@unity-nikkolai , I know, that you are a expert in unity ads themes, so mabye you can help? I thought about the problem and was wondering about the method Advertisement.Initialize(). On https://unityads.unity3d.com/help/Documentation%20for%20Publishers/Integration-Guide-for-Unity-Asset-Store it sais that 'You can initialize the framework only once', but when Initialisation failed (e. g. no network connection) I have to call ' Advertisement.Initialize()' again, haven't I? So I thought, that the problem could be, that the advertisement framework is not initialized sometimes and that is why Advertisement.isReady() returns false. But I also got the problem that videos are not playing any more after some video clips were playing fine? I really don't know the reason, mabye you have an idea how to fix the problem?
Answer by Scribe · Sep 05, 2015 at 11:22 AM
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
Thanks for your answer! I can't see directly a thing that I have forgotten in my application after reading your example script, but I got the idea, that the problem could be in the Advertisement.Initialize() call: I always check internet availability in my application and if it is available for the first time, i call Advertisement.Initialize(...). Do you know, what exactly this call does? I mean, could it be possible, that you have to initialize again, e g after loosing internet connection or $$anonymous$$imizing the app for some hours? Answer would be great, mabye I think totally in the wrong direction, but I can also give you more detailed information about my scripts, if necessary
By the way: I learned Time.realtimeSinceStartup from your script. It helps me a lot, until now I have used OnApplicationFocus() and a own server (not just for this) to get the 'real time since startup'. Thank you!
I don't know the internal workings I'm afraid, I have found something that may be of use, before trying to load an advert you could check:
Advertisement.isInitialized
as well as isReady
, if it is not initialized (`isInitialized == false`), then rerun your initialize code. I would guess loosing internet connection would require you to re-initialize.
$$anonymous$$y guess is, you initialize -> wait until isReady is true -> show an add.
but what happens is:
you initialize -> do some stuff -> loose connection -> you check if isReady -> it returns false and you never reinitialize -> it remains false -> no ads are shown until the app is force to close and reopened or the device restarted.
Answer by RandomCharacters · Nov 21, 2015 at 07:08 PM
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.
Answer by pablodarde · Jul 01, 2018 at 11:47 PM
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;
}
}
Your answer
Follow this Question
Related Questions
unity ads playmaker - review 0 Answers
Unity Ads controller support 0 Answers
Unity 5.5 ads not working 0 Answers
Does unity credit skipped ads? 0 Answers
'This organization has been suspended' unity ads not showing 1 Answer