如何接入Unity Ads?

  • 前言

Unity官方提供的广告插件Unity Ads总体来说还是很方便的,目前只支持Android和iOS的广告,而且官方已经处理好了unity和Android或者iOS的调用,所以根本不需要再为平台编写中间件进行交互。  

一、环境准备

目前有两种方法可以将Ads SDK集成到Unity项目中,使用集成SDK服务窗口或从资源商店添加软件包 。 

二、接入流程

1、File-> Build Settings更改成Android/iOS。

2、Window -> Services点击Ads, 然后在项目中启用SDK。

https://unity3d.com/profiles/unity3d/themes/unity/images/services/unityads-guide/services.png

3、添加代码

(1)、初始化

添加UnityEngine.Advertisement命名空间。在游戏的运行时生命周期的早期初始化SDK,最好是在启动时Initialize。

using UnityEngine.Advertisement;

public class UnityAdsScript : MonoBehaviour { 

    string gameId = "1234567";
    bool testMode = true;

    void Start () {
        Advertisement.Initialize (gameId, testMode);
    }
}

 (2)、展示普通广告(广告可跳过)

启用服务并创建放置后,只需调用Show方法即可显示广告。

Advertisement.Show ();

(3)、展示奖励广告(广告不可跳过)

奖励观看广告的玩家可提高用户参与度,从而带来更高的收入。例如,游戏可以奖励玩家游戏内货币,消耗品,额外生命或经验。要奖励玩家完成视频广告,请使用HandleShowResult以下示例中的回调方法。请务必检查result是否等于ShowResult.Finished,以验证用户是否未跳过广告。

    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;
        }
    }

在需要触发广告的地方调用ShowRewardedAd();即可。

 

  • 参考资料:

快速入门指南:https://unity3d.com/cn/services/ads/quick-start-guide

集成指南:https://unityads.unity3d.com/help/unity/integration-guide-unity

Unity Ads论坛:https://forum.unity.com/forums/unity-ads.67/

猜你喜欢

转载自blog.csdn.net/hello_1995/article/details/87932553