简体   繁体   中英

how to show ads and then restart scene after clicking restart button in unity?

I am new to unity and I want one help,the below script is my Ad manager script, I have one restart button and on click event, I want to show an ad and then start a scene, but when I click the restart button it shows an ad for few mili sec and the scene starts..how to show to fully and then load scene. Thank you.

`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;
using UnityEngine.SceneManagement;

public class ADSmanager : MonoBehaviour
{
    private InterstitialAd interstitial_Ad;
    private RewardedAd rewardedAd;

    private string interstitial_Ad_ID;
    private string rewardedAd_ID;

    void Start () {
        interstitial_Ad_ID = "ca-app-pub-3940256099942544/1033173712";
        rewardedAd_ID = "ca-app-pub-3940256099942544/5224354917";

        MobileAds.Initialize (initStatus => { });

        RequestInterstitial ();
        RequestRewardedVideo ();

    }

    private void RequestInterstitial () {
        interstitial_Ad = new InterstitialAd (interstitial_Ad_ID);
        interstitial_Ad.OnAdLoaded += HandleOnAdLoaded;
        AdRequest request = new AdRequest.Builder ().Build ();
        interstitial_Ad.LoadAd (request);
    }

    private void RequestRewardedVideo () {
        rewardedAd = new RewardedAd (rewardedAd_ID);
        rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
        rewardedAd.OnAdClosed += HandleRewardedAdClosed;
        rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
        AdRequest request = new AdRequest.Builder ().Build ();
        rewardedAd.LoadAd (request);
    }

    public void ShowInterstitial () {
        if (interstitial_Ad.IsLoaded ()) {
            interstitial_Ad.Show ();
            RequestInterstitial ();
        }
        // else{
        //      Savers.Score=0;
        // Savers.died=false;
        // SceneManager.LoadScene(1);
        // Time.timeScale=1;
        // }
       
        //Time.timeScale=1;
       
    }

    public void ShowRewardedVideo () {
        if (rewardedAd.IsLoaded ()) {
            rewardedAd.Show ();
            
        }

        
    }

    public void HandleOnAdClosed(object sender, EventArgs args)
{
 
        
    
}

    public void HandleOnAdLoaded (object sender, EventArgs args) {

       

    }

    public void HandleRewardedAdFailedToShow (object sender, AdErrorEventArgs args) {
        RequestRewardedVideo ();
    }

    public void HandleRewardedAdClosed (object sender, EventArgs args) {
        RequestRewardedVideo ();
    }

    public void HandleUserEarnedReward (object sender, Reward args) {
        RequestRewardedVideo ();
    }
}

`

Load the scene inside this method. And in Editor, the close button will handle this method. As in the editor, no actual ads will be shown.

public void HandleUserEarnedReward (object sender, Reward args) {
    RequestRewardedVideo ();
    sceneManager.LoadScene(sceneIndex);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM