RevenueCat

RevenueCat Documentation

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

Documentation    Support

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 framework to your iOS application

You must integrate Purchases.framework into your iOS application. You can do this either via CocoaPods, Carthage, or by downloading the framework manually.

2. Add PurchasesUnityHelper.m to your iOS project.

The PurchasesUnityHelper.m file connects the Purchases.framework with Unity. You will need to add it to your iOS project. You can do this by adding it your iOS project directly via Xcode or by including it in the Assets/Plugins/iOS folder. Get the latest version here.

3. Add the Purchases Unity package

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

4. 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.

5. 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.