Search Unity

Problem with AdButton

Discussion in 'Unity Ads & User Acquisition' started by Pedro Luckas, Aug 5, 2015.

  1. Pedro Luckas

    Pedro Luckas

    Joined:
    Jun 22, 2015
    Posts:
    6
    I'm trying to create a system that recharge an X number of lives when the Player watch an entire ad. However the ShowResult.Finished event is never called.

    Sorry for my bad english.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using UnityEngine.UI;
    4. using System;
    5. using System.Collections;
    6.  
    7. public class AdReward : MonoBehaviour
    8. {
    9.     [SerializeField] private float CD = 1;
    10.     [SerializeField] private int recharges = 5;
    11.     [SerializeField] private int maxLifes = 15;
    12.     [SerializeField] private string gameID = "53863";
    13.     [SerializeField] private string zoneID = "energyRewardedVideo";
    14.  
    15.  
    16.     private int lifes;
    17.     private double counter;
    18.     private bool showAD = false;
    19.     private bool loaded = false;
    20.  
    21.     private Button reloadButton;
    22.     private Text buttonText;
    23.     private ShowOptions options;
    24.     private DateTime lastReloadDate;
    25.  
    26.  
    27.     void Awake()
    28.     {
    29.         Advertisement.Initialize (gameID);
    30.     }
    31.    
    32.     void Start()
    33.     {
    34.  
    35.         lastReloadDate = DateTime.Parse (PlayerPrefs.GetString ("lastReloadDate", DateTime.Now.ToString ()));
    36.         if (!PlayerPrefs.HasKey ("lastReloadDate"))
    37.             PlayerPrefs.SetString ("lastReloadDate", lastReloadDate.ToString ());
    38.        
    39.  
    40.         reloadButton = GetComponent<Button> ();
    41.         buttonText = GetComponentInChildren<Text> ();
    42.  
    43.  
    44.         lifes = PlayerPrefs.GetInt ("lifes");
    45.        
    46.        
    47.        
    48.     }
    49.    
    50.     void Update()
    51.     {
    52.  
    53.         counter = (DateTime.Now - lastReloadDate).TotalMinutes;
    54.        
    55.  
    56.         if(counter >= CD)
    57.         {
    58.  
    59.             if(!showAD)
    60.             {
    61.                 buttonText.text = "Hold On";
    62.  
    63.                 if(!loaded)
    64.                 {
    65.                     LoadAd();
    66.                     loaded = true;
    67.                 }
    68.             }
    69.  
    70.             else
    71.             {
    72.                 buttonText.text = "Click to Reload";
    73.                 reloadButton.onClick.AddListener(() => { OnButtonDown();  });
    74.             }
    75.         }
    76.  
    77.         else
    78.         {
    79.             buttonText.text = (int)(CD - (int)counter) + "m left";
    80.             reloadButton.onClick.RemoveAllListeners();
    81.         }
    82.        
    83.  
    84.     }
    85.  
    86.     void OnButtonDown()
    87.     {
    88.         Advertisement.Show (zoneID, options);
    89.         lastReloadDate = DateTime.Now;
    90.         PlayerPrefs.SetString ("lastReloadDate", lastReloadDate.ToString ());// salva o tempo
    91.         showAD = false;
    92.         loaded = false;
    93.        
    94.     }
    95.    
    96.  
    97.     void LoadAd()
    98.     {
    99.         StartCoroutine (LoadEvent ());
    100.     }
    101.    
    102.     IEnumerator LoadEvent()
    103.     {
    104.         options = new ShowOptions ();
    105.         options.resultCallback = CallBackhandler;
    106.        
    107.        
    108.         while(!Advertisement.IsReady(zoneID))
    109.             yield return null;
    110.  
    111.  
    112.         showAD = true;
    113.     }
    114.    
    115.    
    116.     void CallBackhandler(ShowResult result)
    117.     {
    118.         if (result == ShowResult.Finished)
    119.         {
    120.             RewardPlayer();
    121.             Application.LoadLevel (Application.loadedLevel);
    122.         }
    123.         if(result == ShowResult.Skipped)
    124.         {
    125.  
    126.             Debug.Log("Skipped");
    127.         }
    128.         if (result == ShowResult.Failed)
    129.         {
    130.  
    131.             Debug.Log("Fail");
    132.         }
    133.        
    134.     }
    135.    
    136.     void RewardPlayer()
    137.     {
    138.         Debug.Log ("Finished");
    139.         lifes += recharges;
    140.         if (lifes > maxLifes)
    141.             lifes = maxLifes;
    142.         PlayerPrefs.SetInt ("lifes", lifes);
    143.     }
    144. }
    145.  
     
  2. rasmus-unity

    rasmus-unity

    Moderator

    Joined:
    Aug 15, 2014
    Posts:
    1,312
    Are you sure the LoadEvent() method is called before calling Show()? Your code seems a little complex to me, e.g. why are you setting button eventhandler on Update (e.g. every frame), and why are the options instantiated elsewhere from where used?

    Think you should generally clean up your code, and instantiate the ShowOptions object right before your call to Show(), as it's not being used for anything else, so cleaner just to keep it in the same scope, as where it's being used.

    Hope this helps.

    /Rasmus
     
  3. Pedro Luckas

    Pedro Luckas

    Joined:
    Jun 22, 2015
    Posts:
    6
    First thanks for your help.

    When I'm in the editor the script works