如何将admob广告加载到Unity 5项目中?

基本上我试图加载一个横幅广告到我的Unity 5项目并导出到iOS。

这里是我打电话给内部的代码,这是连接到一个游戏对象:

using UnityEngine; using System.Collections; using GoogleMobileAds.Api; using System; public class AdController : MonoBehaviour { InterstitialAd interstitial; BannerView bannerView; void Start () { //------ Banner Ad ------- // Create a 320x50 banner at the top of the screen. // Put your Admob banner ad id here bannerView = new BannerView( "ca-app-pub-xxxxxxxxxxxxxxxx", AdSize.SmartBanner, AdPosition.Top); // Create ad request AdRequest request = new AdRequest.Builder().Build(); // Load the banner with the request. bannerView.LoadAd(request); bannerView.Show(); //---- Interstitial Ad ----- // Initialize an InterstitialAd. // Put your admob interstitial ad id here: interstitial = new InterstitialAd("ca-app-pub-xxxxxxxxxxxxxxx"); //Add callback for when ad is loaded interstitial.AdLoaded += HandleAdLoaded; // Create an ad request. AdRequest requestInterstitial = new AdRequest.Builder().Build(); // Load the interstitial with the request. interstitial.LoadAd(requestInterstitial); } public void HandleAdLoaded(object sender, EventArgs args) { interstitial.Show (); } void OnDestroy(){ if (interstitial!=null) { interstitial.AdLoaded -= HandleAdLoaded; interstitial.Destroy (); } if(bannerView!=null){ bannerView.Destroy (); } } } 

我在用着:

  • Unity 5.0.1f1
  • Xcode 6.3
  • 谷歌统一插件2.2.1
  • Google广告SDK 7.2.1

有没有人得到这个服务广告? 注意:我没有用正确的广告单元IDreplacexxxxx。

  1. 添加一些代码到你的团结项目
    • 将这个c#类添加到你的Unity项目
    • 从你的项目中调用这个类的function
  2. 尽早调用构造函数“AD_AdsiOS”并传递AdMob广告ID
  3. 调用“ShowInterstitialAd”以显示全屏广告
  4. 调用“ShowBannerAdTopRight”在右上angular显示横幅广告
  5. 调用“HideBannerAds”隐藏横幅广告
 #if UNITY_IPHONE using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public class AD_AdsiOS { #if !UNITY_EDITOR [DllImport ("__Internal")] private static extern void _initAds(string p_adMobID); [DllImport ("__Internal")] private static extern void _showInterstitialAd(); [DllImport ("__Internal")] private static extern void _showBannerAdTopRight(); [DllImport ("__Internal")] private static extern void _hideBannerAds(); #endif public AD_AdsiOS(string p_adMobID) { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: will not work in editor."); #else _initAds(p_adMobID); #endif } public void ShowInterstitialAd() { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: ShowInterstitialAd called in editor."); #else _showInterstitialAd(); #endif } public void ShowBannerAdTopRight() { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: ShowBannerAdTopRight called in editor."); #else _showBannerAdTopRight(); #endif } public void HideBannerAds() { #if UNITY_EDITOR Debug.Log("AD_AdsiOS: HideBannerAds called in editor."); #else _hideBannerAds(); #endif } } #endif 

GitHub上有一个项目:

Unity Admob插件

它很容易使用,并且我已经成功了。

 using admob; ... Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-2796046890663330/756767388 Admob.Instance().showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);