C# 显示结果统一广告奖励
当您暂停游戏时,会有一个按钮,您可以单击该按钮并在视频结束时查看不可跳过的视频。我希望实现以下目标: 如果视频观看到最后,他们会获得1颗额外的心(最多3颗完整的心),并获得一个聊天框或提醒对话框表示感谢 如果视频未打开、加载满或出现问题,他们将一无所获,并显示聊天框或警报对话框 当前已加载视频,但视频结束时,未收到奖品(1颗额外的心脏),我的代码中有什么错误 下面是按钮广告C# 显示结果统一广告奖励,c#,unity3d,unityscript,ads,unity3d-2dtools,C#,Unity3d,Unityscript,Ads,Unity3d 2dtools,当您暂停游戏时,会有一个按钮,您可以单击该按钮并在视频结束时查看不可跳过的视频。我希望实现以下目标: 如果视频观看到最后,他们会获得1颗额外的心(最多3颗完整的心),并获得一个聊天框或提醒对话框表示感谢 如果视频未打开、加载满或出现问题,他们将一无所获,并显示聊天框或警报对话框 当前已加载视频,但视频结束时,未收到奖品(1颗额外的心脏),我的代码中有什么错误 下面是按钮广告 using UnityEngine; using System.Collections; using UnityEngin
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 ();
}
}
使用UnityEngine;
使用系统集合;
使用UnityEngine。广告;
使用UnityEngine.UI;
公共类广告:单一行为{
公共按钮;
私心;
void-OnEnable()
{
getAds.onClick.AddListener(()=>getAds(getAds));
}
私有无效GetAds(按钮未按下)
{
如果(按钮按下==getAds){
广告。初始化(“XXXXXX”,真);
advision.IsReady(“rewardedVideo”);
广告秀(“rewardedVideo”);
}
}
公共无效句柄显示结果(显示结果结果)
{
开关(结果)
{
案例展示结果。已完成:
heart=GameObject.FindGameObjectWithTag(“Hearts”).GetComponent()作为Hearts;
heart.AddHeart();
打破
案例显示结果。已跳过:
Log(“广告在到达结尾之前被跳过了。”);
打破
案例显示结果。失败:
LogError(“未能显示广告”);
打破
}
}
无效可禁用()
{
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;
}
}
}
使用UnityEngine;
使用系统集合;
公共阶层的心:单一行为{
公共结构2d[]心脏;
私心;
私心;
无效开始(){
GetComponent().texture=initialHeart[0];
心脏=初始心脏。长度;
}
无效更新(){
}
公共图书馆
{
如果(心脏<0){
返回false;
}
如果(当前心脏<(心脏-1)){
电流+=1;
GetComponent().texture=InitialHearts[currentHearts];
返回true;
}否则{
返回false;
}
}
公共图书馆{
如果(currentHearts>0){
当前心脏-=1;
GetComponent().texture=InitialHearts[currentHearts];
返回true;
}否则{
返回false;
}
}
}
您的代码缺少最重要的部分,即
ShowOptions options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show(zoneId, options);
没有它,HandleShowResult
就不会被调用,你也不会知道广告显示后发生了什么。而且分数也不会增加。我实现了一个协同程序
,确保在显示广告之前一切正常。这没有经过测试,但任何问题都可以很容易地解决。错误以红色显示。绿色意味着成功
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();
}
}
公共类广告:单一行为
{
公共字符串配子ID;
公共字符串区域ID;
公共按钮;
私心;
void OnEnable()
{
getAds.onClick.AddListener(()=>getAds(getAds));
}
私有无效GetAds(按钮未按下)
{
如果(按钮按下==getAds)
{
//等待ad显示。超时时间为3秒
启动例程(显示带超时的ADS(3));
}
}
公共无效句柄显示结果(显示结果结果)
{
开关(结果)
{
案例展示结果。已完成:
heart=GameObject.FindGameObjectWithTag(“Hearts”).GetComponent()作为Hearts;
心;
Log(“广告在到达结尾之前被跳过了。”);
打破
案例显示结果。已跳过:
Log(“广告在到达结尾之前被跳过了。”);
打破
案例显示结果。失败:
LogError(“未能显示广告”);
打破
}
}
IEnumerator showAdsWithTimeOut(浮点超时)
{
//检查此平台是否支持ad
如果(!advision.isSupported)
{
Debug.LogError(“不支持Ad”);
yield break;//由于不支持ad,因此退出协程函数
}
Log(“支持Ad”);
//如果ad尚未初始化,则初始化ad
如果(!advision.i初始化)
{
//初始化广告
advision.Initialize(gameId,true);
}
浮点计数器=0;
bool-adisrady=false;
//等待超时秒,直到ad就绪
while(输入代码的计数器+1。您需要修改您的问题并重新表述您的问题。现在我读它时感到困惑。问题的结尾也是“?”“我的代码出了什么问题?”“为什么我的广告没有出现?”……运行,ultra mega blaster fine,101%……我只添加了gameid=“xxxxxx”和zondeId=“rewardedVideo”字符串…感谢您的帮助和耐心。@AlanVieiraRezende不客气。我还回答了您的其他问题。请查看。