当用户选择重播我的 Unity3d 游戏中的当前场景时,我试图在 Android 应用程序上显示插页式广告。我这样做的第一次尝试在编辑器中产生了测试广告的闪烁,但未能在实际设备上产生任何东西。
这是我使用的代码(仅包含相关代码):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Advertisements;
public class GameController : MonoBehaviour
{
...
void Awake ()
{
Advertisement.Initialize("Game ID omitted");
}
...
public void ShowAd ()
{
// If ready, show an advertisement
if (Advertisement.IsReady ())
{
Advertisement.Show ();
}
}
public void Replay ()
{
ShowAd ();
Application.LoadLevel (Application.loadedLevel);
}
我认为这可能与 ShowAd();
之后的 Application.LoadLevel(Application.loadedLevel);
有关,所以我创建了一个新的测试按钮来尝试隔离问题:
public void ShowAd ()
{
// If ready, show an advertisement
if (Advertisement.IsReady ())
{
Advertisement.Show ();
}
}
public void TestAdButton ()
{
ShowAd ();
}
这会正确显示广告;但是,我想在点击重播按钮(然后应该触发当前场景的重播)后实现广告。 Unity 员工说 show an add will automatically pause the game 令我更加困惑.
我在这里做错了什么?
最佳答案
我看到的错误是您在调用 ShowAd 之后调用了 LoadLevel,因此正在显示添加,但随后立即被 LoadLevel 覆盖。尝试存储 PlayerPrefs 以确定关卡是否重新启动。
public void Replay ()
{
PlayerPrefs.SetInt("isReplayed", 1);
Application.LoadLevel (Application.loadedLevel);
}
然后在 GameController 上添加:
public void Start()
{
if(PlayerPrefs.GetInt("isReplayed") == 1)
{
ShowAd();
}
PlayerPrefs.SetInt("isReplayed", 0);
}
关于c# - 显示 Unity3d 广告后跟 Application.LoadLevel (Application.loadedLevel);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31214263/