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 项目中?
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01