【问题标题】:Show Result Unity ADS rewards显示结果 Unity ADS 奖励
【发布时间】:2016-04-28 07:44:22
【问题描述】:

当您暂停游戏时,您可以点击该按钮并在该视频的末尾观看不可跳过的视频。我想实现以下内容:

如果视频观看到最后,他们会额外获得 1 颗爱心(最多 3 颗完整的爱心),并获得一个聊天框或提醒对话框来感谢。

如果视频无法打开、完全加载或出现问题,他们将一无所获,并且会出现一个聊天框或警告对话框。

目前正在加载视频,但是当视频结束时,没有收到奖品(1个额外的心脏),我的代码有什么问题?

下面是按钮广告

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

public class Ads : MonoBehaviour {

    public Button getAds;
    private Hearts heart;

    void OnEnable ()
    {
        getAds.onClick.AddListener (() => GetAds (getAds));
    }


    private void GetAds ( Button buttonPressed)
    {
        if (buttonPressed == getAds) {

            Advertisement.Initialize ("XXXXXX", true);
            Advertisement.IsReady ("rewardedVideo");
            Advertisement.Show ("rewardedVideo");
            }   
    }

    public void HandleShowResult (ShowResult result)
    {
        switch (result)
        {
        case ShowResult.Finished:
            heart = GameObject.FindGameObjectWithTag ("Hearts").GetComponent<Hearts> () as Hearts;
            heart.AddHeart ();
            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;
        }
    }


    void OnDisable ()
    {
        getAds.onClick.RemoveAllListeners ();
    }
}

当前红心系统脚本下方

using UnityEngine;
using System.Collections;

public class Hearts : MonoBehaviour {

    public Texture2D[]initialHeart;
    private int hearts;
    private int currentHearts;

    void Start () {

        GetComponent<GUITexture>().texture = initialHeart[0];
        hearts = initialHeart.Length;

    }

    void Update () {

    }

    public bool TakeHeart()
    {
        if (hearts < 0) {

            return false;

        }

        if (currentHearts < (hearts - 1)) {

            currentHearts += 1;
            GetComponent<GUITexture> ().texture = initialHeart [currentHearts];
            return true;


        } else {

            return false;

        }   
    }

    public bool AddHeart() {

        if (currentHearts > 0) {
            currentHearts -= 1;
            GetComponent<GUITexture> ().texture = initialHeart [currentHearts];
            return true;
        } else {
            return false;

        }
    }
}

【问题讨论】:

  • +1 用于放置代码。您需要修改您的问题并重新表述您的问题。现在我读起来很困惑。问题也以“?”结尾“我的代码有什么问题?” “为什么我的广告没有显示?”......

标签: c# unity3d unityscript ads unity3d-2dtools


【解决方案1】:

您的代码缺少最重要的部分

ShowOptions options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show(zoneId, options);

没有它,HandleShowResult 将不会被调用,并且您将不知道广告展示后发生了什么。分数也不会增加。我继续实施coroutine,以确保在显示广告之前一切正常。这未经测试,但任何问题都可以轻松解决。错误以红色显示。绿色意味着成功。

public class Ads : MonoBehaviour
{

    public string gameId;
    public string zoneId;

    public Button getAds;

    private Hearts heart;

    void OnEnable()
    {
        getAds.onClick.AddListener(() => GetAds(getAds));
    }


    private void GetAds(Button buttonPressed)
    {
        if (buttonPressed == getAds)
        {
            //Wait for ad to show. The timeout time is 3 seconds
            StartCoroutine(showAdsWithTimeOut(3));
        }
    }


    public void HandleShowResult(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                heart = GameObject.FindGameObjectWithTag("Hearts").GetComponent<Hearts>() as Hearts;
                heart.AddHeart();
                Debug.Log("<color=green>The ad was skipped before reaching the end.</color>");
                break;

            case ShowResult.Skipped:
                Debug.Log("<color=yellow>The ad was skipped before reaching the end.</color>");
                break;

            case ShowResult.Failed:
                Debug.LogError("<color=red>The ad failed to be shown.</color>");
                break;
        }
    }

    IEnumerator showAdsWithTimeOut(float timeOut)
    {
        //Check if ad is supported on this platform 
        if (!Advertisement.isSupported)
        {
            Debug.LogError("<color=red>Ad is NOT supported</color>");
            yield break; //Exit coroutine function because ad is not supported
        }

        Debug.Log("<color=green>Ad is supported</color>");

        //Initialize ad if it has not been initialized
        if (!Advertisement.isInitialized)
        {
            //Initialize ad
            Advertisement.Initialize(gameId, true);
        }


        float counter = 0;
        bool adIsReady = false;

        // Wait for timeOut seconds until ad is ready
        while(counter<timeOut){
            counter += Time.deltaTime;
            if( Advertisement.IsReady (zoneId)){
                adIsReady = true;
                break; //Ad is //Ad is ready, Break while loop and continue program
            }
            yield return null;
        }

        //Check if ad is not ready after waiting
        if(!adIsReady){
            Debug.LogError("<color=red>Ad failed to be ready in " + timeOut + " seconds. Exited function</color>");
            yield break; //Exit coroutine function because ad is not ready
        }

        Debug.Log("<color=green>Ad is ready</color>");

        //Check if zoneID is empty or null
        if (string.IsNullOrEmpty(zoneId))
        {
            Debug.Log("<color=red>zoneId is null or empty. Exited function</color>");
            yield break; //Exit coroutine function because zoneId null
        }

        Debug.Log("<color=green>ZoneId is OK</color>");


        //Everything Looks fine. Finally show ad (Missing this part in your code)
        ShowOptions options = new ShowOptions();
        options.resultCallback = HandleShowResult;

        Advertisement.Show(zoneId, options);
    }

    void OnDisable()
    {
        getAds.onClick.RemoveAllListeners();
    }
}

【讨论】:

  • 正在运行,超超级冲击波罚款,101% ...我只添加了,gameid = "XXXXXXX" 和 zondeId = "rewardedVideo" 字符串...感谢 + 1 次的帮助和耐心。
  • @AlanVieiraRezende 欢迎您。我也回答了你的另一个问题。所以看看吧。stackoverflow.com/questions/36875918/…
猜你喜欢
  • 1970-01-01
  • 2022-10-24
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
  • 2020-03-21
相关资源
最近更新 更多