Search Unity

Unity Ads Reward the player with tips (text) and not coins, stars ...

Discussion in 'Scripting' started by Wiz0411, Jan 7, 2019.

  1. Wiz0411

    Wiz0411

    Joined:
    Nov 22, 2018
    Posts:
    1
    Hello, I am a beginner, thank you for your help.
    Rewarded Ads work perfectly but I need to reward the player with tips, not coins or anything like that. My code works in the player but not on android device. What is wrong?
    I am using Unity 2018.2.17f1
    Thank you

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.Monetization;
    5. using UnityEngine.UI;
    6.  
    7. public class RewardedAdsPlacement : MonoBehaviour
    8. {
    9.     public Text Tips;
    10.     public Text Tips01;
    11.  
    12.     public string placementId = "rewardedVideo";
    13.  
    14.     public void ShowAd()
    15.     {
    16.         StartCoroutine(WaitForAd());
    17.     }
    18.  
    19.     IEnumerator WaitForAd()
    20.     {
    21.         while (!Monetization.IsReady(placementId))
    22.         {
    23.             yield return null;
    24.         }
    25.  
    26.         ShowAdPlacementContent ad = null;
    27.         ad = Monetization.GetPlacementContent(placementId) as ShowAdPlacementContent;
    28.  
    29.         if (ad != null)
    30.         {
    31.             ad.Show(AdFinished);
    32.         }
    33.     }
    34.  
    35.     void AdFinished(ShowResult result)
    36.     {
    37.         if (result == ShowResult.Finished)
    38.         {
    39.             // Reward the player
    40.             Tips.GetComponent<Text>().text = "Tips : " + Tips01.GetComponent<Text>().text;
    41.         }
    42.     }
    43.     void Update()
    44.     {
    45.        
    46.     }
    47.  
    48. }
    49.  
     
  2. Jose-daniel

    Jose-daniel

    Joined:
    Oct 29, 2015
    Posts:
    18
    This is the code ;)

    Code (CSharp):
    1.    
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Advertisements;
    6. using UnityEngine.Analytics;
    7.  
    8. public class Ads: MonoBehaviour
    9. {
    10. public void ShowRewardedAd()
    11.     {
    12.         const string RewardedPlacementId = "rewardedVideo";
    13.  
    14.  
    15.         if (!Advertisement.IsReady(RewardedPlacementId))
    16.         {
    17.             Debug.Log(string.Format("Ads not ready for placement '{0}'", RewardedPlacementId));
    18.             return;
    19.         }
    20.  
    21.         var options = new ShowOptions { resultCallback = HandleShowResult };
    22.         Advertisement.Show(RewardedPlacementId, options);
    23.  
    24.     }
    25.  
    26.  
    27.     private void HandleShowResult(ShowResult result)
    28.     {
    29.         switch (result)
    30.         {
    31.             case ShowResult.Finished:
    32.                 Debug.Log("The ad was successfully shown.");
    33.  
    34.                //Your redward. Coins += 100.
    35.  
    36.                 PlayerPrefs.SetInt("Coin", Coins);
    37.  
    38.                 AnalyticsEvent.Custom("RedwardAdsComplete"); //Add event is complete in Unity Analytics.
    39.  
    40.                 //
    41.                 // YOUR CODE TO REWARD THE GAMER
    42.                 // Give coins etc.
    43.                 break;
    44.             case ShowResult.Skipped:
    45.                 Debug.Log("The ad was skipped before reaching the end.");
    46.                 break;
    47.             case ShowResult.Failed:
    48.                 Debug.LogError("The ad failed to be shown.");
    49.                 break;
    50.         }
    51.     }