In unity editor ads work perfectly fine, but in my actually app you have to press the watch ad button twice.
`using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
[SerializeField] Button _showAdButton;
//[SerializeField] string _androidAdUnitId = “Rewarded_Android”;
[SerializeField] string _iOSAdUnitId = “Rewarded_iOS”;
string _adUnitId = null; // This will remain null for unsupported platforms
//string _adUnitId2 = null;
public GameObject continueScreen;
public ShopScript script;
public bool isActive;
public Spawner Spawner;
public bool anotherLife;
public bool coin;
/*void Start()
{
LoadAd();
}*/
public void StartingUpLogic()
{
LoadAd();
}
void Awake()
{
LoadAd();
// Get the Ad Unit ID for the current platform:
// Disable the button until the ad is ready to show:
_showAdButton.interactable = true;
isActive = false;
}
// Call this public method when you want to get an ad ready to show.
public void LoadAd()
{
// IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
Debug.Log("Loading Ad: " + _adUnitId);
//Debug.Log("Loading Ad: " + _adUnitId2);
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))
{
// Configure the button to call the ShowAd() method when clicked:
_showAdButton.onClick.AddListener(showAdForLife);
//_showAdButton.onClick.AddListener(showAdForCoin);
// Enable the button for users to click:
_showAdButton.interactable = true;
}
}
public void Activated()
{
isActive = true;
}
public void coinActivated()
{
coin = 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:
coinActivated();
Advertisement.Show(_adUnitId, this);
Debug.Log("video button clicked");
/*if (gameObject.tag == "watchadbutton")
{
Advertisement.Show(_adUnitId2, this);
Debug.Log("coin button clicked");
}*/
LoadAd();
}
public void showAdForLife()
{
anotherLife = true;
_showAdButton.interactable = false;
Advertisement.Show(_adUnitId, this);
Debug.Log("video button clicked");
LoadAd();
}
/*public void showAdForCoin()
{
Advertisement.Show(_adUnitId2, this);
Debug.Log("coin button clicked");
}*/
// Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
{
Spawner = GameObject.FindGameObjectWithTag("Logic").GetComponent<Spawner>();
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED) && anotherLife == true)
{
Debug.Log("Unity Ads Rewarded Ad Completed");
// Grant a reward.
continueScreen.SetActive(false);
Spawner.revivedBeginning();
turnOffTrigger();
}
if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED) && coin == true)
{
Debug.Log("Unity Ads Rewarded Ad Completed");
// Grant a reward.
script = GameObject.FindGameObjectWithTag("ShopScript").GetComponent<ShopScript>();
script.Reward();
isActive = false;
turnOffCoinTrigger();
}
}
private void turnOffTrigger()
{
anotherLife = false;
}
private void turnOffCoinTrigger()
{
coin = false;
}
// 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:
_showAdButton.onClick.RemoveAllListeners();
}
}`
If you have any suggestions let me know!