Adding Google Admob Plugin to Unity

You can download the plugin from this link: https://github.com/nabrozidhs/unity_admob_android

Example code is after the link:

Here’s the menu code:

using UnityEngine;
using System.Collections;

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

  public GUISkin skin;
  private const string AD_UNIT_ID = "Your unique ad unit ID";
  private const string INTERSTITIAL_ID = "Your unique interstitial ID";
  private AdMobPlugin admob;

  public bool hidden = true;

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

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

  // Update is called once per frame
  public void OnGUI()
  {
    GUI.skin = skin;
    skin.button.fixedWidth = Screen.width - 25;
    skin.textField.fixedWidth = Screen.width - 25;
    GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));

    GUILayout.BeginVertical("box");

    GUILayout.Label("Nabrozidhs AdMob Plugin");

    GUILayout.Space(20);

    //Share Status
    if (GUILayout.Button("Show Ad"))
    {
      if (hidden)
      {
        admob.ShowBanner();
        hidden = false;
      }
    }

    GUILayout.Space(20);

    //Achievement
    if (GUILayout.Button("Hide Ad"))
    {
      if (!hidden)
      {
        admob.HideBanner();
        hidden = true;
      }
    }

    GUILayout.Space(20);

    //Incremental Achievement
    if (GUILayout.Button("Show Interstitial"))
    {
      admob.RequestInterstitial();      
    }

    GUILayout.EndVertical();
    GUILayout.EndArea();
  }

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

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

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

2 comments

  1. i have this error what shold i do
    (23,25): error CS0411: The type arguments for method `UnityEngine.Component.GetComponent()’ cannot be inferred from the usage. Try specifying the type arguments explicitly

Leave a Reply

Your email address will not be published. Required fields are marked *