« SDKBOX Home

IAP on Unity

How to integrate IAP Plugin to Unity project


Creator New Unity Project

Import IAP Plugin

open assert store

Integration

Create Button

create a Buy button, like follow:

add IAP prefab to scene

Create IAPScript Script

using UnityEngine;
using System.Collections;

public class IAPScript : MonoBehaviour {

    private Sdkbox.IAP _iap;

    // Use this for initialization
    void Start () {
        _iap = FindObjectOfType<Sdkbox.IAP>();
        if (_iap == null) {
            Debug.Log("Failed to find IAP instance");
        }
    }

    // Update is called once per frame
    void Update () {
    }

    public void Purchase(string item) {
        if (_iap != null) {
            Debug.Log("About to purchase " + item);
            _iap.purchase(item);
        }
    }

    /*
     * Event Handlers
     */
    public void onInitialized(bool status) {
        Debug.Log("IAPScript.onInitialized " + status);
    }

    public void onSuccess(Sdkbox.Product product) {
        Debug.Log("IAPScript.onSuccess: " + product.name);
    }

    public void onFailure(Sdkbox.Product product, string message) {
        Debug.Log("IAPScript.onFailure " + message);
    }

    public void onCanceled(Sdkbox.Product product) {
        Debug.Log("IAPScript.onCanceled product: " + product.name);
    }

    public void onRestored(Sdkbox.Product product) {
        Debug.Log("IAPScript.onRestored: " + product.name);
    }

    public void onProductRequestSuccess(Sdkbox.Product[] products) {
        foreach (var p in products) {
            Debug.Log("Product: " + p.name + " price: " + p.price);
        }
    }
    public void onProductRequestFailure(string message) {
        Debug.Log("IAPScript.onProductRequestFailure: " + message);
    }
    public void onRestoreComplete(string message) {
        Debug.Log("IAPScript.onRestoreComplete: " + message);
    }

}

Note: this script just handler iap event and purchase, you need handler refresh getProducts, see PurchaseHandler in IAP Sample Scene.

Configure IAP

Configure Buy Button

Note: argment of Purchase is the name your iOS/Android product

Run Player

run player and click button, you will get follow log:

Note: the operation of the purchase will not trigger on player, this just make sure script and other setting is fine.

Configure package name

open player setting, Menu -> Edit -> Project Settings -> Player, change ios/android bundle identifier to yours

Build iOS

Build Android

API Reference

SDKBox IAP For Unity, you can get all api from IAP.cs

Methods

public void setDebug(bool debug)
public void getProducts() //will trigger listener event 'onProductRequestSuccess'
public void purchase(string name)
public void refresh()
public void restore()

Callback

public void onInitialized(bool status)
public void onSuccess(Sdkbox.Product product)
public void onFailure(Sdkbox.Product product, string message)
public void onCanceled(Sdkbox.Product product)
public void onRestored(Sdkbox.Product product)
public void onProductRequestSuccess(Sdkbox.Product[] products)
public void onProductRequestFailure(string message)
public void onRestoreComplete(bool b, string message)

IAP Sample

SDKBox IAP plugin include a sample scene, open it and try.