मैं अपने एंड्रॉइड यूनिटी गेम में Admob को लागू कर रहा हूं। अभी के लिए मैं पुरस्कृत वीडियो विज्ञापन का परीक्षण कर रहा हूं।

मैंने एक ui टेक्स्ट जोड़ा और चाहता था कि पुरस्कृत वीडियो के चलने के बाद टेक्स्ट बदल जाए।

लेकिन वीडियो चलने के बाद और मैं वीडियो बंद कर देता हूं, टेक्स्ट नहीं बदल रहा है।

यहाँ मेरी स्क्रिप्ट है:

private RewardBasedVideoAd rewardBasedVideo;
public Text text;

// Start is called before the first frame update
void Start()
{
    RequestReward();
}

private void RequestReward()
{
    string adUnitId;
    adUnitId = "ca-app-pub-3940256099942544/5224354917";

    //rewardedAd = new RewardedAd(adUnitId);
    rewardBasedVideo = RewardBasedVideoAd.Instance;

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

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

 public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
 {
     rewardBasedVideo.Show();
 }

 public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
 {
     RequestReward();
 }

 public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
 {
     MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
 }

 public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
 {
     MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
 }

 public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
 {
     text.text = "Done!";
 }

 public void HandleRewardBasedVideoRewarded(object sender, Reward args)
 {
     text.text = "Done!";
 }

 public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
 {
     MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
 }

 private void OnEnable()
 {
     HandleRewardedAdEvents(true);
 }

 private void OnDisable()
 {
     HandleRewardedAdEvents(false);
 }
 void HandleRewardedAdEvents(bool subscribe)
{
    if (subscribe)
    {
        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        // Called when an ad is shown.
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        // Called when the ad starts to play.
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        // Called when the ad click caused the user to leave the application.
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
    }
    else
    {
        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded -= HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
        // Called when an ad is shown.
        rewardBasedVideo.OnAdOpening -= HandleRewardBasedVideoOpened;
        // Called when the ad starts to play.
        rewardBasedVideo.OnAdStarted -= HandleRewardBasedVideoStarted;
        // Called when the user should be rewarded for watching a video.
        rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
        // Called when the ad is closed.
        rewardBasedVideo.OnAdClosed -= HandleRewardBasedVideoClosed;
        // Called when the ad click caused the user to leave the application.
        rewardBasedVideo.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;
    }
}

संपादित करें: मैं प्रश्न में HandleRewardedAdEvents विधि जोड़ना भूल गया।

0
Michael 25 जुलाई 2019, 19:58

1 उत्तर

सबसे बढ़िया उत्तर

ठीक है अब आपने HandleRewardedAdEvents का कार्यान्वयन जोड़ा है, मैं अपना उत्तर बदल दूंगा और ध्यान दूंगा कि OnEnable को इससे पहले Start निष्पादित किया जाता है, इसलिए जब rewardBasedVideo शायद अभी तक मौजूद नहीं है।

तो मैं इसके बजाय कॉलबैक जोड़ूंगा

private void RequestReward()
{
    string adUnitId;
    adUnitId = "ca-app-pub-3940256099942544/5224354917";

    //rewardedAd = new RewardedAd(adUnitId);
    rewardBasedVideo = RewardBasedVideoAd.Instance;

    AdRequest request = new AdRequest.Builder().Build();

    // ADD THE CALLBACKS

    HandleRewardedAdEvents(true);

    // Load the rewarded video ad with the request.
    this.rewardBasedVideo.LoadAd(request, adUnitId);
}

आम तौर पर हाँ, आपको किसी ऐसे कॉलबैक को हटा देना चाहिए जिसकी अब आवश्यकता नहीं है। लेकिन इस मामले में मैं इसे करना पसंद करूंगा

private void OnDestroy()
{
    HandleRewardedAdEvents(false);
}

हर बार OnEnable और OnDisable में नहीं।

और उपयोग करें

private void HandleRewardedAdEvents(bool subscribe)
{
    // NOTE that it is always secure to first remove callbacks
    // even if not added yet
    // this makes sure they are definitely only added once

    // Called when an ad request has successfully loaded.
    rewardBasedVideo.OnAdLoaded -= HandleRewardBasedVideoLoaded;
    // Called when an ad request failed to load.
    rewardBasedVideo.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
    // Called when an ad is shown.
    rewardBasedVideo.OnAdOpening -= HandleRewardBasedVideoOpened;
    // Called when the ad starts to play.
    rewardBasedVideo.OnAdStarted -= HandleRewardBasedVideoStarted;
    // Called when the user should be rewarded for watching a video.
    rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
    // Called when the ad is closed.
    rewardBasedVideo.OnAdClosed -= HandleRewardBasedVideoClosed;
    // Called when the ad click caused the user to leave the application.
    rewardBasedVideo.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;

    if (!subscribe) return;

    // Called when an ad request has successfully loaded.
    rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
    // Called when an ad request failed to load.
    rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
    // Called when an ad is shown.
    rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
    // Called when the ad starts to play.
    rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
    // Called when the user should be rewarded for watching a video.
    rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    // Called when the ad is closed.
    rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
    // Called when the ad click caused the user to leave the application.
    rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
}
2
derHugo 25 जुलाई 2019, 21:16