Skip to main content
Solved

Unity setup with several scenes

  • 16 November 2023
  • 3 replies
  • 116 views

Hello!

I’m integrating RevenueCat in a Unity app with several scenes.

In the Scene 1 I have created a game object that includes the reference to the “Purchases” script. In this scene with have all the logic to manage subscriptions.

This scene open another scenes where we don’t have a reference to “Purchases” script (it’s not needed).

The usual user workflow is Scene 1 → Scene 2 → Scene 1 → Scene 2,…

My questions is: Every time the first scene is loaded the “Purchases” script calls to Configure method but I have read that this is not a good approach. How are you guys doing this? Is it ok to call Configure method every time the user open the Scene 1?

 

Thank you!

 

3 replies

Userlevel 4
Badge +6

Hey @juan.riveros,

I’m definitely not an expert in Unity so I hope other developers can chime in to give you an idea of how they configure, but I can confirm that configuring multiple times is not a good idea and can lead to some unexpected behavior and crashes. 

It could be help to check out our sample unity app here: https://github.com/RevenueCat/purchases-unity/tree/main/Subtester

 

Badge +3

Hi Kaitlin,

Thanks for your response. I have checked the sample but it uses only one scene.

It seems like not many users are using Unity with RevenueCat… red flag? 🤔

 

Badge +3

Hello,

Finally solved! Thank you to the RevenueCat team for the support by email 👍

Writing here the solution used just in case anyone need it in the future:

  1. Create an empty first Scene (you don’t need to go back to this scene later) with one GameObject that includes:
    1. Purchases Script.
    2. A custom Singleton script with DontDestroyOnLoad.
using UnityEngine;
using UnityEngine.SceneManagement;

public class RevenueCatInit : Purchases.UpdatedCustomerInfoListener
{
public static RevenueCatInit Instance { get; private set; }

public Purchases purchases;
private bool purchasesConfigured = false;


void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}


void Start()
{
SceneManager.LoadScene("Start"); // Go directly to first scene
}


public void ConfigurePurchases(string userID)
{
if (!purchasesConfigured)
{
// Add HERE the call to purchases.Configure if you want (like me) configure RevenueCat once you have the usedID.


purchasesConfigured = true;
}

}


public override void CustomerInfoReceived(Purchases.CustomerInfo customerInfo)
{
// I didn't use it
}
}
  1. Then from any other scene you can get access to Purchases like this:
RevenueCatInit.Instance.ConfigurePurchases(userID);

RevenueCatInit.Instance.purchases.GetOfferings((offerings, error) =>
{...}

 

Ready to lauch!! 😎

Reply