In Rewarded Video Ads not work reward
Place a GameObject on Scene (UnityAdsManager)
Place a Button on Scene (TestButton with btnTestAdsManager.cs )
It used:
https://docs.unity.com/ads/UnityDeveloperIntegrations.htm
To initialize Unity Ads SDK
https://docs.unity.com/ads/InitializingTheUnitySDK.htm
To show reward video
https://docs.unity.com/ads/ImplementingRewardedAdsUnity.htm
AdsInitializer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener
{
[SerializeField] private string _androidGameId = "39XXXXX";
[SerializeField] private string _iOSGameId = "39XXXXX";
private string _gameId;
void Awake()
{
InitializeAds();
}
public void InitializeAds()
{
_gameId = (Application.platform == RuntimePlatform.IPhonePlayer)
? _iOSGameId
: _androidGameId;
Advertisement.Initialize(_gameId, false);
}
public void OnInitializationComplete()
{
Debug.Log("Unity Ads initialization complete.");
}
public void OnInitializationFailed(UnityAdsInitializationError error, string message)
{
Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
}
}
RewardedAds.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class RewardedAds : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
public string _adUnitId = null;
public string AndroidAdID = "39XXXXX";
public string iOSAdID = "39XXXXX";
// Get the Ad Unit ID for the current platform:
void Awake()
{
#if UNITY_IOS
_adUnitId = iOSAdID;
#endif
#if UNITY_ANDROID
_adUnitId = AndroidAdID;
#endif
}
// Load content to the Ad Unit:
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
Debug.Log("Loading Ad: " + _adUnitId);
Advertisement.Load(_adUnitId, this);
}
// If the ad successfully loads, add a listener to the button and enable it:
public void OnUnityAdsAdLoaded(string adUnitId)
{
Debug.Log("Ad Loaded: " + adUnitId);
if (adUnitId.Equals(_adUnitId))
{
}
}
// Implement a method to execute when the user clicks the button.
public void ShowAd()
{
// Then show the ad:
Advertisement.Show(_adUnitId, this);
}
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
{
Debug.Log("Unity Ads Rewarded Ad Completed");
// Grant a reward.
int icoin = PlayerPrefs.GetInt("coin");
icoin = icoin + 1;
PlayerPrefs.SetInt("coin", icoin);
// Load another ad:
Advertisement.Load(_adUnitId, this);
}
}
// Implement Load and Show Listener error callbacks:
public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
{
Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
{
Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
// Use the error details to determine whether to try to load another ad.
}
public void OnUnityAdsShowStart(string adUnitId) { }
public void OnUnityAdsShowClick(string adUnitId) { }
void OnDestroy()
{
// Clean up the button listeners:
}
}
btnTestAdsManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class btnTestAdsManager : MonoBehaviour
{
Button btnTestAds;
#if UNITY_IOS
string adUnitId = "39XXXXX";
#endif
#if UNITY_ANDROID
// _adUnitId = _androidAdUnitId;
string adUnitId = "39XXXXX";
#endif
// Start is called before the first frame update
void Start()
{
btnTestAds = GameObject.Find("btnTestAds1").gameObject.GetComponent<Button>();
btnTestAds.onClick.AddListener(() => {
if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
|| (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
{
//
RewardedAds ms = new RewardedAds();
ms.ShowAd();
ms.OnUnityAdsShowComplete(adUnitId, UnityAdsShowCompletionState.COMPLETED);
}
});
}
}
OR
btnTestAds.onClick.AddListener(() => {
if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
|| (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
{
//
RewardedAds ms = new RewardedAds();
ms.ShowAd();
}
});
When I press Button (TestAds) , Ads is shown, but reward not work (reward for showing ads not received)
If I use:
btnTestAds.onClick.AddListener(() => {
if ((Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
|| (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork))
{
//
GameObject.Find("UnityAdsManager").GetComponent<RewardedAds>().ShowAd();
}
});
That receive error in log
Error showing Ad Unit 39XXXXX: NOT_READY - Placement 39XXXXX is not ready