Alan Vieira Rezende Alan Vieira Rezende - 3 months ago 16
C# Question

Show Result Unity ADS rewards

When you pause the game, there is a button where you can click and view a non-skippable video at the end of this video. I want to implement the following:

If the video is watched to the end they gain 1 extra heart (max 3 full hearts) and get a chat box or alert dialog thanking.

If the video does not open, fully load or something goes wrong, they get nothing and a chat box or alert dialog appears.

Currently the video loads, But when the video ends, the prize is not received (1 extra heart), what is wrong in my code?

Below are the button ads

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


Below current hearts system script

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;

}
}
}

Answer

Your code is missing the most important part which is

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

Without it, HandleShowResult wont be called and you won't know what happened after ad has been displayed. Also score wont increment either. I went ahead to implement a coroutine which makes sure that everything is fine before displaying ads. This is was not tested but any problem can easily be fixed. Errors are displayed with red color. Green means success.

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();
    }
}
Comments