c# - 显示 Unity3d 广告后跟 Application.LoadLevel (Application.loadedLevel);

标签 c# android unity-game-engine

当用户选择重播我的 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/

相关文章:

c# - 如何将 GUID 值传递到 SqlCommand 对象 SQL INSERT 语句中?

c# - Model3DCollection 和 Model3DGroup 有什么区别?

c# - 在 PayPal 中使用 Classic Api 直接进行信用卡支付

javascript - Android 混合应用程序给我 "Uncaught SyntaxError: Unexpected identifier",源 : (1)

Android:交换两个重叠的 View

android - 记住 ListView 中选中的复选框

ios - 构建服务器 Unity3d + iOS 到 ipa 文件

C# DataGridView (CheckBox) 单元格点击多次回调

c# - Unity从资源文件夹外部加载文件

unity-game-engine - 如何在unity Netcode中为GameObject使用ClientRpc(或ServerRpc)?