Hello, Tudor here! I’m creating a game called Cube Quest. I have this code putted on the camera:
usingSystem; usingUnityEngine; usingUnityEngine.Advertisements; publicclassUnityAdsStart:MonoBehaviour{ voidAwake(){ if(Advertisement.isSupported){ Advertisement.allowPrecache =true; Advertisement.Initialize(“BlaBlaBla”); }else{ Debug.Log(“Platform not supported”); } } voidStart(){ if(Advertisement.isReady()){ Advertisement.Show(); PlayerPrefs.SetInt(“CubePointsLvl”,PlayerPrefs.GetInt(“CubePointsLvl”)+50); } } }
In the editor, it is working, but on my phone (Android) it is not. So what is the problem?
I think it is a initialization problem. Could you please confirm your test ads on anroid device not working.
You should use this code to enable test mode. Advertisement.Initialize(“Your ID”,true);
Initialization on awake good but , trying to show a live ad suddenly after game launch is not a good practice. Probably isReady() function returns false with this script.
Keep reading this forum, you can find best practices from here.
Regards,
ps. I dont know if its abusable but dont share your id’s on a public forum if its your real id.
Agree with Salazar; not only is the launch ad irritating, it usually does not work at all: the problem here is that you are initialising and trying to start the ad quite close to each other (in wall clock time).
When you initialize your game, it contacts our servers and fetches your configuration and an ad-plan (ie. a list of ads in the order they are supposed to be shown). While the server responds in sub-150ms (98% cases) there is also the network latency.
In your example the game has not been able to get the response from the servers yet.
In your case I would try to separate those two calls and try to figure out a better place where you can show the ads (=later in the game).
The methods in the Advertisements subsystem are static, meaning that you can call the methods from different scenes and they work just fine.
It generally is not abusable, but yes; do not share your real Id’s in public forums. (Might be a good idea to edit your first post)
I have changed my ID. How should my final code look? Because I have tried with different methods and it isn’t working. And my phone supports Unity Ads. Sorry for disturbation, but I am new to this and I really need your help.
Sincerly,
Tudor
And I have tried with the example code but the button shows me only “Waiting…”, no “Show ad”. I have also tried on Andy (Android Pc emulator) same result.
Me again. Ok. I have resolved it. For those who wants the script: using UnityEngine; using System.Collections; using UnityEngine.Advertisements; public class : MonoBehaviour { void Start () { Advertisement.Initialize ("<YourUnityAdsGameIdHere); } void Update () { if(Advertisement.isReady()){ ShowAd(); } } void ShowAd(){ Advertisement.Show(); } }
The script is made by me. Now it is working. Thank you Salazar and HeikkiTunkelo!
Its not a good practice again =(
With using this code block in Update function, you are checking if ad ready in every frame. Advertisement.isReady() will return true in every frame and your game tries to show ad in every frame.
You should move this code block into something like button click, key pressed, after game over…
if button clicked and advertisement ready >> show ad
if something happened and advertisement ready >> show ad
If you want to show an advertisement to your users when the application starts up, you can use yield statements to check and see if ads are ready every second (or frame, depending on the yield statement), before attempting to show the ad. If you do use a looping check like this, it’s best to also provide a break point in cases were Unity Ads isn’t able to be initialized for some reason, or if ads simply aren’t available.
In the example provided here, a timer is used. If it takes longer than 15 seconds for ads to be ready after Unity Ads is initialized, the process of showing an ad on start is canceled, and a debug message stating that case is sent to the console.
Hello,
I am also having problems with ad initialization on Android. Here’s my code:
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class ShowAds : MonoBehaviour {
public Transform self;
public bool init = false;
public bool showGUI = false;
// Use this for initialization
void InitializeMyAdOnRequest ()
{
Advertisement.Initialize ("myID", true);
init = true;
}
public void OnGUI()
{
if (showGUI)
{
if (Advertisement.isInitialized) GUI.Label (new Rect (10, 50, 200, 25), "Initializing done.");
else if (init) GUI.Label (new Rect (10, 50, 100, 25), "Initializing AD...");
else GUI.Label (new Rect (10, 50, 200, 25), "No AD request. (on cooldown)");
if (init)
{
if (!Advertisement.IsReady()) GUI.Label (new Rect (10, 100, 200, 25), "Ad loading...");
else GUI.Label (new Rect (10, 100, 200, 25), "Waiting for button...");
}
}
}
public void WinAd()
{
showGUI = true;
}
public void ShowAd(string zone = "")
{ Debug.Log ("requestRecieved");
#if UNITY_EDITOR
StartCoroutine(WaitForAd ());
#endif
//if (string.Equals (zone, ""))
// zone = "rewardedVideoZone";
ShowOptions options = new ShowOptions ();
options.resultCallback = AdCallbackhandler;
if (Advertisement.IsReady ()) {
Advertisement.Show (null ,options);
}
}
void AdCallbackhandler (ShowResult result)
{
switch(result)
{
case ShowResult.Finished:
Debug.Log ("Ad Finished. Rewarding player...");
self.SendMessage("AdEnd", 1);
break;
case ShowResult.Skipped:
Debug.Log ("Ad skipped. Son, I am dissapointed in you");
self.SendMessage("AdEnd", 2);
break;
case ShowResult.Failed:
Debug.Log("I swear this has never happened to me before");
self.SendMessage("AdEnd", 3);
break;
}
}
IEnumerator WaitForAd()
{
float currentTimeScale = Time.timeScale;
Time.timeScale = 0f;
yield return null;
while (Advertisement.isShowing)
yield return null;
Time.timeScale = currentTimeScale;
}
}
This code from: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/integrating-ads just a bit modified.
InitializeMyAdOnRequest() is called after player enters battle, WinAd() if player wins battle and ShowAd() if player clicks button to show AD.
But my OnGUI() always shows “Initializing AD…” and “Ad loading…” no matter how long I wait.
In editor everything works fine.
My game is unpublished and I tried both development build and release build (uncheck development build) in build settings.