显示结果 Unity ADS 奖励
Show Result Unity ADS rewards
当您暂停游戏时,有一个按钮,您可以单击该按钮并在该视频末尾观看不可跳过的视频。我想实现以下内容:
如果视频看完,他们会额外获得 1 颗心(最多 3 颗满心),并获得一个聊天框或警告对话框表示感谢。
如果视频无法打开、无法完全加载或出现问题,他们将一无所获,并会出现一个聊天框或警告对话框。
当前正在加载视频,但是当视频结束时,没有收到奖品(额外的一颗心),我的代码有什么问题吗?
下面是按钮广告
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;
}
}
}
您的代码缺少最重要的部分
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();
}
}
当您暂停游戏时,有一个按钮,您可以单击该按钮并在该视频末尾观看不可跳过的视频。我想实现以下内容:
如果视频看完,他们会额外获得 1 颗心(最多 3 颗满心),并获得一个聊天框或警告对话框表示感谢。
如果视频无法打开、无法完全加载或出现问题,他们将一无所获,并会出现一个聊天框或警告对话框。
当前正在加载视频,但是当视频结束时,没有收到奖品(额外的一颗心),我的代码有什么问题吗?
下面是按钮广告
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;
}
}
}
您的代码缺少最重要的部分
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();
}
}