admin管理员组

文章数量:1641940

                                       unity3d 2017  Ads 谷歌广告的加入 

今天想试下 Unity3D  自带的  谷歌 广告, 上网收了下, 发现都是 2016年之前的版本。试了下 直接报错; 去官网查了下 发现SDK  有了新的变化:

在这这之前你先要去, Unity3d 官网 申请 gameid  区分 Android 和 IOS

https://id.unity/en/conversations/a57055e9-41ec-44df-ba41-d6272215655200df

然后导入 一个  Ads 资源包

 

这是官网 ads 的接入地址

Rewarded video example:

using UnityEngine;
using UnityEngine.Advertisements;

public class RewardedAdsScript : MonoBehaviour, IUnityAdsListener { 

    string gameId = "1234567";
    myPlacementId = “rewardedVideo”;
    bool testMode = true;

    // Initialize the Ads listener and service:
    void Start () {
        Advertisement.AddListener (this);
        Advertisement.Initialize (gameId, testMode);
    }

    // Implement IUnityAdsListener interface methods:
    public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
        // Define conditional logic for each ad completion status:
        if (showResult == ShowResult.Finished) {
            // Reward the user for watching the ad to completion.
        } else if (showResult == ShowResult.Skipped) {
            // Do not reward the user for skipping the ad.
        } else if (showResult == ShowResult.Failed) {
            Debug.LogWarning (“The ad did not finish due to an error.);
        }
    }

    public void OnUnityAdsReady (string placementId) {
        // If the ready Placement is rewarded, show the ad:
        if (placementId == myPlacementId) {
            Advertisement.Show (myPlacementId);
        }
    }

    public void OnUnityAdsDidError (string message) {
        // Log the error.
    }

    public void OnUnityAdsDidStart (string placementId) {
        // Optional actions to take when the end-users triggers an ad.
    } 
}

 

下面是我的效果:

 

下面是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Advertisements;

public class MMoney : MonoBehaviour, IUnityAdsListener {

    public Button buttonPlay;
    public Button buttonLog;
    public Text   textState;

    private string  placementId = "rewardedVideo";
    private string  gameid      = "3374602";
    private bool    testMode    = true;


    // Use this for initialization
    void Start () {
     
        buttonPlay.onClick.AddListener(()=> {

            ShowRewardedVideo();

        });



        buttonLog.onClick.AddListener(() => {

            textState.text = "";

        });


        Advertisement.Initialize(gameid, testMode);
        Advertisement.AddListener(this);

    }

    public void ShowRewardedVideo() {

        if (Advertisement.isInitialized && Advertisement.IsReady(placementId)) {

            Advertisement.Show(placementId);
        }

    }



    //接口回调   开始
    public void OnUnityAdsDidError(string message)
    {
        Debug.Log("OnUnityAdsDidError:" + message);
        textState.text += "OnUnityAdsDidError:" + message + '\n';
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (showResult == ShowResult.Finished)
        {
            Debug.Log("玩家已经观看过 广告, 可在此 奖励玩家");
            textState.text += "玩家已经观看过 广告, 可在此 奖励玩家" + '\n';
        }
        else if (showResult == ShowResult.Skipped)
        {
            Debug.Log("玩家跳过了 广告");
            textState.text += "玩家跳过了 广告" + '\n';
        }
        else if (showResult == ShowResult.Failed) {
            Debug.Log("广告展示失败");
            textState.text += "广告展示失败" + '\n';
        }
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        Debug.Log("OnUnityAdsDidStart:"+ placementId);
        textState.text += "OnUnityAdsDidStart:" + placementId + '\n';
    }

    public void OnUnityAdsReady(string placementId)
    {
        Debug.Log("OnUnityAdsReady:" + placementId);
        textState.text += "OnUnityAdsReady: " + placementId + '\n';
    }
    //接口回调   结束

}

 

工程下载

本文标签: 广告Unity3dADS