diff --git a/RevenueCat/Scripts/Purchases.cs b/RevenueCat/Scripts/Purchases.cs index de14f56d..39104bba 100644 --- a/RevenueCat/Scripts/Purchases.cs +++ b/RevenueCat/Scripts/Purchases.cs @@ -58,7 +58,26 @@ public partial class Purchases : MonoBehaviour [Tooltip("A subclass of Purchases.UpdatedCustomerInfoListener component.\n" + "Use your custom subclass to define how to handle updated customer information.")] - public UpdatedCustomerInfoListener listener; + [SerializeField] + private UpdatedCustomerInfoListener listener; + + // Cache custom listener, if not from class. + private IUpdatedCustomerInfoListener customListener; + public IUpdatedCustomerInfoListener Listener + { + get => listener != null ? listener : customListener; + set + { + if (value is UpdatedCustomerInfoListener listenerValue) + { + this.listener = listenerValue; + } + else + { + customListener = value; + } + } + } [Tooltip("An optional boolean. Set this to true if you have your own IAP implementation " + "and want to use only RevenueCat's backend.\nDefault is false.\n" + @@ -111,22 +130,32 @@ public partial class Purchases : MonoBehaviour private void Start() { -#if UNITY_ANDROID && !UNITY_EDITOR + Prepare(); + + if (useRuntimeSetup) return; + + Configure(string.IsNullOrEmpty(appUserID) ? null : appUserID); + GetProducts(productIdentifiers, null); + } + + private void Prepare() + { + if (_wrapper != null) + { + return; + } + #if UNITY_ANDROID && !UNITY_EDITOR _wrapper = new PurchasesWrapperAndroid(); -#elif UNITY_IPHONE && !UNITY_EDITOR + #elif UNITY_IPHONE && !UNITY_EDITOR _wrapper = new PurchasesWrapperiOS(); -#else + #else _wrapper = new PurchasesWrapperNoop(); -#endif + #endif + if (!string.IsNullOrEmpty(proxyURL)) { _wrapper.SetProxyURL(proxyURL); } - - if (useRuntimeSetup) return; - - Configure(string.IsNullOrEmpty(appUserID) ? null : appUserID); - GetProducts(productIdentifiers, null); } private void Configure(string newUserId) @@ -191,6 +220,9 @@ public void Setup(PurchasesConfiguration purchasesConfiguration) /// public void Configure(PurchasesConfiguration purchasesConfiguration) { + // Ensure wrapper is inited. + Prepare(); + var dangerousSettings = purchasesConfiguration.DangerousSettings.Serialize().ToString(); _wrapper.Setup(gameObject.name, purchasesConfiguration.ApiKey, purchasesConfiguration.AppUserId, purchasesConfiguration.ObserverMode, purchasesConfiguration.UsesStoreKit2IfAvailable, purchasesConfiguration.UserDefaultsSuiteName, @@ -1240,18 +1272,18 @@ private void _receiveCustomerInfo(string customerInfoJson) { Debug.Log("_receiveCustomerInfo " + customerInfoJson); - if (listener == null) return; + if (Listener == null) return; var response = JSON.Parse(customerInfoJson); if (response["customerInfo"] == null) return; var info = new CustomerInfo(response["customerInfo"]); - listener.CustomerInfoReceived(info); + Listener.CustomerInfoReceived(info); } // ReSharper disable once UnusedMember.Local private void _handleLog(string logDetailsJson) { - if (listener == null) return; + if (Listener == null) return; var response = JSON.Parse(logDetailsJson); var logLevelInResponse = response["logLevel"]; diff --git a/RevenueCat/Scripts/UpdatedCustomerInfoListener.cs b/RevenueCat/Scripts/UpdatedCustomerInfoListener.cs index 98c4fe13..fc05dabf 100644 --- a/RevenueCat/Scripts/UpdatedCustomerInfoListener.cs +++ b/RevenueCat/Scripts/UpdatedCustomerInfoListener.cs @@ -2,8 +2,13 @@ public partial class Purchases { - public abstract class UpdatedCustomerInfoListener : MonoBehaviour + public interface IUpdatedCustomerInfoListener + { + public void CustomerInfoReceived(CustomerInfo customerInfo); + } + + public abstract class UpdatedCustomerInfoListener : MonoBehaviour, IUpdatedCustomerInfoListener { public abstract void CustomerInfoReceived(CustomerInfo customerInfo); } -} \ No newline at end of file +}