RevenueCat

RevenueCat Documentation

Here you will find guides for getting RevenueCat setup and references for our SDKs and APIs.

Guides    Discussions

Unity

Step by step guide for using RevenueCat in your Unity app.

The Purchases SDK provided by RevenueCat allows you to implement subscriptions in your Unity app easily.

1. Add the Purchases Unity package

Get the latest version of Purchases.unitypackage from Github. Add it to your Unity project.

Android Dependencies

You need to manually add the required Android dependencies to your gradle file. This can be done by editing the generated gradle file, but this will be blow away when you create a new Android player.

Instead use a gradle template to add the dependencies manually.

compile 'com.revenuecat.purchases:purchases:1.3.5'
compile 'com.android.billingclient:billing:1.0'

2. Create a GameObject with the Purchases behavior

The Purchases package will include a MonoBehavior called Purchases. This will be your access point to RevenueCat from inside Unity. It should be instantiated once and kept as a singleton. You can use properties to configure your API Key, app user ID (if you have one), and product identifiers you want to fetch.

The Purchases behaviour is configured with your RevenueCat API key and the product identifiers you want to fetch.

The Purchases behaviour is configured with your RevenueCat API key and the product identifiers you want to fetch.

3. Subclass Purchases.Listener MonoBehaviour

The Purchases behavior takes one additional parameter, a GameObject with a Purchases.Listener component. This will be where you handle purchase events, and updated subscriber information from RevenueCat. Here is a simple example:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PurchasesListener : Purchases.Listener
{

    public RectTransform parentPanel;
    public GameObject buttonPrefab;
    public Text purchaserInfoLabel;

    void ButtonClicked(string product)
    {
        Purchases purchases = GetComponent<Purchases>();
        purchases.MakePurchase(product);
    }

    public override void ProductsReceived(List<Purchases.Product> products)
    {
      	// Create a Purchase button for each product
        int yOffset = 0;
        foreach (Purchases.Product p in products)
        {
            GameObject button = (GameObject)Instantiate(buttonPrefab);

            button.transform.SetParent(parentPanel, false);
            button.transform.position = new Vector2(button.transform.position.x, button.transform.position.y - yOffset);

            yOffset += 70;

            Button tempButton = button.GetComponent<Button>();

            Text textComponent = tempButton.GetComponentsInChildren<Text>()[0];
            textComponent.text = p.identifier + " " + p.priceString;

            tempButton.onClick.AddListener(() => ButtonClicked(p.identifier));

        }
    }
  	
    public override void PurchaseCompleted(string productIdentifier, Purchases.Error error, Purchases.PurchaserInfo purchaserInfo, bool userCanceled)
    {
        if (userCanceled)
        {
            Debug.Log("User canceled, don't show an error");
        }

        if (purchaserInfo != null)
        {
            DisplayPurchaserInfo(purchaserInfo);
        }
    }

    public override void PurchaserInfoReceived(Purchases.PurchaserInfo purchaserInfo)
    {
        DisplayPurchaserInfo(purchaserInfo);
    }

    private void DisplayPurchaserInfo(Purchases.PurchaserInfo purchaserInfo)
    {
        string text = "";
        foreach (KeyValuePair<string, DateTime> entry in purchaserInfo.AllExpirationDates)
        {
            string active = (DateTime.UtcNow < entry.Value) ? "subscribed" : "expired";
            text += entry.Key + " " + entry.Value + " " + active + "\n";
        }
        text += purchaserInfo.LatestExpirationDate;

        purchaserInfoLabel.text = text;
    }
}

Unity

Step by step guide for using RevenueCat in your Unity app.