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!
Best answer by juan.riveros
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:
Create an empty first Scene (you don’t need to go back to this scene later) with one GameObject that includes:
Purchases Script.
A custom Singleton script with DontDestroyOnLoad.
using UnityEngine;
using UnityEngine.SceneManagement;
publicclass RevenueCatInit : Purchases.UpdatedCustomerInfoListener
{
publicstatic RevenueCatInit Instance { get; privateset; }
public Purchases purchases;
privatebool 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
}
publicvoidConfigurePurchases(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;
}
}
publicoverridevoidCustomerInfoReceived(Purchases.CustomerInfo customerInfo)
{
// I didn't use it
}
}
Then from any other scene you can get access to Purchases like this:
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.
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:
Create an empty first Scene (you don’t need to go back to this scene later) with one GameObject that includes:
Purchases Script.
A custom Singleton script with DontDestroyOnLoad.
using UnityEngine;
using UnityEngine.SceneManagement;
publicclass RevenueCatInit : Purchases.UpdatedCustomerInfoListener
{
publicstatic RevenueCatInit Instance { get; privateset; }
public Purchases purchases;
privatebool 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
}
publicvoidConfigurePurchases(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;
}
}
publicoverridevoidCustomerInfoReceived(Purchases.CustomerInfo customerInfo)
{
// I didn't use it
}
}
Then from any other scene you can get access to Purchases like this:
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.