Advertisement
DugganSC

Untitled

Jan 12th, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
  2. {
  3.     [SerializeField] Button _showAdButton;
  4.     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
  5.     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
  6.     string _adUnitId = null; // This will remain null for unsupported platforms
  7.  
  8.     void Awake()
  9.     {
  10.         // Get the Ad Unit ID for the current platform:
  11. #if UNITY_IOS
  12.         _adUnitId = _iOSAdUnitId;
  13. #elif UNITY_ANDROID
  14.         _adUnitId = _androidAdUnitId;
  15. #endif
  16.  
  17.         // Disable the button until the ad is ready to show:
  18.         _showAdButton.interactable = false;
  19.  
  20.         LoadAd();
  21.     }
  22.  
  23.     // Call this public method when you want to get an ad ready to show.
  24.     public void LoadAd()
  25.     {
  26.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
  27.         Debug.Log("Loading Ad: " + _adUnitId);
  28.         Advertisement.Load(_adUnitId, this);
  29.     }
  30.  
  31.     // If the ad successfully loads, add a listener to the button and enable it:
  32.     public void OnUnityAdsAdLoaded(string adUnitId)
  33.     {
  34.         Debug.Log("Ad Loaded: " + adUnitId);
  35.  
  36.         if (adUnitId.Equals(_adUnitId))
  37.         {
  38.             // Configure the button to call the ShowAd() method when clicked:
  39.             _showAdButton.onClick.AddListener(ShowAd);
  40.             // Enable the button for users to click:
  41.             _showAdButton.interactable = true;
  42.         }
  43.     }
  44.  
  45.     // Implement a method to execute when the user clicks the button:
  46.     public void ShowAd()
  47.     {
  48.         // Disable the button:
  49.         _showAdButton.interactable = false;
  50.         // Then show the ad:
  51.         Advertisement.Show(_adUnitId, this);
  52.     }
  53.  
  54.     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
  55.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
  56.     {
  57.         if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
  58.         {
  59.             Debug.Log("Unity Ads Rewarded Ad Completed");
  60.             // Grant a reward.
  61.  
  62.             // Eventually, this may involve a delay
  63.             LoadAd();
  64.         }
  65.     }
  66.  
  67.     // Implement Load and Show Listener error callbacks:
  68.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
  69.     {
  70.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
  71.         // Use the error details to determine whether to try to load another ad.
  72.     }
  73.  
  74.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
  75.     {
  76.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
  77.         // Use the error details to determine whether to try to load another ad.
  78.     }
  79.  
  80.     public void OnUnityAdsShowStart(string adUnitId) { }
  81.     public void OnUnityAdsShowClick(string adUnitId) { }
  82.  
  83.     void OnDestroy()
  84.     {
  85.         // Clean up the button listeners:
  86.         _showAdButton.onClick.RemoveAllListeners();
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement