Advertisement
k0fe

Untitled

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