Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //First Script
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Advertisements;
- using TMPro;
- using UnityEngine.SceneManagement;
- public class ADscriptCoin : MonoBehaviour, IUnityAdsListener
- {
- #if UNITY_IOS
- private string gameId = "3539240";
- #elif UNITY_ANDROID
- private string gameId = "3539241";
- #endif
- Button myButton;
- public string myPlacementId = "video";
- public TextMeshProUGUI CoinText;
- public int RewardCoins = 50;
- void Start()
- {
- myButton = GetComponent<Button>();
- // Set interactivity to be dependent on the Placement’s status:
- myButton.interactable = Advertisement.IsReady(myPlacementId);
- if (myButton) myButton.onClick.AddListener(ShowRewardedVideo);
- // Initialize the Ads listener and service:
- Advertisement.AddListener(this);
- Advertisement.Initialize(gameId, true);
- }
- // Implement a function for showing a rewarded video ad:
- public void ShowRewardedVideo()
- {
- Advertisement.Show(myPlacementId);
- }
- // Implement IUnityAdsListener interface methods:
- public void OnUnityAdsReady(string placementId)
- {
- // If the ready Placement is rewarded, activate the button:
- if (placementId == myPlacementId)
- {
- myButton.interactable = true;
- }
- }
- public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
- {
- // Define conditional logic for each ad completion status:
- if (showResult == ShowResult.Finished)
- {
- PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins", 20) + RewardCoins);
- CoinText.text = PlayerPrefs.GetInt("Coins", 20).ToString() + "G";
- SSTools.ShowMessage("Added " + RewardCoins.ToString() + " Coins " , SSTools.Position.bottom, SSTools.Time.oneSecond);
- Advertisement.RemoveListener(this);
- }
- else if (showResult == ShowResult.Skipped)
- {
- PlayerPrefs.SetInt("Coins", PlayerPrefs.GetInt("Coins", 20) + RewardCoins);
- CoinText.text = PlayerPrefs.GetInt("Coins", 20).ToString() + "G";
- SSTools.ShowMessage("Added " + RewardCoins.ToString() + " Coins ", SSTools.Position.bottom, SSTools.Time.oneSecond);
- Advertisement.RemoveListener(this);
- }
- else if (showResult == ShowResult.Failed)
- {
- SSTools.ShowMessage("The ad did not finish due to an error.", SSTools.Position.bottom, SSTools.Time.oneSecond);
- }
- Advertisement.RemoveListener(this);
- myButton.onClick.RemoveListener(ShowRewardedVideo);
- }
- public void OnUnityAdsDidError(string message)
- {
- SSTools.ShowMessage("The ad did not finish due to an error.", SSTools.Position.bottom, SSTools.Time.oneSecond);
- Advertisement.RemoveListener(this);
- myButton.onClick.RemoveListener(ShowRewardedVideo);
- }
- public void OnUnityAdsDidStart(string placementId)
- {
- // Optional actions to take when the end-users triggers an ad.
- }
- public void OnDestroy()
- {
- Advertisement.RemoveListener(this);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement