2017-04-07 169 views
1

我正在嘗試將Admob獎勵視頻廣告添加到在Unity中製作的Android遊戲中。顯示效果很好,但是當我關閉廣告時,獎勵從未被給出。我已經測試了函數中的代碼並且正常工作,所以我認爲問題在於不是調用gettting。誰能幫我?Unity:Admob獎勵視頻廣告不會調用事件

using UnityEngine; 
using UnityEngine.UI; 
using System.Collections; 
using System; 
using GoogleMobileAds; 
using GoogleMobileAds.Api; 

public class textEdit : MonoBehaviour 
{ 
    public Image lifeAdUI; 
    static Image lifeAdUIStat; 
    public Text adFailUI; 
    static Text adFailUIStat; 
    public Button lifeButton; 

    private static RewardBasedVideoAd videoAd; 
    static bool adTime = false; 
    static bool adPlaying = false; 
    static int pass = 0; 
    bool watched; 

    // Use this for initialisation 
    void Start() 
    { 
    Button btn = lifeButton.GetComponent<Button>(); 
    btn.onClick.AddListener(VideoAd); 

    videoAd = RewardBasedVideoAd.Instance; 

    videoAd.OnAdFailedToLoad += HandleOnAdFailedToLoad; 
    videoAd.OnAdOpening += HandleOnAdOpening; 
    videoAd.OnAdClosed += HandleOnAdClosed; 
    videoAd.OnAdRewarded += HandleOnAdReward; 
    videoAd.OnAdLeavingApplication += HandleOnAdLeavingApplication; 
    videoAd.OnAdLoaded += HandleOnAdLoaded; 
    videoAd.OnAdStarted += HandleOnAdStarted; 

    lifeAdUIStat = lifeAdUI; 
    adFailUIStat = adFailUI; 

    } 


public static void LoadVideoAd() 
{ 
#if UNITY_EDITOR 
    string adUnitID = "unused"; 
#elif UNITY_ANDROID 
    string adUnitID = "ca-app-pub-3025391748532285/9122766975"; 
#elif UNITY_IPHONE 
    string adUnitID = ""; 
#else 
    string adUnitID = "unexpected_platform"; 
#endif 


    videoAd.LoadAd(new AdRequest.Builder().Build(), adUnitID); 
    pass = pass + 1; 

} 

void VideoAd() 
{ 
    if (videoAd.IsLoaded()) 
    { 
     videoAd.Show(); 


    } 
    else 
    { 
     //ad not loaded 
    } 
} 

//Ad Events 
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) 
{ 
    if (pass < 2) 
    { 
     LoadVideoAd(); 
    } 
    else 
    { 
     StartCoroutine(adFailCoro()); 
    } 
} 

public void HandleOnAdOpening(object ssender, EventArgs args) 
{ 
    adPlaying = true; 
} 

public void HandleOnAdClosed(object sender, EventArgs args) 
{ 
    adPlaying = false; 
    watched = true; 

    if (watched == true) 
    { 
     control controlScript = GameObject.FindGameObjectWithTag("Control").GetComponent<control>(); 

     lifeAdUI.enabled = false; 
     StartCoroutine(controlScript.ExtraLife()); 
    } 
} 

public void HandleOnAdReward(object sender, EventArgs args) 
{ 
    watched = true; 
} 

public void HandleOnAdLeavingApplication(object sender, EventArgs args) 
{ 


} 

    public void HandleOnAdLoaded(object sender, EventArgs args) 
    { 

    } 

    public void HandleOnAdStarted(object sender, EventArgs args) 
    { 

    } 
} 

回答

0

如果你叫VideoAd展示自己的視頻廣告,但由於一些不可預見的原因,您的影片尚未加載,或有加載失敗,於是請求再次加載您的廣告。

void VideoAd() 
{ 
    if (videoAd.IsLoaded()) 
    { 
     videoAd.Show(); 
    } 
    else 
    { 
     LoadVideoAd(); 
    } 
} 

當您的廣告被用戶關閉時,請求加載新的視頻廣告。

public void HandleOnAdClosed(object sender, EventArgs args) 
{ 
    adPlaying = false; 
    watched = true; 

    if (watched == true) //what is the need of this condition, it always true 
    { 
     control controlScript = GameObject.FindGameObjectWithTag("Control").GetComponent<control>(); 

     lifeAdUI.enabled = false; 
     StartCoroutine(controlScript.ExtraLife()); 
     LoadVideoAd(); 
    } 
} 
+0

觀察變量爲null,開始時只在HandleOnAdRewar上設置爲true因此當用戶在觀看廣告後關閉廣告時,他們會獲得另一種生活。但問題是,這些HandleOnAd ....函數沒有被調用。 –

0

初始化AdMob聯播Unity插件

using admob; 
    Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-279xxxxxxxx/xxxxxxxx 
    //Admob.Instance().initAdmob("ca-app-pub-3940256099942544/2934735716", "ca-app-pub-3940256099942544/4411468910"); 

這裏是最少的代碼來創建AdMob的視頻。

Admob.Instance().loadRewardedVideo("ca-app-pub-3940256099942544/1712485313"); 

視頻需要在你的應用合適的停止點明確顯示,請檢查視頻隨時顯示之前:

if (Admob.Instance().isRewardedVideoReady()) { 
    Admob.Instance().showRewardedVideo(); 
} 

手柄獎勵

Admob.Instance().videoEventHandler += onVideoEvent; 
void onVideoEvent(string eventName, string msg) 
{ 
    Debug.Log("handler onAdmobEvent---" + eventName + " " + msg); 
    if (eventName == AdmobEvent.onRewarded) 
    { 
     //msg is the reward count.you can handle it now 
    } 
} 

裁判admob plugin

相關問題