-
Couldn't load subscription status.
- Fork 20
Proper PaywallsBehaviour #706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9226899
Proper PaywallsBehaviour
vegaro 71cf8cb
Merge branch 'main' into proper-behaviour
vegaro 1bd9af5
Merge remote-tracking branch 'origin/main' into proper-behaviour
vegaro 4f8e021
better implementation of behavior
vegaro 60406d5
remove presentOnStart
vegaro e9189fe
add PaywallResultHandler
vegaro 5f1250a
add logs and make PresentPaywallIfNeeded private
vegaro 1a5b6fd
better docs
vegaro 5743661
add logs
vegaro 7c31085
capitalize Paywalls
vegaro adfe843
Merge branch 'main' into proper-behaviour
vegaro 627f78b
PaywallOptions.Offering adjustment
vegaro 2c0e358
OfferingSelection
vegaro efb0e09
fix compilation of PurchasesListener
vegaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,198 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using UnityEngine; | ||
| using UnityEngine.Events; | ||
|
|
||
| namespace RevenueCatUI | ||
| { | ||
| /// <summary> | ||
| /// MonoBehaviour helper that forwards to the static PaywallsPresenter API so paywalls can be driven from scenes. | ||
| /// MonoBehaviour component for presenting RevenueCat Paywalls from the Unity Editor. | ||
| /// Provides an alternative to PaywallsPresenter for developers who prefer configuring | ||
| /// Paywalls through Unity's Inspector interface. | ||
| /// </summary> | ||
| [AddComponentMenu("RevenueCat/Paywalls Behaviour")] | ||
| public class PaywallsBehaviour : MonoBehaviour | ||
| { | ||
| /// <summary> | ||
| /// Presents a paywall configured in the RevenueCat dashboard. | ||
| /// </summary> | ||
| /// <param name="options">Options for presenting the paywall.</param> | ||
| /// <returns>A <see cref="PaywallResult"/> describing the outcome.</returns> | ||
| public async Task<PaywallResult> PresentPaywall(PaywallOptions options = null) | ||
| [Header("Paywall Options")] | ||
| [Tooltip("The identifier of the offering to present. Leave empty to use the current offering.")] | ||
| [SerializeField] private string offeringIdentifier; | ||
|
|
||
| [Tooltip("Whether to display a close button on the paywall (only for original template RevenueCat Paywalls).")] | ||
| [SerializeField] private bool displayCloseButton = false; | ||
|
|
||
| [Header("Conditional Presentation")] | ||
| [Tooltip("If set, the paywall will only be presented if the user doesn't have this entitlement.")] | ||
| [SerializeField] private string requiredEntitlementIdentifier; | ||
|
|
||
| [Header("Events")] | ||
| [Tooltip("Invoked when the user completes a purchase and the paywall is dismissed.")] | ||
| public UnityEvent OnPurchased = new UnityEvent(); | ||
|
|
||
| [Tooltip("Invoked when the user restores purchases and the paywall is dismissed.")] | ||
| public UnityEvent OnRestored = new UnityEvent(); | ||
|
|
||
| [Tooltip("Invoked when the user cancels the paywall and the paywall is dismissed.")] | ||
| public UnityEvent OnCancelled = new UnityEvent(); | ||
|
|
||
| [Tooltip("Invoked when the paywall was not presented, for example when the user already has the required entitlement).")] | ||
| public UnityEvent OnNotPresented = new UnityEvent(); | ||
|
|
||
| [Tooltip("Invoked when an error occurs.")] | ||
| public UnityEvent OnError = new UnityEvent(); | ||
|
|
||
| private bool isPresenting = false; | ||
|
|
||
| public string OfferingIdentifier | ||
| { | ||
| get => offeringIdentifier; | ||
| set => offeringIdentifier = value; | ||
| } | ||
|
|
||
| public bool DisplayCloseButton | ||
| { | ||
| get => displayCloseButton; | ||
| set => displayCloseButton = value; | ||
| } | ||
|
|
||
| public string RequiredEntitlementIdentifier | ||
| { | ||
| return await PaywallsPresenter.Present(options); | ||
| get => requiredEntitlementIdentifier; | ||
| set => requiredEntitlementIdentifier = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Presents a paywall only if the user does not have the specified entitlement. | ||
| /// Presents the paywall with the configured options. | ||
| /// Can be called from Unity UI buttons or programmatically. | ||
| /// </summary> | ||
| /// <param name="requiredEntitlementIdentifier">Entitlement identifier to check before presenting.</param> | ||
| /// <param name="options">Options for presenting the paywall.</param> | ||
| /// <returns>A <see cref="PaywallResult"/> describing the outcome.</returns> | ||
| public async Task<PaywallResult> PresentPaywallIfNeeded(string requiredEntitlementIdentifier, PaywallOptions options = null) | ||
| public async void PresentPaywall() | ||
| { | ||
| return await PaywallsPresenter.PresentIfNeeded(requiredEntitlementIdentifier, options); | ||
| if (isPresenting) | ||
| { | ||
| Debug.LogWarning("[RevenueCatUI] Paywall is already being presented."); | ||
| return; | ||
| } | ||
|
|
||
| isPresenting = true; | ||
|
|
||
| try | ||
| { | ||
| var options = CreateOptions(); | ||
| PaywallResult result; | ||
|
|
||
| if (!string.IsNullOrEmpty(requiredEntitlementIdentifier)) | ||
| { | ||
| result = await PaywallsPresenter.PresentIfNeeded(requiredEntitlementIdentifier, options); | ||
| } | ||
| else | ||
| { | ||
| result = await PaywallsPresenter.Present(options); | ||
| } | ||
|
|
||
| HandleResult(result); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Debug.LogError($"[RevenueCatUI] Exception in PaywallsBehaviour: {e.Message}"); | ||
| HandleResult(PaywallResult.Error); | ||
| } | ||
| finally | ||
| { | ||
| isPresenting = false; | ||
| } | ||
| } | ||
|
|
||
| private async void PresentPaywallIfNeeded(string entitlementIdentifier) | ||
| { | ||
| if (string.IsNullOrEmpty(entitlementIdentifier)) | ||
| { | ||
| Debug.LogError("[RevenueCatUI] Entitlement identifier cannot be null or empty."); | ||
| HandleResult(PaywallResult.Error); | ||
| return; | ||
| } | ||
|
|
||
| if (isPresenting) | ||
| { | ||
| Debug.LogWarning("[RevenueCatUI] Paywall is already being presented."); | ||
| return; | ||
| } | ||
|
|
||
| isPresenting = true; | ||
|
|
||
| try | ||
| { | ||
| var options = CreateOptions(); | ||
| var result = await PaywallsPresenter.PresentIfNeeded(entitlementIdentifier, options); | ||
| HandleResult(result); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Debug.LogError($"[RevenueCatUI] Exception in PaywallsBehaviour: {e.Message}"); | ||
| HandleResult(PaywallResult.Error); | ||
| } | ||
| finally | ||
| { | ||
| isPresenting = false; | ||
| } | ||
| } | ||
|
|
||
| private PaywallOptions CreateOptions() | ||
| { | ||
| if (string.IsNullOrEmpty(offeringIdentifier)) | ||
| { | ||
| return new PaywallOptions(displayCloseButton); | ||
| } | ||
|
|
||
| return new PaywallOptions(offeringIdentifier, displayCloseButton); | ||
| } | ||
|
|
||
| private void HandleResult(PaywallResult result) | ||
| { | ||
| if (result == null) | ||
| { | ||
| Debug.Log("[RevenueCatUI] Received null PaywallResult."); | ||
| OnError?.Invoke(); | ||
| return; | ||
| } | ||
|
|
||
| switch (result.Result) | ||
| { | ||
| case PaywallResultType.Purchased: | ||
| if (OnPurchased.GetPersistentEventCount() == 0) | ||
| { | ||
| Debug.Log("[RevenueCatUI] Paywall purchase completed but OnPurchased event has no listeners."); | ||
| } | ||
| OnPurchased?.Invoke(); | ||
| break; | ||
| case PaywallResultType.Restored: | ||
| if (OnRestored.GetPersistentEventCount() == 0) | ||
| { | ||
| Debug.Log("[RevenueCatUI] Paywall restore completed but OnRestored event has no listeners."); | ||
| } | ||
| OnRestored?.Invoke(); | ||
| break; | ||
| case PaywallResultType.Cancelled: | ||
| if (OnCancelled.GetPersistentEventCount() == 0) | ||
| { | ||
| Debug.Log("[RevenueCatUI] Paywall cancelled but OnCancelled event has no listeners."); | ||
| } | ||
| OnCancelled?.Invoke(); | ||
| break; | ||
| case PaywallResultType.NotPresented: | ||
| if (OnNotPresented.GetPersistentEventCount() == 0) | ||
| { | ||
| Debug.Log("[RevenueCatUI] Paywall not presented but OnNotPresented event has no listeners."); | ||
| } | ||
| OnNotPresented?.Invoke(); | ||
| break; | ||
| case PaywallResultType.Error: | ||
| if (OnError.GetPersistentEventCount() == 0) | ||
| { | ||
| Debug.Log("[RevenueCatUI] Paywall error occurred but OnError event has no listeners."); | ||
| } | ||
| OnError?.Invoke(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These UnityEvents can accept generics, but won't work with a custom class like
PaywallResult, so I decided to split it into multiple events, that way they can be set in the editor. The alternative is to use a primitive type (string), but I think that limits us more if we ever want to send something in the events:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we ever need to send something like
UnityEvent<CustomerInfo>, it's still possible but devs need to hook to it via code (it doesn't show up in the Editor's dropdown):Or we could remove the parameter and inform developers to fetch the CustomerInfo themselves which I believe is the Unity standard since it works in the Editor: