首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使UnityAds在3例死亡后出现?

如何使UnityAds在3例死亡后出现?
EN

Stack Overflow用户
提问于 2022-01-13 11:04:57
回答 1查看 70关注 0票数 1

现在,游戏中每次玩家死后都会播放广告。我希望广告在玩家第三次死后出现。请指导我,因为我是一个初学者,没有真正的知识编码。还请指定是否需要将脚本附加到特定的游戏对象。

我已经把广告代码放在下面了。谢谢。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour

{
    public GameObject gameOverCanvas;

public GameObject Interstitial;

public int DeathCount = 0;

private void Start()
{
    Time.timeScale = 1;
   
}


public void GameOver()
{
    gameOverCanvas.SetActive(true);
    Time.timeScale = 0;
    
    DeathCount++;
    
}

private bool DeathCountCheck()
{


    if (DeathCount == 3)
    {
        // Reset the variable, otherwise it will increase indefinitely and the condition will not be met in the future
        Interstitial.GetComponent<InterstitialAds>().ShowInterstitial();
        DeathCount = 0;
        return true;

    }

    return false;

}

public void Replay()
{
    SceneManager.LoadScene(0);
}
EN

回答 1

Stack Overflow用户

发布于 2022-01-13 11:20:50

如果每次玩家死后,"deathCount“都会增加,那么当广告显示给你看的时候,只需检查这个变量并适当地重置它。

代码语言:javascript
复制
// Function that checks if there were 3 deaths
private bool DeathCountCheck()
        {

            
            if (deathCount == 3)
            {
                // Reset the variable, otherwise it will increase indefinitely and the condition will not be met in the future
                deathCount = 0;
                return true;
            }

            return false;

        }

然后在"ShowInterstitialAd“中添加另一个条件

代码语言:javascript
复制
    public void ShowInterstitial()
{
    if (Advertisement.IsReady(interstitialID) && DeathCountCheck())
    {
        Advertisement.Show(interstitialID);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70695506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档