Unity purchasing 4.1.2
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
- Add
IAPButton
to the scene and select It. - Click on the
IAP Catalog
button in the Inspector. - Add first product Id (It must be defined product on developer panel).
- Select product type(Consumable, Non-Consumable, or Subscription).
- Add alternative Product ID, Title, and Description.
- Repeat 3~6 steps until import all products.
- Active the
Automatically initialize
checkbox. - Close the
IAP Catalog
panel.
Step II: Add title, description, price, and handler for the button
- Click on the button and select
Product ID
in the Inspector. - Add Text for your button and attach it to the related position in Inspector.
- Add methods for
OnPurchaseComplete
andOnPurchaseFailed
handlers. - 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
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?
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