Search Unity

Adding reward script to unity ads file.

Discussion in 'Unity Ads & User Acquisition' started by s00ny5, Oct 26, 2016.

  1. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    Hello,

    I`m kinda new to this so hope I get some ideas from you guys.
    I got a small game to play with in unity and I added the ads to.

    The game is in java and my unity ads file is a cs#.

    All works as intended. When I click my ad button will pop up the ad.
    I`m stuck at adding the actual rewards for player.

    this is unity ads file
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3.  
    4. public class ShowAd : MonoBehaviour {
    5.  
    6.     public void PlayAd()
    7.     {
    8.         if (Advertisement.IsReady())
    9.         {
    10.             Advertisement.Show ("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
    11.         }
    12.     }
    13.  
    14.     private void HandleAdResult(ShowResult result)
    15.     {
    16.         switch (result)
    17.         {
    18.             case ShowResult.Finished:
    19.                 //Here i`m stuck :D
    20.                 Debug.Log("Players get coins");
    21.                 break;
    22.             case ShowResult.Skipped:
    23.                 Debug.Log("Player did not watch");
    24.                 break;
    25.             case ShowResult.Failed:
    26.                 Debug.Log("Player failed to lunch");
    27.                 break;
    28.         }
    29.     }
    30. }
    Game is a slots one based on coins.
    All I found on the game scripts in this file UserData.js

    Code (JavaScript):
    1. coins = PlayerPrefs.GetFloat("Coins");
    2.  
    3. function AddCoins(amount : float, increment : boolean, speed : int)
    4. {
    5.     coins += amount;
    6.     incrementMuliplier = speed * 10;
    7.     PlayerPrefs.SetFloat("Coins", coins);
    8.     if(!increment)
    9.     {
    10.         fluxCoins = coins;
    11.     }
    12.     UpdateUserInfo();
    13. }

    Can any one give me an help here ?

    Thanks
     
  2. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    ads 1.0
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;

    3. public class ShowAd : MonoBehaviour {
    4. public int coins;
    5. private string vid = "rewardedVideo";
    6. void Awake(){
    7. coins = PlayerPrefs.GetInt("Coins");
    8. }
    9. public void PlayAd()
    10. {
    11. if (Advertisement.IsReady())
    12. {
    13. Advertisement.Show (vid, new ShowOptions() { resultCallback = HandleAdResult });
    14. }
    15. }

    16. private void HandleAdResult(ShowResult result)
    17. {
    18. switch (result)
    19. {
    20. case ShowResult.Finished:
    21. AddCoins(amount);
    22. Debug.Log("Players get coins");
    23. break;
    24. case ShowResult.Skipped:
    25. Debug.Log("Player did not watch");
    26. break;
    27. case ShowResult.Failed:
    28. Debug.Log("Player failed to lunch");
    29. break;
    30. }
    31. }
    32. void AddCoins(int amount){
    33. coins += amount;
    34. PlayerPrefs.SetInt("Coins", coins);


    35. }
    36. }
    37. ads 2.0
    38. I had to put bool in ads 2.0 and call AddCoins() in update,maybe its fixed now try top script first

    39. using UnityEngine;
    40. using UnityEngine.Advertisements;
      1. public class ShowAd : MonoBehaviour {
      2. public int coins;
      3. private string vid = "rewardedVideo";
      4. private bool adDone = false;
      5. void Awake(){
      6. coins = PlayerPrefs.GetInt("Coins");
      7. }
      1. void Update(){
      2. if(adDone){
      3. AddCoins( amount);
      4. adDone = false;
      5. }
      6. }
      1. public void PlayAd()
      2. {
      3. if (Advertisement.IsReady())
      4. {
      5. Advertisement.Show (vid, new ShowOptions() { resultCallback = HandleAdResult });
      6. }
      7. }
      8. private void HandleAdResult(ShowResult result)
      9. {
      10. switch (result)
      11. {
      12. case ShowResult.Finished:
      13. adDone = true;
      14. Debug.Log("Players get coins");
      15. break;
      16. case ShowResult.Skipped:
      17. Debug.Log("Player did not watch");
      18. break;
      19. case ShowResult.Failed:
      20. Debug.Log("Player failed to lunch");
      21. break;
      22. }
      23. }
      24. void AddCoins(int amount){
      25. coins += amount;
      26. PlayerPrefs.SetInt("Coins", coins);
      27. }
      28. }
      29. Does this help?
     
  3. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    hi,

    Thank you for your replay.
    Did not did the trick was doing funny things like putting balance on 0 or started all the time with what I set.

    Any way I did it like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using System.Collections;
    4.  
    5. public class ShowAd : MonoBehaviour {
    6.     public float coinsAd;
    7.     public void PlayAd()
    8.     {
    9.         if (Advertisement.IsReady())
    10.         {
    11.             Advertisement.Show ("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
    12.         }
    13.     }
    14.  
    15.     private void HandleAdResult(ShowResult result)
    16.     {
    17.         switch (result)
    18.         {
    19.             case ShowResult.Finished:
    20.                 if (PlayerPrefs.HasKey("Coins"))
    21.                 {
    22.                     coinsAd = PlayerPrefs.GetFloat("Coins");
    23.                     coinsAd += 100;
    24.                     PlayerPrefs.SetFloat("Coins", coinsAd);
    25.                 }
    26.                     Debug.Log("Players get coins");
    27.                 break;
    28.             case ShowResult.Skipped:
    29.                 Debug.Log("Player did not watch");
    30.                 break;
    31.             case ShowResult.Failed:
    32.                 Debug.Log("Player failed to lunch");
    33.                 break;
    34.         }
    35.     }
    36. }
    37.  
    This do the trick but now the problem is wont update in real time.
    I have to close and reopen my game to update the coins

    I also saw on java is this
    UpdateUserInfo();

    and the function is:
    Code (JavaScript):
    1. function UpdateUserInfo()
    2. {
    3.     coinText.text = fluxCoins.ToString("n2");
    4.     levelText.text = level.ToString();
    5.     expText.text = experience.ToString("n0") + " / " + experienceToLevel.ToString("n0");
    6. }
    Any idea ?

    Regards
     
  4. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using System.Collections;

    4. public class ShowAd : MonoBehaviour {
    5. public TextMesh coinText;
    6. public int coinsAd = 0;
    7. void Awake(){
    8. if (PlayerPrefs.HasKey("Coins"))
    9. {
    10. coinsAd = PlayerPrefs.GetInt("Coins");
    11. coinText.text ="" + coinsAd;
    12. }
    • }
    • public void PlayAd()
    • {
    • if (Advertisement.IsReady())
    • {
    • Advertisement.Show ("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
    • }
    • }
    • private void HandleAdResult(ShowResult result)
    • {
    • switch (result)
    • {
    • case ShowResult.Finished:
    • coinsAd += 100;
    • coinText.text ="" + coinsAd;
    • }
    • break;
    • case ShowResult.Skipped:
    • break;
    • case ShowResult.Failed:
    • break;
    • }
    • }
    • }
    • coins dont need float as its not a decimal number (1.4) its a whole number
    • place Textmesh in Your Scene and add in inspector
    • sorry my first post was a little messy
     
  5. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    1. my script gets messed when i post but check the changes from your last script to mine
     
  6. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    Hi edg,

    Well tried with int and just wont work I dont know why.
    Changed to float your script and its working but I get this error:

    Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using System.Collections;
    4.  
    5. public class ShowAd : MonoBehaviour
    6. {
    7.     public TextMesh coinText;
    8.     public float coinsAd = 0;
    9.     void Awake()
    10.     {
    11.         if (PlayerPrefs.HasKey("Coins"))
    12.         {
    13.             coinsAd = PlayerPrefs.GetFloat("Coins");
    14.             coinText.text = "" + coinsAd;
    15.         }
    16.     }
    17.     public void PlayAd()
    18.     {
    19.         if (Advertisement.IsReady())
    20.         {
    21.             Advertisement.Show("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
    22.         }
    23.     }
    24.     private void HandleAdResult(ShowResult result)
    25.     {
    26.         switch (result)
    27.         {
    28.             case ShowResult.Finished:
    29.                 coinsAd += 100;
    30.                 PlayerPrefs.SetFloat("Coins", coinsAd);
    31.                 coinText.text = "" + coinsAd;
    32.         Debug.Log("Players get coins");
    33.         break;
    34. case ShowResult.Skipped:
    35. Debug.Log("Player did not watch");
    36.         break;
    37. case ShowResult.Failed:
    38. Debug.Log("Player failed to lunch");
    39.         break;
    40.     }
    41. }
    42. }
    This is the inspector:



    I dont know what you mean by this:

    Regards
     
  7. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    Unity has 3dtext ,place in scene ,then add in inspector ,it will show coins as text in scene,
    Playerpref might give u error because "Coins" is already set as float.delete playerprefs and set as int might work. (playerpref.deleteAll)
     
  8. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    Hi,

    Remade all all added some extra info

    This is the code now:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Advertisements;
    3. using System.Collections;
    4.  
    5. public class ShowAd : MonoBehaviour
    6. {
    7.     public float coinsAd = 0;
    8.     public float fluxCoins;
    9.     public GameObject userDataPrefab;
    10.     public GameObject userData;
    11.     public GameObject CoinsGO;
    12.     void Start()
    13.     {
    14.         if (PlayerPrefs.HasKey("Coins"))
    15.         {
    16.             coinsAd = PlayerPrefs.GetFloat("Coins");
    17.             fluxCoins = coinsAd;
    18.         }
    19.     }
    20.     public void PlayAd()
    21.     {
    22.         if (Advertisement.IsReady())
    23.         {
    24.             Advertisement.Show("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
    25.         }
    26.     }
    27.     private void HandleAdResult(ShowResult result)
    28.     {
    29.         switch (result)
    30.         {
    31.             case ShowResult.Finished:
    32.                 if (GameObject.Find("UserData(Clone)"))
    33.                 {
    34.                     userData = GameObject.Find("UserData(Clone)"); // Sesion UserData
    35.                     CoinsGO = GameObject.Find("Coins"); // This is the UserData(Clone) Coins
    36.                     fluxCoins += 1000;
    37.                     PlayerPrefs.SetFloat("Coins", fluxCoins); //This set the coins betwen sessions
    38.                     // Need to update on this session the Coins.
    39.                 }
    40.                 Debug.Log("Players get coins");
    41.                 break;
    42.             case ShowResult.Skipped:
    43.                 Debug.Log("Player did not watch");
    44.                 break;
    45.             case ShowResult.Failed:
    46.                 Debug.Log("Player failed to lunch");
    47.                 break;
    48.         }
    49.     }
    50. }
    This is the User DB on session
    1.
    2.
    3.

    Once the player saw the ad it will reward the coins and set with PlayerPrefs his coins.. how ever this need restart to see the new balance.

    My question is how do I change the Text on Coins to update on session.
    After this "PlayerPrefs.SetFloat("Coins", fluxCoins);" I think I should run something to update the UserData(Clone) with new balance so will reflect the new balance without restart.

    Regards
     
  9. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    Is userdata in the scene or instantiated on start cause I see userdata (clone) ?
     
  10. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    I think userdata is the database when game is in off mode.
    Once the game start will clone the userdata "userdata (clone)" and all mod. made here will save in userdata
    http://puu.sh/s0RKM/3c4a4b1e8f.png
     
  11. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    give userdata a tag.
    if userdata instatiate on awake, maybe find with tag on start instead of every time ad ends;
    reference the script UserData userData instead of the Gameobject;

    userData.coinText.text = "" + fluxCoins;
     
  12. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    private UserData userData
    userData = GameObject.FindWithTag ("the tag u pick").GetComponent<UserData>(). as UserData ;
    userData.coinText.text = "" + fluxCoins;
     
  13. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    Hi,

    It has a tag "Player" i`ll try to do what u said and see.

    Regards
     
  14. s00ny5

    s00ny5

    Joined:
    Oct 26, 2016
    Posts:
    7
    Getting this now

    Also I moved the script to Plugins directory as UserDara script is a js.

    Regards
     
  15. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    I tried to reference js script from c# script with no success maybe cannot be done I'm not sure.

    Try send message to userdata instead.add function in user data to receive message

    I started with js myself and then switched to c#
    Found it was better for scripting.
     
  16. edg12345

    edg12345

    Joined:
    Feb 7, 2015
    Posts:
    22
    GameObject UserData = GameObject.FindWithTag ("player");
    UserData.Send message("Function Name in UserData to receive message");