基本上,我正在尝试将横幅广告加载到我的 Unity 5 项目中并导出到 iOS。
这是我在 unity 中调用的代码,它附加到一个游戏对象:
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 ();
}
}
}
我正在使用:
- 统一 5.0.1f1
- Xcode 6.3
- Google Unity 插件 2.2.1
- 谷歌广告 SDK 7.2.1
有人有这个来转换广告吗?注意:我确实用正确的广告单元 ID 替换了 xxxxx。
最佳答案
GitHub上有一个项目:
它易于使用,而且我在这方面取得了成功。
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);
关于c# - 如何将 admob 广告加载到 unity 5 项目中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30062841/