Purchasing
The Purchasing module provides functionality for making in-game purchases, retrieving purchasable items, and tracking user transactions.
Overview
The Purchasing module allows you to:
- Make in-game purchases using user coins
- Retrieve all available purchasable items from your Pixidus Dashboard
- Get a list of items the user has already purchased
- Customize your in-game items from the Pixidus Dashboard without updating your game
Configure your in-game items in the Pixidus Dashboard to make them available for purchase in your game.
Classes
PixidusProductPreset
Represents a purchased item.
Unity
public class PixidusProductPreset
{
public string id; // Unique item identifier
public string name; // Item display name
public string description; // Item description
// Additional properties from your dashboard configuration
}PixidusPurchasableProductPreset
Represents an item available for purchase.
Unity
public class PixidusPurchasableProductPreset
{
public string id; // Unique item identifier
public string name; // Item display name
public string description; // Item description
public int price; // Price in coins
// Additional properties from your dashboard configuration
}Methods
Purchase
Make a purchase using the item ID. The callback returns true if the purchase was successful, false otherwise.
Unity
// Make a purchase
PixidusSDK.Purchasing.Purchase(itemId, (success) => {
if (success)
{
Debug.Log("Purchase successful!");
// Grant the item to the player
// Update UI, unlock content, etc.
}
else
{
Debug.Log("Purchase failed");
// Show error message to user
// Could be insufficient coins, network error, etc.
}
});GetAllPurchasableItems
Retrieves all items available for purchase from your Pixidus Dashboard.
Unity
// Get all purchasable items
PixidusSDK.Purchasing.GetAllPurchasableItems((items) => {
if (items != null && items.Count > 0)
{
foreach (var item in items)
{
Debug.Log($"Item: {item.name} - Price: {item.price} coins");
}
}
else
{
Debug.Log("No purchasable items available");
}
});GetPurchasedItems
Retrieves all items the current user has purchased.
Unity
// Get user's purchased items
PixidusSDK.Purchasing.GetPurchasedItems((items) => {
if (items != null && items.Count > 0)
{
foreach (var item in items)
{
Debug.Log($"Owned: {item.name}");
// Unlock content based on purchased items
}
}
else
{
Debug.Log("No items purchased yet");
}
});Example Usage
Here’s a complete example of implementing an in-game shop:
Unity
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections.Generic;
using Pixidus;
public class ShopManager : MonoBehaviour
{
public Transform shopItemContainer;
public GameObject shopItemPrefab;
public TMP_Text coinsText;
private List<PixidusPurchasableProductPreset> availableItems;
private HashSet<string> ownedItemIds = new HashSet<string>();
private void Start()
{
LoadShop();
}
public void LoadShop()
{
// First, get owned items
PixidusSDK.Purchasing.GetPurchasedItems((purchasedItems) => {
ownedItemIds.Clear();
foreach (var item in purchasedItems)
{
ownedItemIds.Add(item.id);
}
// Then, load all available items
PixidusSDK.Purchasing.GetAllPurchasableItems((items) => {
availableItems = items;
DisplayShopItems();
});
});
// Update coins display
PixidusSDK.User.GetUser((user) => {
if (user != null)
{
coinsText.text = user.coins.ToString();
}
});
}
private void DisplayShopItems()
{
// Clear existing items
foreach (Transform child in shopItemContainer)
{
Destroy(child.gameObject);
}
// Create shop item UI for each item
foreach (var item in availableItems)
{
var shopItem = Instantiate(shopItemPrefab, shopItemContainer);
var nameText = shopItem.transform.Find("Name").GetComponent<TMP_Text>();
var priceText = shopItem.transform.Find("Price").GetComponent<TMP_Text>();
var buyButton = shopItem.transform.Find("BuyButton").GetComponent<Button>();
nameText.text = item.name;
bool isOwned = ownedItemIds.Contains(item.id);
if (isOwned)
{
priceText.text = "Owned";
buyButton.interactable = false;
}
else
{
priceText.text = $"{item.price} coins";
string itemId = item.id;
buyButton.onClick.AddListener(() => PurchaseItem(itemId));
}
}
}
private void PurchaseItem(string itemId)
{
PixidusSDK.Purchasing.Purchase(itemId, (success) => {
if (success)
{
Debug.Log("Purchase successful!");
LoadShop(); // Refresh shop to update owned status
}
else
{
Debug.Log("Purchase failed - insufficient coins or error");
}
});
}
}Support
Need help? Our support team is here for you:
Last updated on