【问题标题】:Unity ads not working on real deviceUnity 广告无法在真实设备上运行
【发布时间】:2018-11-21 22:18:22
【问题描述】:

在我当前的 Unity 程序中,我想实现广告。当我运行游戏时,广告在 Unity 编辑器中运行,但是当我尝试在我的 iPhone 7 或 iPad Air 上运行广告时,没有广告出现。有人知道我做错了什么吗?

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Advertisements;

public class GameManager : MonoBehaviour
{

void Start()
{
    Advertisement.Initialize("Appstore-id");
}

bool gameHasEnded = false;

public float restartDelay = 1f;
public float addDelay = 1f;

public GameObject completeLevelUI;

public void CompleteLevel ()
{
    completeLevelUI.SetActive(true);
    Invoke("ShowAdd", addDelay);
}

public void EndGame()
{
    if (gameHasEnded == false)
    {
        gameHasEnded = true;
        Debug.Log("Game over");
        Invoke("Restart", restartDelay);
    }

}

public void ShowAdd()
{

    if (Advertisement.IsReady())
    {
        Advertisement.Show ();
    }
}

void Restart()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

}

【问题讨论】:

标签: c# ios visual-studio unity3d ads


【解决方案1】:

我遇到了同样的问题,我修复它的方法是递归函数,直到广告准备好。比如:

IEnumerator ShowAd()
{
    yield return new waitForSeconds(1);
    if (Advertisement.IsReady())
    {
        Advertisement.Show ();
    }
    else { StartCoroutine(ShowAd()); }
}

这样,它将调用该方法,直到广告准备好展示为止。 此外,请确保您在 Unity 广告设置中启用了测试(或调试)模式。

【讨论】:

  • 感谢您的快速回答,我将尝试一下,看看它是否也适用于我。
  • 我仍然收到错误,即使我将其更改为 ShowAdd
  • @GriffinTeurlings 而不是调用 invoke 你是在调用 StartCoroutine 吗?
  • [与问题无关]:@Mykod 在 else 块中 yield return ShowAd(); 还不够,因为那时我们已经在协程中了吗?
  • 我刚刚在安装我的项目版本后测试了广告是否在我的手机上运行,​​但它们仍然无法运行。我还注意到的另一件事是,我的暂停菜单中的 UIButton 在我的手机上也不起作用,但它们在 Unity 编辑器中确实起作用。
【解决方案2】:

除了递归启动 IEnumerator 之外,只需添加另一个选项:

IEnumerator ShowAd() 
{
    while(!Advertisement.isReady()) 
    {
        yield return new waitForSeconds(1);
    }
    Advertisement.Show();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多