基本上我尝试将横幅广告加载到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 ();
}
}
}
我正在使用:
有人有这个广告吗?注意:我确实用正确的广告单元ID替换了xxxxx。
答案 0 :(得分:1)
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);
答案 1 :(得分:0)
#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
第1步完成:)
第2步完成:D
MyAdMobAdsImpl.h
#ifndef __Unity_iPhone__MyAdMobAdsImpl__
#define __Unity_iPhone__MyAdMobAdsImpl__
void InitMyAdMob(UIView* p_rootView, UIViewController* p_rootController);
void DestroyMyAdMob();
#endif /* defined(__Unity_iPhone__MyAdMobAdsImpl__) */
MyAdMobAdsImpl.mm
#import "GADBannerView.h"
#import "GADInterstitial.h"
#include "MyAdMobAdsImpl.h"
NSString* _adMobID;
UIViewController* _adRootViewController;
GADBannerView* _adBanner;
GADInterstitial *_adInterstitial;
float _adBannerRelWidth;
float _adBannerRelHeight;
extern "C"
{
void _initAds (const char* p_adMobID)
{
_adMobID = [NSString stringWithUTF8String:p_adMobID];
_adBanner.adUnitID = _adMobID;
_adInterstitial.adUnitID = _adMobID;
GADRequest *request = [GADRequest request];
[_adInterstitial loadRequest:request];
}
void _showInterstitialAd ()
{
[_adInterstitial presentFromRootViewController:_adRootViewController];
// get next add
_adInterstitial = [[GADInterstitial alloc] init];
_adInterstitial.adUnitID = _adMobID;
GADRequest *request = [GADRequest request];
[_adInterstitial loadRequest:request];
}
void _showBannerAdTopRight ()
{
_adBanner.hidden = NO;
// force refresh
GADRequest *request = [GADRequest request];
[_adBanner loadRequest:request];
}
void _hideBannerAds ()
{
_adBanner.hidden = YES;
}
float _getAdsRelativeWidth () { return _adBannerRelWidth; }
float _getAdsRelativeHeight () { return _adBannerRelHeight; }
}
void InitMyAdMob(UIView* p_rootView, UIViewController* p_rootController)
{
_adRootViewController = p_rootController;
// Initialize the banner at the bottom of the screen.
CGPoint origin = CGPointMake(0.0, 0.0);
// Use predefined GADAdSize constants to define the GADBannerView.
_adBanner = [[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin] autorelease];
[_adBanner setRootViewController:p_rootController];
[p_rootView addSubview:_adBanner];
// ios frame size is not rotated -> exchange width-height
_adBannerRelWidth = kGADAdSizeBanner.size.width / p_rootView.frame.size.height;
_adBannerRelHeight = kGADAdSizeBanner.size.height / p_rootView.frame.size.width;
_adBanner.center = CGPointMake(_adBanner.center.x+
MAX(p_rootView.frame.size.height-kGADAdSizeBanner.size.width,
p_rootView.frame.size.width-kGADAdSizeBanner.size.width),
_adBanner.center.y);
// Initialize the interstitial ad
_adInterstitial = [[GADInterstitial alloc] init];
}
void DestroyMyAdMob()
{
_adBanner.delegate = nil;
[_adBanner release];
[_adInterstitial release];
}
这两个文件实现了AdMob逻辑。使用此代码,可以在右上角显示和隐藏横幅广告,并显示全屏广告。此外,使用getAdsRelative *,您可以在相对屏幕空间中获得横幅的大小(宽度为0.1表示横幅占用0.1 * Screen.width像素)。为简单起见,这些方法不是第一步中的引用,但如果需要,可以使用它们。
//// ******************* ////
//// *** AdMob START *** ////
//// ******************* ////
#import "MyAdMobAdsImpl.h"
//// ******************* ////
//// *** AdMob END *** ////
//// ******************* ////
//// ******************* ////
//// *** AdMob START *** ////
//// ******************* ////
InitMyAdMob(_rootView, _rootController);
//// ******************* ////
//// *** AdMob END *** ////
//// ******************* ////
//// ******************* ////
//// *** AdMob START *** ////
//// ******************* ////
DestroyMyAdMob();
//// ******************* ////
//// *** AdMob END *** ////
//// ******************* ////
第3步完成:p
http://stackoverflow.com/questions/25950990/undefined-symbols-for-architecture-when-add-admob