I have been trying for like a month now to properly implement Google Play Services and Google Mobile Ads into my Unity Android Game. I have had success in getting Google Play Services to work but when I added Google Mobile Ads at first it didn't work. I just didn't see any ads pop up. Now when I build my app it won't open it just crashes. Not exactly sure what I did. I have configured all the services correctly according to my knowledge and I'm am just using code from the documentation for the ads. Heres that code:

public static AdManager Instance { set; get; }

public string bannerId;
public string videoId;
public string deviceId;
BannerView bannerView;
InterstitialAd interstitial;

private void Start ()
{
    Instance = this;
    DontDestroyOnLoad(gameObject);
    interstitial = new InterstitialAd(videoId);
    AdRequest request = new AdRequest.Builder().AddTestDevice(deviceId).Build();
    // Load the banner with the request.
    interstitial.LoadAd(request);
}

public void ShowInterstitial()
{
    if (interstitial.IsLoaded())
        interstitial.Show();
}

I am using the correct ad Ids, I double checked that. I don't know what else to do. I can provide any other information you may need upon request.

Anyone with experience with this would be super helpful! Thanks!

  • When the game crashes on the device do you get a message on screen? – Tartle Wizard Feb 21 '17 at 16:55
  • No, there's nothing to tell me whats wrong. I am using an emulator but I didn't think that would affect anything, does it? @TartleWizard – Sam R Feb 22 '17 at 18:16
  • I recently had an issue like this with Unity 5 and I had to update my jdk install and I redownloaded and updated all of android studio. After that my game ran with the services and the ads. – Tartle Wizard Feb 22 '17 at 20:05

I've used admob in my earlier work. Check the code here;

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AdMobPlugin))]
public class ReklamAdmob : MonoBehaviour
{

public GUISkin skin;
private const string AD_UNIT_ID = "YourAdUnitId";
private const string INTERSTITIAL_ID = "YourInterstirialId";
private AdMobPlugin admob;
public Score other2;

public bool hidden = true;


public void Awake()
{
    DontDestroyOnLoad(this);
}

public void Start()
{
    admob = GetComponent<AdMobPlugin>();
    admob.CreateBanner(AD_UNIT_ID, AdMobPlugin.AdSize.SMART_BANNER, true, INTERSTITIAL_ID);
    admob.RequestAd();
    admob.HideBanner();
}

// Update is called once per frame

public void Update()
{
    if(other2.CurrentScore<=0 && hidden==true)
    {
        admob.RequestInterstitial();
        hidden=false;
    }

}


void OnEnable()
{
    AdMobPlugin.InterstitialLoaded += HandleInterstitialLoaded;
}

void OnDisable()
{
    AdMobPlugin.InterstitialLoaded -= HandleInterstitialLoaded;
}

void HandleInterstitialLoaded()
{
    admob.ShowInterstitial();
}
}

But i've used Unity Ads, which seems better for me.

If you did not check it yet look here. Implementation is much easier.

  • Admob and Google Mobile Ads are a little different. There are two different repositories for the unity plugins. I tried using Admob first but a friend of mine told me Google Mobile Ads is easier to implement so I switched to that. @EmreE – Sam R Feb 22 '17 at 18:14

Here's the official guide on implementing AdMob to your game: https://developers.google.com/admob/unity/start

You basically download the scene on top of this website and import it through Assets -> Import -> Custom Package.

Then you restart Unity and your code editor to apply library changes and make the namespace visible.

Then, you create a script called AdmobManager and put inside:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;

public class AdmobManager : MonoBehaviour {

    private InterstitialAd staticAd;

    void Awake () {
        LoadStaticAd(); // load it first time
    }

    private void LoadStaticAd(){
        if(staticAd != null) staticAd.Destroy();
        staticAd = new InterstitialAd("ca-app-pub-###/###"); // put here the ID of the static ad from the admob website
        AdRequest request = new AdRequest.Builder()
        // you can add a test device id to test the ads on your phone before releasing the game
        // download this app: https://play.google.com/store/apps/details?id=pe.go_com.admobdeviceidfinder&hl=pl
        // to find the correct id of your device
        //  .AddTestDevice("################################").Build(); 
            .Build();

        staticAd.LoadAd(request);
        staticAd.OnAdClosed += OnAdClosed; // ad a listener to reload the ad when user finishes watching it
    }


    private void OnAdClosed(object ob, EventArgs args){
        LoadStaticAd(); // when the ad is closed, reload the ad in background
    }


    public void ShowStaticAd(){
        if(staticAd != null && staticAd.IsLoaded()){ // safety checks
            staticAd.Show();
        }
    }   

    private static AdmobManager instance; // singleton pattern to access the AdmobManager anywhere
    public static AdmobManager Instance() {
        if(!instance){
            instance = FindObjectOfType(typeof (AdmobManager)) as AdmobManager;
            if(!instance)
                Debug.Log("There need to be at least one active AdmobManager on the scene");
        }

        return instance;
    }
}

Remember to replace the ad ID with your own from the AdMob website. You can also put your DeviceID to test the ads when you're in the Beta phase and the game isn't published yet.

To show an ad in the game, you do:

void LevelFinished(){
    AdmobManager.Instance().ShowStaticAd();
}

And that's it.

There are two ways on implementing this. One from the tutorial above and one from the Unity Admob Plugin. I've tried 6 times implementing Admob Plugin from github, always crashes the device, ALWAYS. This guide above worked for 4 games so far on Unity 5.5 and Unity 5.6.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.