Hello Readers !!
Today we are going to discuss the IAP in the Unity3D.
We can do IAP by number of plugins. There are variety of plugins available in the market which we can use for the IAP. But almost all of the plugins are paid.
Here are the links for the plugins:-
https://www.assetstore.unity3d.com/en/#!/content/59353
https://www.assetstore.unity3d.com/en/#!/content/12343
https://www.assetstore.unity3d.com/en/#!/content/28679
But all these plugins have limitations. We can use these plugins only for some platforms. If we want to create IAP for all the platforms which Unity provides then we need to do something different from this.
For this only, Unity provides a new feature that is called Unity IAP, which comes under the Unity services.
Adding the unity services in the code is very simple and straight forward. You just need to click on the "On" button under services section.
This will import all the required plugins in your game. After that, you need to write down a small code.
Hello Readers !!
Today we are going to discuss the IAP in the Unity3D.
We can do IAP by number of plugins. There are variety of plugins available in the market which we can use for the IAP. But almost all of the plugins are paid.
Here are the links for the plugins:-
https://www.assetstore.unity3d.com/en/#!/content/59353
https://www.assetstore.unity3d.com/en/#!/content/12343
https://www.assetstore.unity3d.com/en/#!/content/28679
But all these plugins have limitations. We can use these plugins only for some platforms. If we want to create IAP for all the platforms which Unity provides then we need to do something different from this.
For this only, Unity provides a new feature that is called Unity IAP, which comes under the Unity services.
Adding the unity services in the code is very simple and straight forward. You just need to click on the "On" button under services section.
This will import all the required plugins in your game. After that, you need to write down a small code.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
public class Purchaser : MonoBehaviour, IStoreListener
{
private static IStoreController m_StoreController; // The Unity Purchasing system.
private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
public static string kProductIDConsumable = "consumable";
public static string kProductIDNonConsumable = "nonconsumable";
public static string kProductIDSubscription = "subscription";
void Start()
{
if (m_StoreController == null)
{
// Begin to configure our connection to Purchasing
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized())
{
return;
}
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(kProductIDConsumable, ProductType.Consumable);
builder.AddProduct(kProductIDNonConsumable, ProductType.NonConsumable);
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void BuyConsumable()
{
BuyProductID(kProductIDConsumable);
}
public void BuyNonConsumable()
{
BuyProductID(kProductIDNonConsumable);
}
public void BuySubscription()
{
BuyProductID(kProductIDSubscription);
}
void BuyProductID(string productId)
{
if (IsInitialized())
{
Product product = m_StoreController.products.WithID(productId);
if (product != null && product.availableToPurchase)
{
m_StoreController.InitiatePurchase(product);
}
else
{
Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
}
}
else
{
Debug.Log("BuyProductID FAIL. Not initialized.");
}
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
m_StoreController = controller;
m_StoreExtensionProvider = extensions;
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
if (String.Equals(args.purchasedProduct.definition.id, kProductIDConsumable, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
}
else if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
}
else if (String.Equals(args.purchasedProduct.definition.id, kProductIDSubscription, StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
}
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
}
}
You need to add your own Product ID, Rest of the code will be same for you.
Try this and have fun.
Till then keep coding.
0 Comment(s)