Ads are working In Editor but not In Android

Viewed 96

I am trying to use ads In my game . They are properly working in Unity but not in Android. Because of I am using unity 2018.4.25f1 personal so it's supporting older version of unity monetization asset. Maybe it's a problem but here is my code of Rewarded Video CSharp file

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;

[RequireComponent(typeof(Button))]
public class RewardedVideo : MonoBehaviour, IUnityAdsListener
{

#if UNITY_IOS
    private string gameId = "3853032";
#elif UNITY_ANDROID
    private string gameId = "3853033";
#endif

    [SerializeField]Button myButton;
    [SerializeField]GameObject errorMessage;
    public string myPlacementId = "rewardedVideo";

    void Start()
    {

        // Map the ShowRewardedVideo function to the button’s click listener:
        if (myButton)
            myButton.onClick.AddListener(ShowRewardedVideo);

        // Initialize the Ads listener and service:
        Advertisement.AddListener(this);
        Advertisement.Initialize(gameId, true);
    }
    public void okError()
    {
        errorMessage.SetActive(false);
    }
    // Implement a function for showing a rewarded video ad:
    public void ShowRewardedVideo()
    {
        if(Advertisement.IsReady() == true)
            Advertisement.Show(myPlacementId);
        else
            errorMessage.SetActive(true);
    }

    // Implement IUnityAdsListener interface methods:
    public void OnUnityAdsReady(string placementId)
    {
        // If the ready Placement is rewarded, activate the button: 
        if (placementId == myPlacementId)
        {
            // myButton.interactable = true;
        }
    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        // Define conditional logic for each ad completion status:
        if (showResult == ShowResult.Finished)
        {
            int levels = PlayerPrefs.GetInt("unlockedLevel", 1);
            if(levels != 5)
            {
                PlayerPrefs.SetInt("unlockedLevel", levels+1);
            }
        }
        else if (showResult == ShowResult.Skipped)
        {
            // Do not reward the user for skipping the ad.
        }
        else if (showResult == ShowResult.Failed)
        {
            Debug.LogError("The ad did not finish due to an error");
        }
    }

    public void OnUnityAdsDidError(string message)
    {
        // Log the error.
    }

    public void OnUnityAdsDidStart(string placementId)
    {
        // Optional actions to take when the end-users triggers an ad.
    }
}

Downloaded asset from Assets store not from package manager. I saw Unity help, unity forum, stackoverflow but nothing solved my problem Have you any suggestion?

0 Answers
Related