Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 悬赏广告回收没有被触发_Unity3d_Admob - Fatal编程技术网

Unity3d 悬赏广告回收没有被触发

Unity3d 悬赏广告回收没有被触发,unity3d,admob,Unity3d,Admob,我有一个游戏,当玩家失去了他们的生命,我希望他们能够看到第二次机会的视频 我使用的是unity版本2018.1.1f1 person,我已经下载了admob unity插件版本3.13.1 因此,如果玩家同意观看广告,广告将播放,然后继续游戏,而不会触发奖励玩家的回调。这是我的代码: using System; using UnityEngine; using GoogleMobileAds.Api; using UnityEngine.UI; public class RewardAd :

我有一个游戏,当玩家失去了他们的生命,我希望他们能够看到第二次机会的视频

我使用的是unity版本2018.1.1f1 person,我已经下载了admob unity插件版本3.13.1

因此,如果玩家同意观看广告,广告将播放,然后继续游戏,而不会触发奖励玩家的回调。这是我的代码:

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

public class RewardAd : MonoBehaviour {
    private BannerView bannerView;
    private InterstitialAd interstitial;
    private RewardBasedVideoAd rewardBasedVideo;
    private float deltaTime = 0.0f;
    private static string outputMessage = string.Empty;

    public AudioSource musicPlayer;
    public Player player;

    public Text UIText;

    public static string OutputMessage
    {
        set { outputMessage = value; }
    }

    public void Start()
    {

        #if UNITY_ANDROID
        string appId = "ca-app-pub-3940256099942544~3347511713";
        #elif UNITY_IPHONE
        string appId = "ca-app-pub-3940256099942544~1458002511";
        #else
        string appId = "unexpected_platform";
        #endif

        MobileAds.SetiOSAppPauseOnBackground(true);

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

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

        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
        this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
        this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
        this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
        this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
        this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
        this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
    }

    public void Update()
    {
        // Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
        // value.
        this.deltaTime += (Time.deltaTime - this.deltaTime) * 0.1f;
    }

    // Returns an ad request with custom ad targeting.
    private AdRequest CreateAdRequest()
    {
        return new AdRequest.Builder()
            .AddTestDevice(AdRequest.TestDeviceSimulator)
            .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
            .AddKeyword("game")
            .SetGender(Gender.Male)
            .SetBirthday(new DateTime(1985, 1, 1))
            .TagForChildDirectedTreatment(false)
            .AddExtra("color_bg", "9B30FF")
            .Build();
    }

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

        this.rewardBasedVideo.LoadAd(this.CreateAdRequest(), adUnitId);
    }

    private void ShowInterstitial()
    {
        if (this.interstitial.IsLoaded())
        {
            this.interstitial.Show();
        }
        else
        {
            MonoBehaviour.print("Interstitial is not ready yet");
        }
    }

    private void ShowRewardBasedVideo()
    {
        if (this.rewardBasedVideo.IsLoaded())
        {
            this.rewardBasedVideo.Show();
        }
        else
        {
            MonoBehaviour.print("Reward based video ad is not ready yet");
        }
    }

    #region RewardBasedVideo callback handlers

    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        this.rewardBasedVideo.Show();
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        musicPlayer.Pause();
        player.disableMovment = true;
    }

    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        musicPlayer.Pause();
        player.disableMovment = true;
    }

    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        player.rewardAdUI.SetActive(false);
        UIText.text = "Watch a short video\nfor an extra life ?";
        player.disableMovment = false;

        if (PlayerPrefs.GetInt("music") == 1)
        {
            musicPlayer.Play();
        }

        Lives.addLives(1);
        player.RespawnPlayer();
    }

    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        UIText.text = "Watch a short video\nfor an extra life ?";
    }

    public void secondChance()
    {
        UIText.text = "Loading video...\nplease wait.";
        this.RequestRewardBasedVideo();
    }

    public void GameOver()
    {
        player.disableMovment = false;

        if (PlayerPrefs.GetInt("music") == 1)
        {
            musicPlayer.Play();
        }

        player.gameOver();
    }

    #endregion
}
因此,第二个到最后一个名为secondChance()的函数是用来播放广告的。在我的android设备上,这会播放测试视频


广告播放后,应该调用HandlerewardbasedVideoPaired函数,但它没有调用。我在Android Studio中调试后发现一个无声错误,这是由于Unity中的线程问题造成的

解决方案:在主线程中调用处理事件(在更新方法中)


你说,广告在播放,但回调没有被调用?我这样问是因为在您的脚本中,secondChance()只调用RequestRewardBasedVideo(),而ShowRewardBasedVideo()在您的代码中从未被调用。是的,没错,ShowRewardBasedVideo在HandlerRewardBasedVideo加载的回调中被调用。这将在加载后显示视频。看来至少有一个回调正在运行,我可以建议从代码中删除您的appId吗?不确定Google的AdMob,但大多数广告系统强烈建议不要共享你的ID。@Eddge hi,这只是他们提供的测试ID,我只是想确保你没有发布可能影响你的应用程序的内容。
bool isAdClosed = false;
bool isRewarded = false;
void Update()
{
    if (isAdClosed)
    {
        if (isRewarded)
        {
            // do all the actions
            // reward the player
            isRewarded = false;
        }
        else
        {
            // Ad closed but user skipped ads, so no reward 
           // Ad your action here 
        }
        isAdClosed = false;  // to make sure this action will happen only once.
    }
}

public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
    print("HandleRewardBasedVideoClosed event received");

    RequestRewardBasedVideo(); // to load Next Videoad
    isAdClosed = true;

}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
    string type = args.Type;
    double amount = args.Amount;
    print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
            type);
        isRewarded = true;
 }