繁体   English   中英

我如何每 5 轮/输了一次统一广告?

[英]How do I make a unity ad play every 5 rounds/losses?

我一直在尝试制作我的游戏,每 5 轮/失败播放一次广告。 我曾尝试复制一些脚本,但它们对我不起作用。 我是一个非常早期的开发人员,对 c# 和统一性了解不多。 如果您找到解决方案,请告诉我我需要将代码放在哪个脚本中。 我的游戏管理器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject gameOverCanvas;
    public AdsManager ads;

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

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

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

}

我的 AdsManager 脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;

public class AdsManager : MonoBehaviour, IUnityAdsListener
{
#if UNITY_IOS
    string gameId = "#######";
#else
    string gameId = "#######";
#endif

    Action onRewardedAdSuccess;

    // Start is called before the first frame update
    void Start()
    {
        Advertisement.Initialize(gameId);
        Advertisement.AddListener(this);
        ShowBanner();
    }

    public void PlayAd()
    {
        if(Advertisement.IsReady("Interstitial_Android"))
            Advertisement.Show("Interstitial_Android");
    }

    public void PlayRewardedAd(Action onSuccess)
    {
        onRewardedAdSuccess = onSuccess;
        if(Advertisement.IsReady("Rewarded_Android"))
        {
        Advertisement.Show("Rewarded_Android");
        }
        else
        {
            Debug.Log("Rewarded ad is not ready!");
        }
    }

    public void ShowBanner()
    {
        if (Advertisement.IsReady("Banner_Android"))
        {
            Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
            Advertisement.Banner.Show("Banner_Android");
        }
        else
        {
            StartCoroutine(RepeatShowBanner());
        }
    }

    public void HideBanner()
    {
        Advertisement.Banner.Hide();
    }

    IEnumerator RepeatShowBanner()
    {
        yield return new WaitForSeconds(1);
        ShowBanner();
    }

    public void OnUnityAdsReady(string placementId)
    {
        Debug.Log("ADS ARE READY!");
    }

    public void OnUnityAdsDidError(string message)
    {
        Debug.Log("ERROR: " + message);
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        Debug.Log("VIDEO STARTED!");
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (placementId == "Rewarded_Android" && showResult == ShowResult.Finished)
        {
            onRewardedAdSuccess.Invoke();
        }
    }
}

如果您需要更多脚本,请告诉我。 我将不胜感激任何反馈。

创建一个计数器,在每场比赛后将其递增,然后检查您是否达到了所需的比赛次数。

您需要创建计数器和所需的游戏变量。 然后,将增量和 if 语句代码放入 GameOver 方法中:

public void GameOver()
{
    gameOverCanvas.SetActive(true);
    Time.timeScale = 0;
    
    gameCount++; //increment game count

    if(gameCount >= gamesToShowAd) // check if player played enough games to show ad
    {
        ads.PlayAd();
        gameCount = 0; // reset counter
    }
}

如果您愿意,您可以考虑使用https://docs.unity3d.com/ScriptReference/PlayerPrefs.html或使用现成的在线解决方案来保存游戏计数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM