How can I integrate Unity IAP into my project?

Tram Ho

Note with Unity IAP 2d integration to facilitate integration when needed:

Step 1: Enable Service In App Purchasing

Step 2: Create Script Purchase includes the following functions:

  • InitializePurchasing: Initializes the IAP Builder, adds game products & adds the necessary callbacks to manage the purchase event.
  • BuyProductID: 1 private func to buy products based on the ID of the product that we declare (config by store)
  • BuyConsumable, BuyNonConsumable, BuySubscription: Public function to make purchases based on the type of product, passed in to get the exact ID of the product.
  • RestorePurchases: On iOS call this function to restore a previously purchased product.
  • OnInitialize: is called to check if the app can connect to the Unity IAP or not, OnInitialize is running in the background, only fails when the config fails.
  • OnInitializeFailed: Call when IAP init has problem and display the log in the console
  • ProcessPurchase: Check a successfully purchased product & show log in console.
  • OnPurchaseFailed: Showing an error when the transaction failed in the console.

Add the following script to the source folder:

Step 3:

Drag the Purchaser script to the gameObject that you need to test / implement IAP.

Creating test to buy IAP with 1 consumable item called 100 coins. Upon successful purchase, you will be added 100 coins.

We will call the Purchase script function to purchase the IAP. Add a Button to the IAP Panel & add an event for the button that points to a Purchase script function.

Step 4: Testing:

Running the game & activating the screen containing the Purchase script will display like this in the console log:

Click on the button that implements the purchase event of the consummer product, it will display:

DONE.

To test real on iOS / Android, you must configure on account of each store. See more on youtube videos to see how to configure.

Share the news now

Source : Viblo