Unity is one of the most popular game engines in existence today.
With over 50 percent of all new mobile games developed using Unity, and 2.4 billion unique devices running Unity games, it's obvious that this engine plays an important role in the global game market.
When looking for new game monetization strategies, selling merchandise is a common way to add revenue. In fact, some game developers already do this through online stores associated with their gaming site.
While this can be a straightforward purchase flow for PC games, for mobile games leaving the in-game experience to make a purchase in-browser, is not an optimal flow.
To help make in-game sales and purchases possible, we launched our Shopify SDK for Unity at Unite, Shopify’s partner and developer conference. This SDK allows game developers to embed shops that sell real-world items for real-world currencies, directly into their games.
Today, we’re going to walk you through a simple tutorial we’ve created that will show you the steps for integrating an existing Shopify Store into a game.
How to connect a Shopify store to a game
Since these steps are based on connecting an individual game to a single store, we’ll build a private app. This app will act as a means of communication between a Shopify store and the Unity game.
We'll start by setting up permissions for our private app to interact with our game's Shopify store. Then, we'll initialize the Shopify SDK for Unity, walk through adding a product to a cart, and generating a checkout.
Initializing a private app
Within your Shopify store account, click on
Apps, located at the bottom of the left-hand navigation bar, and then hit
Manage private apps in the main content panel.
From here, we'll be generating API credentials for the store. In the description field, name your app “unity-sdk-demo”. Then, enable
Custom Storefront by hitting the checkbox beside
Allow this app to access data from your storefront.
Now you'll see a generated
API key for your application, a
shared secret, and
store access token.
If you don't currently have any products in the test store, add one now by selecting
Product in the left-hand navigation bar. Access the product's information view, and notice the
Sales Channel panel in the top right-hand corner.
Sales Channel is an alternative way for customers to browse and buy merchandise. The Unity app is a sales channel because customers can buy items from a store without visiting the store’s webpage.
By connecting these channels to a Shopify account, you’re able to keep track of all products, orders, and customers through the Shopify Admin. The private app we’ve created will become a sales channel when it connects to the store.
Sales Channel panel allows you to manage which channels have access to displaying a product. If not already visible, click
Manage and the checkbox beside your new app to enable it.
It's important to note that a product will only be accessible to your private app (and therefore queryable through the Shopify SDK for Unity) if it’s marked as
visible in the
Manage Sales Channel Visibility modal we discussed above.
You might also like: How to Generate a Shopify API Token.
Initializing the Shopify SDK for Unity in your project
Now that we have everything setup on Shopify, let’s jump into Unity.
Create a new project and call it, "Unity SDK Demo".
Download the Shopify SDK for Unity from the Github link. Once
.unitypackage has been downloaded, simply open that file and press
You’re ready to roll with the SDK!
Create a new empty
Game Object by clicking
GameObject > Create Empty. This is the Game Object we’ll attach a script to, in order to test the Shopify SDK for Unity.
Assets > Create > C# Script and name it
Using your editor of choice, update the
UnityBuySDKDemo script to import the
Shopify.Unity package. In the
Start method, initialize the SDK using your store's access token and domain name (the
access token was displayed when setting up permissions for your private app and checking the box for enabling storefront access).
Querying a product
With the SDK initialized, we can retrieve products from our Shopify store.
In your same UnityBuySDKDemo file, update the
Start() method with the following:
Add your script as a component of the new GameObject by either dragging and dropping the script icon onto the
GameObject, or by selecting the
Add Component button in the GameObject's inspector.
Now when you press
Play above the scene view in Unity, you should see similar output in the console (depending on the number of products in the store, of course):
You might also like: Custom Storefronts — Building Commerce Anywhere.
Creating a cart, adding products, and creating a web checkout link
Finally, let's create a cart object, add a product to the cart, and display a Shopify checkout page.
Update your script, replacing the product query with the following:
When you press
Play you should see a log with your product's ID, and a browser window should open with a pre-populated cart:
In the above code sample, we first initialize a new
Cart object, and then retrieve the first variant of our first product.
A variant is an option applied to a product in your store. For example, you may stock a t-shirt as one of your products with the different size options of small, medium, and large. These sizes would be your product's variants.
Finally, we add this product variant to our cart object and generate a URL to display the cart to the user. Below is an example of what this set-up looks like.
You might also like: Building for Developer Success with Shopify’s Newest APIs.
More references to get you started
In addition to this tutorial, you can access more query examples using the Shopify SDK for Unity, by visiting the API Reference. For more information on the SDK in general, visit the Shopify SDK for Unity Help page.
What are your thoughts on the Shopify SDK for Unity? Let us know if the comments below!