How to load admob ads into a unity 5 project?(如何将 admob 广告加载到 unity 5 项目中?)
问题描述
基本上我正在尝试将横幅广告加载到我的 Unity 5 项目中并导出到 iOS.
Basically I'm trying to load a banner ad into my Unity 5 project and export to iOS.
这是我在 unity 内部调用的代码,它附加到游戏对象:
Here is the code i'm calling inside of unity which is attached to a game object:
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
- Google Unity 插件 2.2.1
- Google Ads SDK 7.2.1
有人用这个来投放广告吗?注意:我确实将 xxxxx 替换为正确的广告单元 ID.
Has anyone got this to serve ads? Note: I did replace the xxxxx with the correct Ad Unit ID.
推荐答案
GitHub上有一个项目:
There is an project on GitHub:
Unity Admob 插件
它易于使用,我在这方面取得了成功.
it easy to use,and I have success with this.
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);
这篇关于如何将 admob 广告加载到 unity 5 项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 admob 广告加载到 unity 5 项目中?
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01