关闭后,Admob奖励的视频未调用事件

时间:2018-08-15 19:53:49

标签: c# unity3d admob admob-rewardedvideoad

我正在尝试将Admob奖励视频广告添加到我在Unity中制作的Android游戏中。显示屏可以正常显示,但是当我关闭广告时,永远不会得到奖励(硬币)。 这是我的C#脚本

AddCoinScript:

public class AddCoinScript : MonoBehaviour {  

    public Text scoreTextMesh, txtCoin;  
    public int coin;  
    int numberOfCoins;  

    void OnEnable ()  
    {  
        scoreTextMesh.text = "" + 0;  
        numberOfCoins = PlayerPrefs.GetInt("Coin", coin);       // Load archive coins  
        coin = coin + numberOfCoins;  
        txtCoin.text = coin.ToString();  
    }  

    public void AddsCoin()  
    {  
        coin += 30;  
        txtCoin.text = "" + coin;  
        PlayerPrefs.SetInt("Coin", coin);   // Archive the Score coins in "PlayerPrefs.SetInt"  
    }  
}  

AdmobRewardedVideo:

public class AdmobRewardedVideo : MonoBehaviour {  

private RewardBasedVideoAd rewardBasedVideo;  
public AddCoinScript Controller;  


    public void Start()
    {
        DontDestroyOnLoad(gameObject);

        #if UNITY_ANDROID
            string appId = "ca-app-pub-6144446801473222~4916984641";
        #elif UNITY_IPHONE
            string adUnitId = "";
        #else
            string adUnitId = "";
        #endif

        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);

        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;

        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;

        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;

        this.RequestRewardBasedVideo();
    }



    private void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID
        //Test;
            string adUnitId = "ca-app-pub-3940256099942544/5224354917";
        #elif UNITY_IPHONE
            string adUnitId = "";
        #else
            string adUnitId = "";
        #endif

        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the rewarded video ad with the request.
        this.rewardBasedVideo.LoadAd(request, adUnitId);
    }

    public void ShowRewardBasedAd()
    {
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
        }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        Controller.AddsCoin();
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        this.RequestRewardBasedVideo();
    }  
}   

0 个答案:

没有答案