# Unity

## Introduction

Every project may have multiple custom currencies. For example "diamonds" and/or "cash". Aside from these typical game currencies, projects wishing to sell NFTs must integrate a specific currency. The internal name for this currency is "rgn-coin". When the "rgn-coin" is presented in the UI it can have any currency name, for example, "gems".

The "rgn-coin" can only be sold using in-app purchases. For more information, please refer to the [Currency GDG](/play-sdk-documentation/game-design-guides/currency-iap.md#feature-overview).

Other typical currencies can be added and used in an expected manner - as rewards to the user's account, or sold for in-app purchases.

{% hint style="info" %}
We suggest utilizing [Unity's IAP Package](https://docs.unity3d.com/Packages/com.unity.purchasing@4.2/manual/index.html) to handle the in-app purchase process on Android and iOS.
{% endhint %}

### **Purchase** `rgn-coin` (IAP required)

Purchasing `rgn-coin` works a bit differently, it has its own API called `PurchaseRGNCoinAsync(string iapUUID)`

We recommend using the latest version of the Unity IAP plugin.

You will need to get the transactionId and receipt from the `PurchaseEventArgs` when the in app purchase is completed.&#x20;

<details>

<summary><code>rgn-coin</code> Products</summary>

10 rgn-coin :

```json
"uid": "4a13bcd3-ff64-43a4-8c13-e978d968f68c",
"priceInUSD": 0.99,
"quantity": 10
```

20 rgn-coin :

```json
"uid": "0eeae85c-c9af-47f0-8df4-56a4442c23d7",
"priceInUSD": 1.99,
"quantity": 20
```

30 rgn-coin :&#x20;

```json
"uid": "9d2ed68b-2a6c-45d4-bc07-29f256799943",
"priceInUSD": 2.99,
"quantity": 30
```

50 rgn-coin :&#x20;

```json
"uid": "b7bcbbf6-753d-47dd-8bff-93b4539c825d",
"priceInUSD": 4.99,
"quantity": 50
```

110 rgn-coin : &#x20;

```json
"uid": "b81e8e95-59a6-4a77-b1bc-f5b7d6eacfa4",
"priceInUSD": 9.99,
"quantity": 110
```

300 rgn-coin :&#x20;

```json
"uid": "f35c4f83-cf22-4723-828a-66e8069614eb",
"priceInUSD": 24.99,
"quantity": 300
```

500 rgn-coin :&#x20;

```json
"uid": "3c814835-9f6f-4947-98f0-6e70964b887d",
"priceInUSD": 39.99,
"quantity": 500
```

1000 rgn-coin :&#x20;

```json
"uid": "b65994dc-ecfd-4549-ab32-51e196947c8b",
"priceInUSD": 69.99,
"quantity": 1000
```

</details>

**Add rgn-coin to user**

```csharp
using UnityEngine;
using RGN.Modules.Currency;
using UnityEngine.Purchasing;

public class CurrenciesExamples : MonoBehaviour
{
    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
    {
        bool validPurchase = true; // Presume valid for platforms with no R.V.

        if (validPurchase)
        {
            Debug.Log($"Product {purchaseEvent.purchasedProduct.definition.id} Purchased");
            string transactionId = purchaseEvent.purchasedProduct.transactionID;
            string receipt = purchaseEvent.purchasedProduct.receipt;
            
            PurchaseRGNCoinAsync(transactionId, receipt);
        }
        else
        {
            Debug.LogError($"Something happened when trying to purchase product {purchaseEvent.purchasedProduct.definition.id}");
        }

        return PurchaseProcessingResult.Complete;
    }

    public async void PurchaseRGNCoinAsync(string transactionId, string receipt)
    {
        string rgnProductId = "4a13bcd3-ff64-43a4-8c13-e978d968f68c"; // 10 rgn-coin
        await CurrencyModule.I.PurchaseRGNCoinAsync(rgnProductId, transactionId, receipt);
    }
}
```

**Get user rgn-coin**&#x20;

```csharp
using System.Collections.Generic;
using UnityEngine;
using RGN.Modules.Currency;

public class CurrenciesExamples : MonoBehaviour
{
    public async void GetRgnCoinCurrenciesAsync()
    {
        List<Currency> currencies = await CurrencyModule.I.GetUserCurrenciesAsync();
        Currency readyCurrency = currencies.Find(x => x.name.Equals("rgn-coin"));
        Debug.Log($"User has {readyCurrency.quantity} rgn-coin");
    }
}
```

### Custom currencies

**Add soft currencies to user**

```csharp
using System.Collections.Generic;
using UnityEngine;
using RGN.Modules.Currency;

public class CurrenciesExamples : MonoBehaviour
{
    public async void AddSoftCurrenciesAsync()
    {
        Currency goldCurrency = new Currency
        {
            name = "gold",
            quantity = 10
        };
        List<Currency> currenciesToAdd = new List<Currency> { goldCurrency };
        await CurrencyModule.I.AddUserCurrenciesAsync(currenciesToAdd);
    }
}
```

**Get user soft currencies**

```csharp
using System.Collections.Generic;
using UnityEngine;
using RGN.Modules.Currency;

public class CurrenciesExamples : MonoBehaviour
{
    public async void GetSoftCurrenciesAsync()
    {
        List<Currency> currencies = await CurrencyModule.I.GetUserCurrenciesAsync();
        Currency goldCurrency = currencies.Find(x => x.name.Equals("gold"));
        Debug.Log($"User has {goldCurrency.quantity} gold");
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://playnetwork.gitbook.io/play-sdk-documentation/sdk-integration-guides/currency-iap/unity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
