Ⅰ.添加广告并真机测试

1.用示例广告ID演示Admob Unity 插件

 

参考(admob官方文字教程)

 

  • 导入插件包

Unity项目添加广告,内购测试并上线APP Store

  • 示例广告id(测试时务必用示例广告id)

Unity项目添加广告,内购测试并上线APP Store

注意导出的app包主文件已变,不要打开错了。

Unity项目添加广告,内购测试并上线APP Store

 

  • 添加一个示例激励广告

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    ...

    public void Start()
    {
        #if UNITY_ANDROID//示例广告的appID
            string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
            string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
            string appId = "unexpected_platform";
        #endif

        // Initialize the Google Mobile Ads SDK.初始化
        MobileAds.Initialize(appId);

        // Get singleton reward based video ad reference.激励广告是单例模式,把脚本关联上单例
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        this.RequestRewardBasedVideo();//尽早加载广告资源
    }

    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID//示例广告ID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1712485313";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        // Create an empty ad request.创建一个新广告请求
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.加载
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }
}

 

hello world示例里创建的广告需求

Unity项目添加广告,内购测试并上线APP Store

 

  • 广告事件

Unity项目添加广告,内购测试并上线APP Store

 

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    private RewardBasedVideoAd rewardBasedVideo;
    ...

    public void Start()
    {
        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        // Called when an ad is shown.
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        // Called when the ad starts to play.
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        // Called when the ad click caused the user to leave the application.
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;

        this.RequestRewardBasedVideo();
    }

    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/1712485313";
        #else
            string adUnitId = "unexpected_platform";
        #endif

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print(
            "HandleRewardBasedVideoFailedToLoad event received with message: "
                             + args.Message);
    }

    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
    }

    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        MonoBehaviour.print(
            "HandleRewardBasedVideoRewarded event received for "
                        + amount.ToString() + " " + type);
    }

    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
    }
}

Unity项目添加广告,内购测试并上线APP Store

 

  • 展示广告&重新加载一段新的广告

Unity项目添加广告,内购测试并上线APP Store

 

2.使用自己创建的真实广告ID并真机测试

参考来源-admob测试广告

 

上面说的是用示例广告测试,下面我们说真实广告真机测试。

 

Unity项目添加广告,内购测试并上线APP Store

Unity项目添加广告,内购测试并上线APP Store

Unity项目添加广告,内购测试并上线APP Store

 

 

 

 

Ⅱ.添加内购并真机测试

 

 

 

Ⅲ.上线App Store

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关文章: