Respond to button actions in Unity SDK

If you are building paywalls using the Adapty paywall builder, itโ€™s crucial to set up buttons properly:

  1. Add a button in the paywall builder and assign it either a pre-existing action or create a custom action ID.
  2. Write code in your app to handle each action youโ€™ve assigned.

This guide shows how to handle custom and pre-existing actions in your code.

Only purchases and restorations are handled automatically. All the other button actions, such as closing paywalls or opening links, require implementing proper responses in the app code.

Close paywalls

To add a button that will close your paywall:

  1. In the paywall builder, add a button and assign it the Close action.
  2. In your app code, implement a handler for the close action that dismisses the paywall.
public void PaywallViewDidPerformAction(
    AdaptyUIPaywallView view, 
    AdaptyUIUserAction action
) {
    switch (action.Type) {
        case AdaptyUIUserActionType.Close:
            view.Dismiss(null);
            break;
        default:
            // handle other events
            break;
    }
}

Open URLs from paywalls

If you want to add a group of links (e.g., terms of use and purchase restoration), add a Link element in the paywall builder and handle it the same way as buttons with the Open URL action.

To add a button that opens a link from your paywall (e.g., Terms of use or Privacy policy):

  1. In the paywall builder, add a button, assign it the Open URL action, and enter the URL you want to open.
  2. In your app code, implement a handler for the openUrl action that opens the received URL in a browser.
public void PaywallViewDidPerformAction(
    AdaptyUIPaywallView view,
    AdaptyUIUserAction action
) {
    switch (action.Type) {
        case AdaptyUIUserActionType.OpenUrl:
            var urlString = action.Value;
            if(!string.IsNullOrWhiteSpace(urlString)) {
                Application.OpenURL(urlString);
            }
            break;
        default:
            // handle other events
            break;
    }
}

Log into the app

To add a button that logs users into your app:

  1. In the paywall builder, add a button and assign it the Custom action with ID login.
  2. In your app code, implement a handler for the login custom action that identifies your user.
public void PaywallViewDidPerformAction(
    AdaptyUIPaywallView view,
    AdaptyUIUserAction action
) {
    switch (action.Type) {
        case AdaptyUIUserActionType.Custom:
            if (action.Value == "login") {
                // Navigate to login scene
                SceneManager.LoadScene("LoginScene");
            }
            break;
        default:
            // handle other events
            break;
    }
}

Handle custom actions

To add a button that handles any other actions:

  1. In the paywall builder, add a button, assign it the Custom action, and assign it an ID.
  2. In your app code, implement a handler for the action ID youโ€™ve created.

For example, if you have another set of subscription offers or one-time purchases, you can add a button that will display another paywall:

public void PaywallViewDidPerformAction(
    AdaptyUIPaywallView view,
    AdaptyUIUserAction action
) {
    switch (action.Type) {
        case AdaptyUIUserActionType.Custom:
            if (action.Value == "openNewPaywall") {
                // Display another paywall
                ShowAlternativePaywall();
            }
            break;
        default:
            // handle other events
            break;
    }
}

private void ShowAlternativePaywall() {
    // Implement your logic to show alternative paywall
}