Advertisement
k0fe

Untitled

Apr 7th, 2020
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Advertisements;
  4. using System.Collections.Generic;
  5.  
  6. [RequireComponent(typeof(Button))]
  7. [AddComponentMenu("Ads/Rewarded", 1)]
  8. public class RewardedAdsButton : MonoBehaviour, IUnityAdsListener
  9. {
  10.     public string myPlacementId = "rewardedVideo";
  11.     private Button myButton;
  12.     private GameManager manager;
  13.  
  14.     void Start()
  15.     {
  16.         myButton = GetComponent<Button>();
  17.  
  18.         // Set interactivity to be dependent on the Placement’s status:
  19.         myButton.interactable = Advertisement.IsReady(myPlacementId);
  20.  
  21.         // Map the ShowRewardedVideo function to the button’s click listener:
  22.         if (myButton)
  23.         {
  24.             myButton.onClick.AddListener(ShowRewardedVideo);
  25.         }
  26.  
  27.         manager = GameManager.Instance;
  28.  
  29.         // Initialize the Ads listener:
  30.         Advertisement.AddListener(this);
  31.     }
  32.  
  33.     // Implement a function for showing a rewarded video ad:
  34.     void ShowRewardedVideo()
  35.     {
  36.         Advertisement.Show(myPlacementId);
  37.     }
  38.  
  39.     // Implement IUnityAdsListener interface methods:
  40.     public void OnUnityAdsReady(string placementId)
  41.     {
  42.         // If the ready Placement is rewarded, activate the button:
  43.         if (placementId == myPlacementId)
  44.         {
  45.             myButton.interactable = true;
  46.         }
  47.     }
  48.  
  49.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
  50.     {
  51.         // Define conditional logic for each ad completion status:
  52.         if (showResult == ShowResult.Finished && placementId == myPlacementId)
  53.         {
  54.             // Reward the user for watching the ad to completion.
  55.             manager.Coins += 150;
  56.         }
  57.         else if (showResult == ShowResult.Skipped)
  58.         {
  59.             // Do not reward the user for skipping the ad.
  60.         }
  61.         else if (showResult == ShowResult.Failed)
  62.         {
  63.             Debug.LogWarning("The ad did not finish due to an error.");
  64.         }
  65.     }
  66.  
  67.     public void OnUnityAdsDidError(string message)
  68.     {
  69.         // Log the error.
  70.     }
  71.  
  72.     public void OnUnityAdsDidStart(string placementId)
  73.     {
  74.         // Optional actions to take when the end-users triggers an ad.
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement