2016-09-24 148 views
1

我的遊戲中有一個事件在遊戲過程中多次觸發。我希望在事件發生的每個奇怪時間展示廣告。 但它顯示的廣告只有一次,然後它似乎加載新廣告無限Admob只加載廣告一次

using GoogleMobileAds.Api; 
using System; 
using UnityEngine; 

public class AdMobManager : MonoBehaviour 
{ 
#if UNITY_ANDROID 

    private static AdMobManager _instance; 
    private bool isLoading; 
    private bool show; 
    private int isShown = -1; 
#if UNITY_ANDROID 
    string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"; 
#elif UNITY_IPHONE 
     string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxx"; 
#else 
     string adUnitId = "unexpected_platform"; 
#endif 

    public int IsShown 
    { 
     get 
     { 
      if (isShown < 0) 
      { 
       isShown = PlayerPrefs.GetInt("shown", 0); 
      } 
      return isShown; 
     } 
     set { isShown = value; } 
    } 


    public static AdMobManager Instance 
    { 
     get { return _instance; } 
     set { _instance = value; } 
    } 

    InterstitialAd interstitial; 

    void Awake() 
    { 
     if (_instance == null) 
      _instance = this; 

     IsShown = PlayerPrefs.GetInt("shown", 0); 
     interstitial = new InterstitialAd(adUnitId); 
    } 

    void Start() 
    { 
     RequestAd(); 
    } 

    public void Show() 
    { 
     Debug.Log("SHOW IS STARTED"); 
     if (IsShown == 0) 
     { 
      if (interstitial.IsLoaded()) 
      { 
       IsShown = 1; 
       Debug.Log("SHOW ADS"); 
       interstitial.Show(); 
      } 
      else 
      { 
       show = true; 
       Debug.Log("REQUESTING A NEW AD"); 
       RequestAd(true); 
      } 
     } 
     else 
     { 
      IsShown = 0; 
     } 
    } 

    private void RequestAd(bool show = false) 
    { 
     if (isLoading) 
     { 
      Debug.Log("RETURN FROM REQUEST AD"); 
      return; 
     } 
     isLoading = true; 
     AdRequest request = new AdRequest.Builder().Build(); 
     // Load the banner with the request. 
     interstitial.LoadAd(request); 
     if (show) 
      interstitial.OnAdLoaded += ShowAd; 
     interstitial.OnAdFailedToLoad += FailedToLoad; 
     interstitial.OnAdClosed += AdClosed; 

    } 

    private void AdClosed(object sender, EventArgs e) 
    { 
     RequestAd(); 
    } 

    private void FailedToLoad(object sender, AdFailedToLoadEventArgs e) 
    { 
     isLoading = false; 
     show = false; 
     Debug.Log("FAILED TO LOAD: " + e.Message); 
    } 

    void ShowAd(object sender, System.EventArgs args) 
    { 
     Debug.Log("LOADED"); 
     if (show) 
     { 
      IsShown = 1; 
      Debug.Log("SHOW ADS"); 
      interstitial.Show(); 
      show = false; 
     } 
     isLoading = false; 
    } 

    void OnApplicationPause(bool pause) 
    { 
     if (pause) 
      PlayerPrefs.SetInt("shown", isShown); 
    } 
#endif 
} 

是我的代碼錯誤或它是AdMob的失敗?

+0

我覺得被示是搞亂我們的邏輯,嘗試註釋掉它無處不在,重新構建它。 –

+0

做到了,沒有任何變化 – user2686299

+0

分享您的日誌與錯誤 –

回答

1

確定我想通了這個錯誤,我要叫

interstitial.Destroy(); 

顯示出廣告後