Print


With Unity purchasing, setting up in-app purchases for your game across multiple app stores has never been easier.

One common API to access all stores for free so you can fully understand and optimize your in-game economy.
• Automatic coupling with Unity Analytics to enable monitoring and decision-making based on trends in your revenue and purchase data across multiple platforms.
• Support for iOS, Mac, tvOS, Bazaar, Google Play, Windows, and Amazon app stores.
• Support to work with the Unity Distribution Portal to synchronize catalogs and transactions with other app stores.
• Client-side receipt validation for Apple App Store and Google Play.

‌‌

Part 1 - Import package:

Disable Official Unity Purchasing from project settings in Unity and Import the latest unity package from here.
Keep in your mind all the below examples exist in the unity package.

پرداخت درون برنامه ای

Part 2 - Implementation:

Unity purchasing provides two ways to implementation :
First Way (Codeless)
Second Way (Through-code)

First Way (Codeless Implementation) :

Step I: Add IAPButton and define your products
IAP catalog

  1. Add IAPButton to the scene and select It.
  2. Click on the IAP Catalog button in the Inspector.
  3. Add first product Id (It must be defined product on developer panel).
  4. Select product type(Consumable, Non-Consumable, or Subscription).
  5. Add alternative Product ID, Title, and Description.
  6. Repeat 3~6 steps until import all products.
  7. Active the Automatically initialize checkbox.
  8. Close the IAP Catalog panel.
    ‌‌

    Step II: Add title, description, price, and handler for the button
butttons
  1. Click on the button and select Product ID in the Inspector.
  2. Add Text for your button and attach it to the related position in Inspector.
  3. Add methods for OnPurchaseComplete and OnPurchaseFailed handlers.
  4. Repeat the above steps for all product items.

Second Way (Through-code Implementation) :

Step I (Create the Payment class) :

Create custom payment class implemented from IStoreListener interfase.

public class ThroughCodeShop : MonoBehaviour, IStoreListener
{
  void OnInitialized(IStoreController controller, IExtensionProvider extensions);
  void OnInitializeFailed(InitializationFailureReason error);
  PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent);
  void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason);
}

Step II (Define your products) :

var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
//Your products IDs. They should match the ids of your products in your store.
//Add products that will be purchasable and indicate its type.
builder.AddProduct("gas", ProductType.Consumable);
builder.AddProduct("premium", ProductType.NonConsumable);
builder.AddProduct("infinite_gas_monthly", ProductType.Subscription);

UnityPurchasing.Initialize(this, builder);

After product definition, you will see initial result in the one of the OnInitialized  or OnInitializeFailed  methods. You can instantiate your buttons in OnInitialized method:

public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
  Debug.Log("In-App Purchasing successfully initialized");
  m_StoreController = controller;
  // Create shop items
  foreach (var product in controller.products.all)
  {
    Instantiate<ShopItem>(shopItemTemplate, transform).Init(product);
  }
}

public void OnInitializeFailed(InitializationFailureReason error)
{
  Debug.Log($"In-App Purchasing initialize failed: {error}");
}

Step III (Purchase Flow):

Call  m_StoreController.InitiatePurchase(productId) method to start purchase process:

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
  //Retrieve the purchased product
  var product = args.purchasedProduct;
  Debug.Log($"Purchase Complete - Product: {product.definition.id}");

  //We return Complete, informing IAP that the processing on our side is done and   the transaction can be closed.
  return PurchaseProcessingResult.Complete;
}

public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
  Debug.Log($"Purchase failed - Product: '{product.definition.id}', PurchaseFailureReason: {failureReason}");
}

Part 3 - Define PackageName

Edit -> Project Settings -> Player -> Other Settings -> Override Default Package Name


override packagename

Part 4 - Define Store

In `Resources` directory, open `BillingMode` file an Update `androidStore` value from `Google` to `Cafebazaar`

{"androidStore":"Cafebazaar"}

Keep in your mind, for Unity 2019 and older you must remove the `queries` tag from `AndroidManifest.xml` file in this library:

Bazaar\Purchasing\Plugins\UnityPurchasing\Android\bazaar-purchasing.aar


بیشتر بخوانید:

Was this content helpful?
Write your comment here...

If you have any comment on this content or any idea to make it better, use this form to send us your comment.

Publish Date: 1400-10-28 14:01

Latest Update: 1401-02-04 19:30