I am attempting to have two different reward ads in my unity game. One is a "continueAd" and the other is a "DoubleRewards" ad. Both are on buttons in my game... the first allows you to revive if you watch an ad, and the second gives you double resources if you watch an ad. The issue I am having is after watching the continueAd, that one is called on every subsequent ad watch.
Sorry if this is confusingly worded... I'm not sure how else to word it. If I watch continueAd, I get the reward. If I then watch RewardDouble ad, I get both rewards (undesired). It is almost as though the Unity Editor has a queue and doesn't clear it?
My code, at first, was identical to this: https://docs.unity.com/ads/ImplementingRewardedAdsUnity.html
Now, in an effort to fix this issue, I have added a parent to this class that attempts to destroy the object after playing the ad. The next time this parent is enabled, it would have to recreate the button object.
public class RewardAdsParent : MonoBehaviour
{
RewardAdsButton _rewardAdsButton;
[SerializeField] Button _showAdButton;
[SerializeField] string _androidAdUnitId = "Rewarded_Android";
[SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
private void OnEnable()
{
if (_rewardAdsButton == null && _showAdButton.enabled)
{
_rewardAdsButton = _showAdButton.AddComponent<RewardAdsButton>();
_rewardAdsButton.Initialize(_showAdButton, _androidAdUnitId, _iOSAdUnitId, this);
_rewardAdsButton.enabled = true;
}
}
private void OnDisable()
{
PostAdCleanup();
}
public void PostAdCleanup()
{
Destroy(_rewardAdsButton, 0.25f); // don't love this delay, better would be to wait for an event
_rewardAdsButton = null;
}
}
Modified RewardAdsButton:
public void Initialize(Button showAdButton, string androidAdUnitId, string iOSAdUnitId,
RewardAdsParent rewardAdsParent)
{
this._showAdButton = showAdButton;
this._androidAdUnitId = androidAdUnitId;
this._iOSAdUnitId = iOSAdUnitId;
this.parent = rewardAdsParent;
// Get the Ad Unit ID for the current platform:
#if UNITY_IOS
_adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
_adUnitId = _androidAdUnitId;
#endif
if (_showAdButton != null)
{
//Disable the button until the ad is ready to show:
_showAdButton.interactable = false;
}
else
{
Debug.LogWarning("showAdButton was null");
}
if (Advertisement.isInitialized)
{
LoadAd();
}
}
void OnDestroy()
{
// Clean up the button listeners:
_showAdButton.onClick.RemoveAllListeners();
}
// 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.
RewardAdHandler.HandleReward(adUnitId);
// Load another ad:
// Advertisement.Load(_adUnitId, this);
parent.PostAdCleanup();
}
}
// 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))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(ShowAd);
// Enable the button for users to click:
_showAdButton.interactable = true;
}
}
// Implement a method to execute when the user clicks the button:
public void ShowAd()
{
// Disable the button:
_showAdButton.interactable = false;
// Then show the ad:
Advertisement.Show(_adUnitId, this);
}
I have tried the solutions outlined here: Unity Ads 4.0 - Multiple OnUnityAdsShowComplete Callbacks for Rewarded ads
