框架視圖
效果圖
關(guān)鍵代碼
GoogleAdManager
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;//摻入廣告命名空間
public class GoogleAdManager : MonoBehaviour
{
public string adUnitId = "ca-app-pub-5711132426115648/1023732213";
// Use this for initialization
void Start()
{
//橫幅廣告
//摻入廣告
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);//參數(shù):廣告id,廣告尺寸(橫幅),位置;
//構(gòu)建廣告
AdRequest request = new AdRequest.Builder().Build();
//加載廣告
bannerView.LoadAd(request);
//bannerView.Show ();
}
}
UnityAd
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class UnityAd : MonoBehaviour
{
void Start()
{
Advertisement.Initialize("1321938", true);
}
public void ShowAd()
{
print(Advertisement.IsReady());
if (Advertisement.IsReady())
{
Advertisement.Show();
}
}
void Update()
{
if (Advertisement.IsReady() && !Advertisement.isShowing)
{
ShowAd();
}
}
public void ShowRewardedAd()
{
if (Advertisement.IsReady("rewardedVideo"))
{
var options = new ShowOptions { resultCallback = HandleShowResult };
Advertisement.Show("rewardedVideo", options);
}
}
private void HandleShowResult(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
Debug.Log("The ad was successfully shown.");
//
// YOUR CODE TO REWARD THE GAMER
// Give coins etc.
break;
case ShowResult.Skipped:
Debug.Log("The ad was skipped before reaching the end.");
break;
case ShowResult.Failed:
Debug.LogError("The ad failed to be shown.");
break;
}
}
}