The question is answered, right answer was accepted
Ads do not work. help me!
using UnityEngine; using System.Collections; using GoogleMobileAds; using GoogleMobileAds.Api;
public class interfasemenu : MonoBehaviour {
public Font Fonte;
private string Som = "Lig.";
public static InterstitialAd interstitial;
private BannerView bannerView;
void Start(){
Recorde2 = PlayerPrefs.GetFloat ("recorde");
interstitial = new InterstitialAd("ca-app-pub-xxxxxxxxxxxx");
AdRequest request = new AdRequest.Builder().Build();
interstitial.LoadAd(request);
bannerView = new BannerView("ca-app-pub-xxxxxxxxxxxxxxx", AdSize.Banner, AdPosition.TopRight);
AdRequest requestb = new AdRequest.Builder().Build();
bannerView.LoadAd(requestb);
}
this is my startup code.It is the main menu and put the script on camera.
start the game, before it starts, opens a screen where the rules of the game are shown, and this screen I put the following code:
using UnityEngine; using System.Collections; using GoogleMobileAds; using GoogleMobileAds.Api;
public class botaoregras : MonoBehaviour {
public Font Fonte;
void Start (){
if (interfasemenu.interstitial.IsLoaded ()) {
interfasemenu.interstitial.Show ();
}
}
void OnGUI (){
GUI.skin.font = Fonte;
GUI.skin.button.fontSize = Screen.height / 16;
if (GUI.Button (new Rect (Screen.width/2+Screen.width/4, Screen.height-Screen.height/5, Screen.width/5, Screen.height/8), "start")){
Application.LoadLevel ("game");
interfasemenu.interstitial.Destroy();
}
already signed up the game in admob, created the building blocks of ads, and the reports says this with 11 requests. but the views are cleared.
already I tried everything and will not appear the ads. Can anyone help me?
NeverHopeless thanks for the tip, but I am very very new to program$$anonymous$$g, and do not know how or where to use this code you gave me. I tried to use, but could not.
The interstitial needs time to load. I waited a long time before going to next screen to see if he was already loaded, and was not. Already the banners do not need much time to load, and banners are not showing up as well.
my code is correct?
I need a code as simple as possible because I'm starting now, this is my first game.
I see that the interstitial ads are very difficult to program. but if I can make the banners work, it's a start.
thanks in advance.
Be sure you don't have any sort of ad-blocker on your device. If you used to have one but uninstalled it, you might still have leftovers of it in your system folders.
Answer by NeverHopeless · Jul 01, 2015 at 05:53 AM
A couple of tips:
Are testing it in device ? Please use device for ads testing instead of unity editor (if it is the case).
You wrote this in
Start
function:if (interfasemenu.interstitial.IsLoaded ()) { interfasemenu.interstitial.Show (); }
It is possible that Ads is not loaded yet, so the if condition will fail. Use Admob delegates in interfasemenu
to use this condition, like:
public static void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
ShowInterstitial();
}
Hope it helps!
EDIT
Based on your comment you can check this demo script GoogleMobileAdsDemoScript.cs
available here: https://github.com/googleads/googleads-mobile-plugins
The event i highlighted is included in this script. Please use this script.
Change Admob ids in the script.
Adjust the event i highlighted. So once the ad loaded successfully only then show it.
Attach this script to camera. (for example)
From your script, just call:
Camera.main.GetComponent<GoogleMobileAdsDemoScript>().RequestBanner(); Camera.main.GetComponent<GoogleMobileAdsDemoScript>().RequestInterstitial();
Make RequestBanner
and RequestInterstitial
as public
functions in the script.
I hope then you are good to go.
The link is valid but there was a dot character at the end which was not valid with url. try now.
now worked, I download and try. thanks
after I post here if it worked or not.
Thank you for trying to help me. But it did not work, did everything as you said. I do not know what might be wrong.
@Gamerdrill, could you please update your question with latest code ?
Answer by CoreApp2 · May 02, 2016 at 07:30 PM
Please i have the same issue, can someone please help me
public class ScoreScript : MonoBehaviour
{
static public bool checkInter = true;
static private int countCallIntUp = 0;
void Awake()
{
RequestInterstitial();
RequestBanner();
}
void Update()
{
if (checkInter == false)
{
ShowInterstitial();
checkInter = true;
}
}
IEnumerator OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Physics.gravity = new Vector3(0, -9.81f, 0);
countCallIntUp++;
if (countCallIntUp == 3)
{
countCallIntUp = 0;
checkInter = false;
}
other.gameObject.SetActive(false);
yield return new WaitForSeconds(0.3f);
SetCountText();
imageScore.SetActive(true);
}
}
//=========================================#
// Google Ad Mob
//public static string OutputMessage
//{
// set { outputMessage = value; }
//}
public void BannerViewShow()
{
bannerView.Show();
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxx/xxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Register for ad events.
bannerView.OnAdLoaded += HandleAdLoaded;
bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
bannerView.OnAdLoaded += HandleAdOpened;
bannerView.OnAdClosed += HandleAdClosed;
bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
// Load a banner ad.
//bannerView.LoadAd(new AdRequest.Builder().Build());
bannerView.LoadAd(createAdRequest());
}
private void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.OnAdLoaded += HandleInterstitialLoaded;
interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.OnAdOpening += HandleInterstitialOpened;
interstitial.OnAdClosed += HandleInterstitialClosed;
interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad.
//interstitial.LoadAd(new AdRequest.Builder().Build());
interstitial.LoadAd(createAdRequest());
}
//Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
// private void RequestRewardBasedVideo()
// {
//#if UNITY_EDITOR
// string adUnitId = "unused";
//#elif UNITY_ANDROID
// string adUnitId = "INSERT_ANDROID_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
//#elif UNITY_IPHONE
// string adUnitId = "INSERT_IOS_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
//#else
// string adUnitId = "unexpected_platform";
//#endif
// rewardBasedVideo.LoadAd(new AdRequest.Builder().Build(), adUnitId);
// }
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
print("Interstitial is not ready yet.");
}
}
//public void ShowRewardBasedVideo()
//{
// if (rewardBasedVideo.IsLoaded())
// {
// rewardBasedVideo.Show();
// }
// else
// {
// print("Reward based video ad is not ready yet.");
// }
//}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
print("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
bannerView.Destroy();
RequestBanner();
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
print("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
interstitial.Destroy();
RequestInterstitial();
print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
}
Your code that requests an ad seems to be correct but you are not showing the ad when it loads successfully.
public void HandleAdLoaded(object sender, EventArgs args)
{
print("HandleAdLoaded event received.");
BannerViewShow(); // <--------------------------------- Display the loaded ad
}
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
ShowInterstitial(); // <---------------------- Display the loaded interstitial
}
Beside this double check your ad mob ids, and run it on real device.
Happy coding!!!
Thank you for the answer. It seems to work. I have questions please. Is it necessary to handle all this? I mean for instance, can i add in HandleInterstitialClosing() and HandleInterstitialFailedToLoad() ,the function RequestInterstital() , just to play it safe, when an interstitial does not load?
Glad to see you made it. :)
Regarding your question, it is up to YOU when you have to request an interstitial. AFAI$$anonymous$$, if you request too frequently (for example on closing ad you request an ad, and on load of this ad, the ad displays), they may block your bundle. Also, for a case of failure, perhaps the network is down it will keep sending request no matter what is the cause of failure, so make your request only when REALLY needed. It is observed that the interstital requests on some events like load of screen, game pause etc.
Don't ask a question in an answer to a question.
Also you don't have to mention every user who commented on the question in every comment, they will receive an email anyways when you comment.
If you mention them, they will receive another email for he same purpose, which is annoying. ;)
Answer by Gamerdrill · Jul 07, 2015 at 07:23 AM
using System; using UnityEngine; using GoogleMobileAds; using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoHandler : IInAppPurchaseHandler { private readonly string[] validSkus = { "android.test.purchased" };
//Will only be sent on a success.
public void OnInAppPurchaseFinished(IInAppPurchaseResult result)
{
result.FinishPurchase();
GoogleMobileAdsDemoScript.OutputMessage = "Purchase Succeeded! Credit user here.";
}
//Check SKU against valid SKUs.
public bool IsValidPurchase(string sku)
{
foreach(string validSku in validSkus) {
if (sku == validSku) {
return true;
}
}
return false;
}
//Return the app's public key.
public string AndroidPublicKey
{
//In a real app, return public key instead of null.
get { return null; }
}
}
// Example script showing how to invoke the Google Mobile Ads Unity plugin. public class GoogleMobileAdsDemoScript : MonoBehaviour {
private BannerView bannerView;
private InterstitialAd interstitial;
private static string outputMessage = "";
public static string OutputMessage
{
set { outputMessage = value; }
}
public void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_ANDROID
string adUnitId = "INSERT_ANDROID_BANNER_AD_UNIT_ID_HERE";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.TopRight);
// Register for ad events.
bannerView.AdLoaded += HandleAdLoaded;
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
bannerView.AdOpened += HandleAdOpened;
bannerView.AdClosing += HandleAdClosing;
bannerView.AdClosed += HandleAdClosed;
bannerView.AdLeftApplication += HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(createAdRequest());
}
public void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_ANDROID
string adUnitId = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.AdLoaded += HandleInterstitialLoaded;
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.AdOpened += HandleInterstitialOpened;
interstitial.AdClosing += HandleInterstitialClosing;
interstitial.AdClosed += HandleInterstitialClosed;
interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
GoogleMobileAdsDemoHandler handler = new GoogleMobileAdsDemoHandler();
interstitial.SetInAppPurchaseHandler(handler);
// Load an interstitial ad.
interstitial.LoadAd(createAdRequest());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
public void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
print("Interstitial is not ready yet.");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
print("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
ShowInterstitial();
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
print("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
}
this is the code "GoogleMobileAdsDemoScript" where I put my adUnityid and put this script in camera.
then. in my script from the main menu interface. which also is in the camera. I put like this:
void Start(){
Camera.main.GetComponent<GoogleMobileAdsDemoScript>().RequestBanner();
Camera.main.GetComponent<GoogleMobileAdsDemoScript>().RequestInterstitial();
}
void OnGUI (){
GUI.skin.font = Fonte;
GUI.skin.button.fontSize = Screen.height / 16;
GUI.skin.label.fontSize = Screen.height / 16;
GUI.Label (new Rect (Screen.width/4-Screen.width/6, Screen.height/2+Screen.height/3,500,100), Recorde1 + Recorde2);
if (GUI.Button (new Rect (Screen.width / 4 - Screen.width / 6, Screen.height / 2 + Screen.height / 16, Screen.width / 3, Screen.height / 7), "New Game")) {
Application.LoadLevel ("game");
Camera.main.GetComponent<GoogleMobileAdsDemoScript> ().ShowInterstitial ();
Neither the interstitial nor the Banner are working.
all scripts work normally, everything is in order, but sometimes I see this error in the console:
Asset 'C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll' is in timestamps but is not known in guidmapper...
can you be wrong?
Answer by Gamerdrill · Jul 07, 2015 at 07:24 AM
I used the code you gave me and modified as you said. and I put it in the camera
then I put the script from my main menu, the following:
void Start(){
Camera.main.GetComponent<GoogleMobileAdsDemoScript>().RequestBanner();
Camera.main.GetComponent<GoogleMobileAdsDemoScript>().RequestInterstitial();
}
void OnGUI (){
GUI.skin.font = Fonte;
GUI.skin.button.fontSize = Screen.height / 16;
GUI.skin.label.fontSize = Screen.height / 16;
GUI.Label (new Rect (Screen.width / 4 - Screen.width / 6, Screen.height / 2 + Screen.height / 3, 500, 100), Recorde1 + Recorde2);
if (GUI.Button (new Rect (Screen.width / 4 - Screen.width / 6, Screen.height / 2 + Screen.height / 16, Screen.width / 3, Screen.height / 7), "New Game")) {
Application.LoadLevel ("game");
Camera.main.GetComponent<GoogleMobileAdsDemoScript> ().ShowInterstitial ();
}
}
I realized also that is showing the following error:
Timestamps (437) and assets (435) maps out of sync.
Asset 'C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll' is in timestamps but is not known in guidmapper...
will this be the problem?
The code looks good to me. can u please check the admob in a seperate test app to see if it is working there? the error you are highlighting now doesn't seems to be relevant to admob. Could you please give it a try?
I modified the code and now it works.
Thank you for the support.
I changed this:
#if UNITY_EDITOR adUnitId string = "ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXX"; #elif UNITY_ANDROID adUnitId string = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
For this:
#if UNITY_EDITOR adUnitId string = "unused"; #elif UNITY_ANDROID adUnitId string = "ca-app-pub-XXXXXXXXXXXXXXXXXXXXXXXXX";