docs.unity3d.com
    Show / Hide Table of Contents

    IAP Listeners

    Codeless IAP dispatches successful and failed purchase events to an active IAP Button component in the hierarchy. However, there may be times when it is difficult or undesirable to have an active IAP Button when handling a successful purchase. For example, if a purchase is interrupted before completing, Unity IAP attempts to process the purchase again the next time it is initialized. You may want this to happen immediately after the app launches, in which case an IAP Button wouldn't make sense. Codeless IAP includes the IAP Listener component precisely for these cases. An active IAP Listener in the Scene hierarchy receives any purchase events that cannot be dispatched to an IAP Button.

    To add an IAP Listener:

    1. In the Unity Editor, select Services > In-App Purchasing > Create IAP Listener.
    2. Follow the steps for writing a purchase fulfillment script as a GameObject component.
    3. Select the IAP Listener in the Scene and locate its IAP Listener (Script) component in the Inspector, then click the plus (+) button to add a function to the On Purchase Complete (Product) list.
    4. Drag the GameObject with the purchase fulfillment script onto the event field in the component’s Inspector, then select your function from the dropdown menu. Configuring an __IAP Listener__ to handle processing exceptions

    Purchase fulfillment

    When your catalog contains at least one Product, you can define IAP Button behavior when the purchase completes or fails.

    1. Select your IAP Button in the Scene view, then locate its IAP Button (Script) component in the Inspector.
    2. Select the Product to link to the IAP Button from the Product ID drop-down list. Selecting a Product to associate with a Codeless **IAP Button**
    3. Create your own function that provides purchase fulfillment, or import an Asset that does this (see code sample, below).
    4. Apply your purchase fulfilment script to a GameObject as a component. Creating a GameObject with a purchase fulfillment script
    5. Return to the IAP Button (Script) component in the Inspector, and click the plus (+) button to add a function to the On Purchase Complete (Product) list.
    6. Drag the GameObject with the purchase fulfillment script onto the On Purchase Complete (Product) event field (illustrated below), then select your function from the dropdown menu. Assigning your purchase fulfillment script to an **IAP Button** event field

    Fulfillment script code sample:

    public void GrantCredits (int credits){
        userCredits = userCredits + credits;
        Debug.Log(“You received “ + credits “ Credits!”);
    }
    

    Run your game to test the IAP Button.

    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023