2015-07-03 66 views
1

當用戶選擇在Unity3D遊戲中重播當前場景時,我試圖在Android應用程序上顯示插頁式廣告。我的第一次嘗試在編輯器中產生了閃爍的測試廣告,但在實際設備上未能產生任何東西。顯示Unity3d廣告後跟Application.LoadLevel(Application.loadedLevel);

這是我使用的代碼(僅包括相關的代碼):

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); 
    } 

我想這可能是與Application.LoadLevel (Application.loadedLevel);ShowAd();,所以我創建了一個新的測試按鈕,嘗試找出問題:

public void ShowAd() 
{ 
    // If ready, show an advertisement 
    if (Advertisement.IsReady()) 
    { 
     Advertisement.Show(); 
    } 
} 

public void TestAdButton() 
{ 
    ShowAd(); 
} 

這會正確顯示廣告;不過,我希望在點擊重播按鈕之後實施廣告(這會觸發重播當前場景)。我被統一員工進一步混淆,聲稱show an add will automatically pause the game

我在這裏做錯了什麼?

回答

1

我看到的錯誤是您調用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); 
} 
+0

這篇作品出來的嗎? –