Hello Kin

In the last tutorial, we learned how to set up the developer environment to get ready to use the Kin Unity SDK. In this tutorial, we will set up and use a wrapper to start interacting with Kin’s blockchain.

Understanding the SDK

Currencies have been in games for decades. They let your players

Earn (Receive currency) Spend (Send currency)

At the heart of it all, this is what your user will be doing in your app. The difference between a regular in-app currency and Kin is that Kin is a currency with actual value. The SDK will connect your app to Kin’s blockchain and handle all the complicated business in the middle, so all you have to think about is:

Sending Kin Receiving Kin

The SDK has a client that handles connections to the blockchain depending on whether the user is on an Android or iOS (hence the word client).

The account handles your user’s personal account on the blockchain, just like a bank account with an account number (public key) and signature (private key).

Sending a payment

To send a payment to the blockchain, the account:

Builds the transaction on the client Signs the transaction Sends it to the blockchain Waits for a success confirmation.

You can find details on how this process works here. For now, we will use a simpler method to connect your app to Kin’s blockchain.

Make it easier!

To make it easier for your app to handle payments, we will first install a wrapper. This way, you will be able to use the SDK with simple, one-line commands.

Setting up the wrapper

Step 1: Open your Unity project and create a folder under Assets called Scripts, and right-click Scripts to create a C# file called KinWrapper.

Step 2: Double click this file to open it in your editor, paste this wrapper code, and save the the file.

Step 3: Create an empty object in your hierarchy window, name it KinWrapper, and drag the C# script into it (or use Add Component).

From now on, any time your code runs, your wrapper will self initialize and start listening to Kin’s blockchain. Wow!

Hello? Kin

Our first exercise is to listen to the chatter between your app and Kin’s blockchain.

Step 1: Right click on your hierarchy window and add a UI item > Panel.

This will add a panel and a canvas onto your scene. To make things easier, switch your scene to 2D mode.

Step 2: Create another file under Scripts and call it tutorial.

Drag this file onto the Canvas object, so it will also be instantiated as soon as your app starts. Open up the tutorial, and add the following lines to the start function:

private KinWrapper kinWrapper; void Start() { kinWrapper = GameObject.Find("KinWrapper").GetComponent<KinWrapper>(); kinWrapper.Initialize(ListenKin); }

The code above simply finds the wrapper we created, and then tells it to send any relevant chatter from Kin’s blockchain to our function, ListenKin. So now we need to add this function:

Add the following code to the same file:

void ListenKin(object eventData, string type) { GameObject.Find("TutorialLog").GetComponent<Text>().text += "

" + eventData.ToString(); }

So now, every time we run our app, ListenKin will receive all the info that is relevant to us. Let us create a simple control to display this information.

Step 3: Create a UI > Text object under your Panel, and name it TutorialLog

Size it in your Canvas so it’s nice and big, and we might as well give our Panel a white background so we can easily read the text. It should look something like this when you’re done. You can also remove New Text so it’s nice and neat.

Now, we can tell Unity to post any event data from the SDK into our Text object.

void ListenKin(object eventData, string type) { GameObject.Find("TutorialLog").GetComponent<Text>().text += "

" + eventData.ToString(); }

Compile and Listen

The SDK must run on an Android device or emulator for it to work, and not on the Unity Editor. To test it, connect your Android phone to your computer via USB and go to build settings, select your device, and click Build and Run.

Unity will ask you for a location to save your apk file — You can create a folder called Builds and save it there. You will also need to name your package under Player Settings, but Unity will prompt you to do this.

Once Unity is done building (a couple of minutes), open the app on your device to see the results. Alternatively to using a physical device, you can also follow this tutorial to use a virtual one. However, we should be able to test faster with a physical one.

Once compiled, open up your device (make sure you are online) to see the result:

Display from your Android device

Wow! Your phone just said hello and gave you an address. What just happened?

We will cover this in the next tutorial: What did my phone just say?