Unity Virtual Goods

Introduction

This tutorial shows you how to buy and consume Virtual Goods when using Unity with the GameSparks platform. You'll also see how to get a list of what Virtual Goods are owned by the player.

Before following this tutorial, make sure you have checked out the previous tutorial Creating Virtual Goods. Nothing else is needed to carry out this tutorial. However, if you would like to see the project, you can download it:

Granting Currency

To begin with, you'll need to create a new Event with some Cloud Code that will grant the player some currency.

1. Navigate to Configurator > Events and click Add:

For this tutorial we've:

2. Click to Save and Close the new Event.

3. Navigate to Configurator > Cloud Code and under Scripts click Events.

4. Select the newly-created GRANT_CURRENCY Event. The Cloud Code editor opens for this Event and you can add the required Cloud Code:

This is the Cloud Code:


  var newCash = Spark.getData().CASH; // we can get the cash we intend to grant to the player
  Spark.getPlayer().credit1(newCash); //this will credit the player to currency 1


Setting up the Shop

If you have downloaded the sample project for this tutorial, you'll find that these interfaces are already set up in your project. If not, you'll need to create a really simple interface where you can buy and consume gold coins.

The shop set up works as follows.

Fetching Player Data

When the Shop is selected or transitioned, you need to call the AccountDetailsRequest in order to get the player’s account details from GameSparks:

One of the details in the Response is a list of Virtual Goods. You can get that data back using the Short Code for your gold coins and the GetNumber() method.

    new GameSparks.Api.Requests.AccountDetailsRequest().Send((response) => {
        if (!response.HasErrors) {
            Debug.Log("Account Details Found...");
            string playerName = response.DisplayName; // we can get the display name
            int cashAvailable = (int) response.Currency1;
            int goldCoinsAvailable = (int) response.VirtualGoods.GetNumber("GOLD_COIN");
        } else {
            Debug.Log("Error Retrieving Account Details...");
        }
    });

Awarding the Player some Virtual Goods

So, before you can buy Virtual Goods you need make sure your player has some currency to spend. In this case, the gold coins are going to cost one unit of currency1 each:

Here's what we've entered in the Test Harness JSON builder for the LogEventRequest and submitted by clicking to Send Request:

{
"@class": ".LogEventRequest",
"eventKey": "GRANT_CURRENCY",
"CASH": 100
}

Buying Virtual Goods

In order to buy Virtual Goods you need to call the BuyVirtualGoodsRequest. This request takes a few attributes:

    new GameSparks.Api.Requests.BuyVirtualGoodsRequest().SetCurrencyType(1).SetQuantity(1).SetShortCode("GOLD_COIN").Send((response) => {
        if (!response.HasErrors) {
            Debug.Log("Virtual Goods Bought Successfully...");
            UpdatePlayerDetails();
        } else {
            Debug.Log("Error Buying Virtual Goods...");
        }
    });

Consuming Virtual Goods

The next thing to do is give your player the ability to use their Virtual Goods and communicate with your GameSparks game to check that the Virtual Good has been consumed. To do this you need to call the ConsumeVirtualGoodRequest, which requires you to enter the Short Code and quantity of the Virtual Good:

    new GameSparks.Api.Requests.ConsumeVirtualGoodRequest().SetQuantity(1).SetShortCode("GOLD_COIN").Send((response) => {
        if (!response.HasErrors) {
            Debug.Log("Virtual Goods Consumed Successfully...");
            UpdatePlayerDetails();
        } else {
            Debug.Log("Error Consuming Virtual Goods...");
        }
    });

Testing Your Shop

You'll now have the ability to buy and consume Virtual Goods, as well as grant your players some currency and retrieve and update the account details each time an action is taken. If you are working from the GameSparks sample project for this tutorial, you'll see the following screen.

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here