Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. //First Script
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Advertisements;
  5. using TMPro;
  6. using UnityEngine.SceneManagement;
  7.  
  8.  
  9. public class ADscriptCoin : MonoBehaviour, IUnityAdsListener
  10. {
  11.  
  12. #if UNITY_IOS
  13.     private string gameId = "3539240";
  14. #elif UNITY_ANDROID
  15.     private string gameId = "3539241";
  16. #endif
  17.  
  18.     Button myButton;
  19.     public string myPlacementId = "video";
  20.     public TextMeshProUGUI CoinText;
  21.  
  22.     public int RewardCoins = 50;
  23.  
  24.     void Start()
  25.     {
  26.         myButton = GetComponent<Button>();
  27.  
  28.         // Set interactivity to be dependent on the Placement’s status:
  29.         myButton.interactable = Advertisement.IsReady(myPlacementId);
  30.  
  31.         if (myButton) myButton.onClick.AddListener(ShowRewardedVideo);
  32.  
  33.         // Initialize the Ads listener and service:
  34.         Advertisement.AddListener(this);
  35.         Advertisement.Initialize(gameId, true);
  36.     }
  37.  
  38.     // Implement a function for showing a rewarded video ad:
  39.     public void ShowRewardedVideo()
  40.     {
  41.         Advertisement.Show(myPlacementId);
  42.     }
  43.  
  44.     // Implement IUnityAdsListener interface methods:
  45.     public void OnUnityAdsReady(string placementId)
  46.     {
  47.         // If the ready Placement is rewarded, activate the button:
  48.         if (placementId == myPlacementId)
  49.         {
  50.             myButton.interactable = true;
  51.         }
  52.     }
  53.  
  54.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
  55.     {
  56.         // Define conditional logic for each ad completion status:
  57.         if (showResult == ShowResult.Finished)
  58.         {
  59.             PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins", 20) + RewardCoins);
  60.             CoinText.text = PlayerPrefs.GetInt("Coins", 20).ToString() + "G";
  61.             SSTools.ShowMessage("Added " + RewardCoins.ToString() + " Coins " , SSTools.Position.bottom, SSTools.Time.oneSecond);
  62.             Advertisement.RemoveListener(this);
  63.  
  64.  
  65.         }
  66.         else if (showResult == ShowResult.Skipped)
  67.         {
  68.             PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins", 20) + RewardCoins);
  69.             CoinText.text = PlayerPrefs.GetInt("Coins", 20).ToString() + "G";
  70.             SSTools.ShowMessage("Added " + RewardCoins.ToString() + " Coins ", SSTools.Position.bottom, SSTools.Time.oneSecond);
  71.             Advertisement.RemoveListener(this);
  72.  
  73.         }
  74.         else if (showResult == ShowResult.Failed)
  75.         {
  76.             SSTools.ShowMessage("The ad did not finish due to an error.", SSTools.Position.bottom, SSTools.Time.oneSecond);
  77.         }
  78.  
  79.         Advertisement.RemoveListener(this);
  80.         myButton.onClick.RemoveListener(ShowRewardedVideo);
  81.     }
  82.  
  83.     public void OnUnityAdsDidError(string message)
  84.     {
  85.         SSTools.ShowMessage("The ad did not finish due to an error.", SSTools.Position.bottom, SSTools.Time.oneSecond);
  86.        
  87.         Advertisement.RemoveListener(this);
  88.         myButton.onClick.RemoveListener(ShowRewardedVideo);
  89.     }
  90.  
  91.     public void OnUnityAdsDidStart(string placementId)
  92.     {
  93.         // Optional actions to take when the end-users triggers an ad.
  94.     }
  95.     public void OnDestroy()
  96.     {
  97.         Advertisement.RemoveListener(this);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement