当您暂停游戏时,会有一个按钮,您可以在此按钮上单击并查看此视频末尾的不可跳过的视频。我想实现以下内容:
如果观看视频到最后,他们会获得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;
}
}
}
答案 0 :(得分: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();
}
}