Quantcast
Channel: Questions in topic: "ads"
Viewing all articles
Browse latest Browse all 1416

Unity Ads works only once with ad package 4.4.2

$
0
0
here is my Code for Ads, I appreciate you helping me beginner. using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine.Advertisements; public class AdsInitializer : MonoBehaviour, IUnityAdsInitializationListener { [SerializeField] string _androidGameId; [SerializeField] string _iOSGameId; [SerializeField] bool _testMode = false; private string _gameId; [SerializeField] RewardedAdsButton rewardedAdsButton; void Awake() { InitializeAds(); } public void InitializeAds() { #if UNITY_IOS _gameId = _iOSGameId; #elif UNITY_ANDROID _gameId = _androidGameId; #elif UNITY_EDITOR _gameId = _androidGameId; //Only for testing the functionality in the Editor #endif if (!Advertisement.isInitialized && Advertisement.isSupported) { Advertisement.Initialize(_gameId, _testMode, this); Debug.Log("Init Success:"); } } public void OnInitializationComplete() { StartCoroutine(Wait()); } public void OnInitializationFailed(UnityAdsInitializationError error, string message) { Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}"); } private IEnumerator Wait() { while (rewardedAdsButton._adUnitId == "") { yield return new WaitForSeconds(0.25f); } rewardedAdsButton.LoadAd(); } } and RewardedAds Script using UnityEngine; using UnityEngine.UI; using UnityEngine.Advertisements; public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener { [SerializeField] Button _showAdButton; [SerializeField] string _androidAdUnitId = "Rewarded_Android"; [SerializeField] string _iOSAdUnitId = "Rewarded_iOS"; public string _adUnitId; // This will remain null for unsupported platforms [SerializeField] PlayerController playerController; [SerializeField] ObstacleSpawner obstacleSpawner; public GameObject resetPosition; private void Start() { playerController = GameObject .FindGameObjectWithTag("Player") .GetComponent(); resetPosition = GameObject.FindGameObjectWithTag("ResetPosition"); obstacleSpawner = GameObject .FindGameObjectWithTag("OBSSpawner") .GetComponent(); } void Awake() { // Get the Ad Unit ID for the current platform: #if UNITY_IOS _adUnitId = _iOSAdUnitId; #elif UNITY_ANDROID _adUnitId = _androidAdUnitId; #endif } // Call this public method when you want to get an ad ready to show. public void LoadAd() { // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script). Advertisement.Load(_adUnitId, this); } // If the ad successfully loads, add a listener to the button and enable it: public void OnUnityAdsAdLoaded(string adUnitId) { Debug.Log("Ad Loaded: " + adUnitId); if (adUnitId.Equals(_adUnitId)) { // Configure the button to call the ShowAd() method when clicked: _showAdButton.onClick.AddListener(ShowAd); // Enable the button for users to click: _showAdButton.interactable = true; } } // Implement a method to execute when the user clicks the button: public void ShowAd() { // Disable the button: _showAdButton.interactable = false; // Then show the ad: Advertisement.Show(_adUnitId, this); } // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward: public void OnUnityAdsShowComplete( string adUnitId, UnityAdsShowCompletionState showCompletionState ) { if ( adUnitId.Equals(_adUnitId)&& showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED) ) { Debug.Log("Unity Ads Rewarded Ad Completed"); // Grant a reward. Debug.Log("Reward Granted:"); playerController.isGameOver = false; playerController.GameOverPanel.SetActive(false); playerController.scoreText.SetActive(true); playerController.scoreText2.SetActive(true); playerController.cooldownButton.SetActive(true); _showAdButton.gameObject.SetActive(false); playerController.anim.SetTrigger("jump"); playerController.StartCoroutine("IncreaseGameSpeed"); playerController.transform.position = resetPosition.transform.position; obstacleSpawner.StartCoroutine("SpawnObstacles"); // Another Ad. Advertisement.Load(_adUnitId, this); } } // Implement Load and Show Listener error callbacks: public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message) { Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}"); // Use the error details to determine whether to try to load another ad. } public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message) { Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}"); // Use the error details to determine whether to try to load another ad. } public void OnUnityAdsShowStart(string adUnitId) { } public void OnUnityAdsShowClick(string adUnitId) { } // void OnDestroy() // { // // Clean up the button listeners: // _showAdButton.onClick.RemoveAllListeners(); // } }

Viewing all articles
Browse latest Browse all 1416

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>