Version: 5.5 (switch to 5.6b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Product

class in UnityEngine.Purchasing

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Represents a product that may be purchased as an In-App Purchase.

Here is an example of a sample Store class working with a Product referencs.

#pragma strict
public class MyStoreClass extends MonoBehaviour {
	static var kProductID100Currency: String = "virtualcurrency_100";
	var m_StoreController: IStoreController;
	function Awake() {
		var builder: ConfigurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
		builder.AddProduct(kProductID100Currency, ProductType.Consumable);
		UnityPurchasing.Initialize(this, builder);
	}
	public function PurchaseCurrency() {
		if (m_StoreController != null) {
			// Fetch the currency Product reference from Unity Purchasing
			var product: Product = m_StoreController.products.WithID(kProductID100Currency);
			if (product != null && product.availableToPurchase) {
				m_StoreController.InitiatePurchase(product);
			}
		}
	}
	public function OnInitialized(controller: IStoreController, extensions: IExtensionProvider) {
		m_StoreController = controller;
	}
	public function OnInitializeFailed(error: InitializationFailureReason) {
	}
	public function ProcessPurchase(e: PurchaseEventArgs) {
		if (String.Equals(e.purchasedProduct.definition.id, kProductID100Currency, StringComparison.Ordinal)) {
			Debug.Log("Purchased 100 coins");
		}
		return PurchaseProcessingResult.Complete;
	}
	public function OnPurchaseFailed(item: Product, r: PurchaseFailureReason) {
	}
}
using System;
using UnityEngine;
using UnityEngine.Purchasing;

public class MyStoreClass : MonoBehaviour, IStoreListener { static string kProductID100Currency = "virtualcurrency_100"; IStoreController m_StoreController; void Awake() { ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance()); builder.AddProduct(kProductID100Currency, ProductType.Consumable); UnityPurchasing.Initialize(this,builder); } public void PurchaseCurrency() { if (m_StoreController != null) { // Fetch the currency Product reference from Unity Purchasing Product product = m_StoreController.products.WithID(kProductID100Currency); if (product != null && product.availableToPurchase) { m_StoreController.InitiatePurchase(product); } } } public void OnInitialized(IStoreController controller, IExtensionProvider extensions) { m_StoreController = controller; } public void OnInitializeFailed(InitializationFailureReason error) {} public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e) { if (String.Equals(e.purchasedProduct.definition.id, kProductID100Currency, StringComparison.Ordinal)) { Debug.Log("Purchased 100 coins"); } return PurchaseProcessingResult.Complete; } public void OnPurchaseFailed(Product item, PurchaseFailureReason r) {} }

Variables

availableToPurchaseDetermine if this product is available to purchase according to the store subsystem.
definitionFundamental immutable product properties.
hasReceiptOwned Non Consumables and Subscriptions should always have receipts.
metadataLocalized metadata provided by the store system.
receiptThe purchase receipt for this product, if owned. Otherwise null.
transactionIDA unique identifier for this product's transaction, if available. Otherwise null.

Public Functions

EqualsEquality defined for use in collections.
GetHashCodeGetHashCode defined for use in collections.