【问题标题】:Unity - Advertisment called multiple timesUnity - 多次调用广告
【发布时间】:2021-05-01 11:04:57
【问题描述】:

我在使用 Unity 的广告时遇到了这个问题。具体看视频后点击X按钮关闭视频,我应该把奖品给玩家(进入下一关)。问题是 OnUnityAdsDidFinish 函数多次调用 if (showResult == ShowResult.Finished) 。我究竟做错了什么?如何调用 FindObjectOfType () .LoadNextLevel () 函数一次; ?提前谢谢你

public class UnityAdsInterstitial : MonoBehaviour, IUnityAdsListener
{
private string gameID = "******";
//nome scelto nella DashBoard di Unity
private string interstitialID = "interstitial";

private string myPlacementId = "rewardedVideo";

public int randomHighValue = 30;

private bool TestMode = true;

private bool adsClosed = false;

public Button _button;

private void Awake()
{
    _button = GetComponent<Button>();
}

void Start()
{
    Debug.Log("Ads  start");
    _button = GameObject.Find("StartAds").GetComponent<Button>();

    _button.interactable = Advertisement.IsReady(myPlacementId);

    if (_button) _button.onClick.AddListener(ShowRewardedVideo);

    Advertisement.Initialize(gameID, TestMode);
    Advertisement.AddListener(this);
    if (adsClosed)
    {
        adsClosed = false;
    }
    
}

public void ShowInterstitial()
{
    if (Advertisement.IsReady(interstitialID) )
    {
        Advertisement.Show(interstitialID);
    }
}

public void ShowRewardedVideo()
{
    if (Advertisement.IsReady(myPlacementId))
    {
        Debug.Log("Rewarded video is Ready");
        Advertisement.Show(myPlacementId);
    }
    else
    {
        Debug.Log("Rewarded video is not ready at the moment! Please try again later!");
    }
}

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

public void OnUnityAdsReady(string placementdID)
{
    if (placementdID == interstitialID)
    {
        Debug.Log("InterstitialIsReady");
    }

    if (placementdID == myPlacementId)
    {
        Debug.Log("RewardedIsReady");
        _button.interactable = true;
    }

}

public void OnUnityAdsDidFinish(string placementdID, ShowResult showResult)
{
    if (showResult == ShowResult.Finished)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }
    else if (showResult == ShowResult.Skipped)
    {
        // Do not reward the user for skipping the ad.
    }
    else if (showResult == ShowResult.Failed)
    {
        Debug.LogWarning("The ad did not finish due to an error.");
    }
}

public void OnUnityAdsDidError(string message)
{

    Debug.Log("OnUnityAdsDidError");
}
public void OnUnityAdsDidStart(string message)
{

    Debug.Log("OnUnityAdsDidStart");
}

【问题讨论】:

  • 这不是关于 Visual Studio 应用程序的问题,所以我已经为您删除了该标签。

标签: c# unity3d ads


【解决方案1】:

我会通过检查展示位置 ID 开始我的调查(以防有更多展示位置)

  1. 检查回调是否用于正确的展示位置 ID
    if (showResult == ShowResult.Finished && placementId == myPlacementId)
    {
        // Reward the user for watching the ad to completion.
        
        if (!adsClosed)
        {
            adsClosed = true;
            FindObjectOfType<LevelLoader>().LoadNextLevel();  
        }
        
    }

第二个是我只有一个UnityAdsInterstitial 的活动实例。您可以通过对象的引用在调试模式下检查这一点。如果多个实例从代码中的其他位置开始,那么您应该限制为一个。

【讨论】:

  • 可能有多个实例。你如何建议我从我上面发布的代码开始限制一个?在您看来,没有更多实例的最正确和最干净的方法是什么?单身人士?
  • 取决于行为的外部实现和初始化。我从不建议单例,因为我认为它主要是一种反模式。我可能会将实例化委托给另一个工厂或依赖注入容器,并从那里处理对象的生命周期。 (就像一个简单的工厂,在完成工作之前总是提供相同的实例)。
猜你喜欢
  • 2020-04-23
  • 2017-01-02
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-01
相关资源
最近更新 更多