Search Unity

Resolved How to retrieve Real Money Purchases

Discussion in 'Economy' started by lambch0p, Nov 25, 2021.

  1. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    Hi,

    In the getting started guide, the suggested workflow is to retrieve the RMPs from Economy and display them to the user. What's the API call to do this? I've looked through the API guide but couldn't spot anything obvious.

    Thanks
    Mick
     
  2. EllieF_Unity

    EllieF_Unity

    Unity Technologies

    Joined:
    Nov 27, 2020
    Posts:
    39
    Hi Mick,

    The API call to do this is "GetRealMoneyPurchasesAsync".

    This call will return a List of type RealMoneyPurchaseDefinition. You can find more details about the data type on this page in the documentation.
     
  3. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    OK. I'll open my eyes next time.


    GetRealMoneyPurchasesAsync
     
  4. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    I hope I'm not still being stupid (it happens a lot), but i've add the Unity.Services.Economy namespace to a class, and it can't find the GetRealMoneyPurchasesAsync method. I've opened the library in the object browser in visual studio and I can't see the method in there either.
    I'm using
    "com.unity.services.economy": "1.0.0-pre.6"

    Thanks
    Mick
     

    Attached Files:

  5. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    using Unity.Services.Economy;

    var stuff = Economy.Configuration.GetRealMoneyPurchasesAsync();
     
  6. lambch0p

    lambch0p

    Joined:
    Oct 22, 2014
    Posts:
    62
    Here's a really basic example for getting RealMoneyPurchases (make sure that options.SetEnvironmentName is set to the environment where your RMPs are published):


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using Unity.Services.Economy;
    4. using Unity.Services.Economy.Model;
    5. using Unity.Services.Core;
    6. using Unity.Services.Authentication;
    7. using Unity.Services.Core.Environments;
    8.  
    9. public class EconomyActions : MonoBehaviour
    10. {
    11.  
    12.     async void Awake()
    13.     {
    14.         var options = new InitializationOptions();
    15.  
    16.         options.SetEnvironmentName("development");
    17.         await UnityServices.InitializeAsync(options);
    18.  
    19.         AuthenticationService.Instance.SignedIn += delegate
    20.         {
    21.             Debug.Log("All signed in and ready to go!");
    22.             Debug.Log($"Player ID is {AuthenticationService.Instance.PlayerId}");
    23.             Debug.Log($"Access Token is {AuthenticationService.Instance.AccessToken}");
    24.         };
    25.         await AuthenticationService.Instance.SignInAnonymouslyAsync();
    26.         FetchRealMoneyPurchases();
    27.     }
    28.  
    29.     public async void FetchRealMoneyPurchases()
    30.     {
    31.         if (!IsAuthenticationSignedIn())
    32.         {
    33.             return;
    34.         }
    35.  
    36.         List<RealMoneyPurchaseDefinition> realMoneyPurchases = await Economy.Configuration.GetRealMoneyPurchasesAsync();
    37.  
    38.         if (realMoneyPurchases.Count == 0)
    39.         {
    40.             Debug.Log("No Purchases");
    41.         }
    42.         else
    43.         {
    44.             foreach (var rmp in realMoneyPurchases)
    45.             {
    46.                 Debug.Log($"RMP {rmp.Name}: {rmp.Id}");
    47.             }
    48.         }
    49.     }
    50.     static bool IsAuthenticationSignedIn()
    51.     {
    52.         if (!AuthenticationService.Instance.IsSignedIn)
    53.         {
    54.             Debug.Log("Wait until sign in is done");
    55.             return false;
    56.         }
    57.  
    58.         return true;
    59.     }
    60. }
     
    unity_Ctri likes this.
  7. Laurie-Unity

    Laurie-Unity

    Unity Technologies

    Joined:
    Mar 5, 2020
    Posts:
    220
    Hi Mick,

    I'm delighted to see that you managed to retrieve your Real Money Purchase items from the store.
    Thanks for sharing your code and experince with us. Your code looks good and I can confirm it works :)