RESOURCES: You can find the source for this project on my GitHub.

This is a cross post from my personal blog, which you can find here:

In part 1 of my tutorial, I showed you how you how to set up the initial project with prime[31]. Now that we have it built, I'm going to walk you through the code, as well as how it all works.

Now that we have the project built, let's open it up the metro folder, and launch the Prime31 Visual Studio solution.

Your folder structure should look like this


Launching from Visual Studio

Something that threw me in a loop initially, was the fact that the project wants to deploy to an ARM device immediately. If you hit debug "Local Machine" it will throw an error about your machine not being an ARM tablet. The solution:

Arm-Debug-1Go to Configuration Manager and change the Active Solution Platform to X86.


You can now run your projects and deploy them via Visual Studio. Do that, and you will be greeted with this screen:

Unity_Prime31_FirstBuiltPerfect! We're up and running!

NOTE: On occasion, I'll get an error, as seen in the text below. I'm not sure of what causes this, but when I switch my deployment from whatever it is currently on (for example, Debug) to Release or Master, it suddenly builds fine. I can then go back to Debug, and use that if I'd like.

Error 1 The command "echo UnityInstallationDir 'C:\Program Files (x86)\Unity\Editor'
echo UnityProjectDir 'C:\Users\DaveVoyles\Desktop\Adam\Prime31'
echo Copying assemblies...
copy /Y "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\Unprocessed\*" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\"
copy /Y "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Unprocessed\Assembly-CSharp.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Assembly-CSharp.dll"
copy /Y "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Unprocessed\Assembly-CSharp-firstpass.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Assembly-CSharp-firstpass.dll"
if exist "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Unprocessed\Assembly-CSharp.pdb" copy /Y "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Unprocessed\Assembly-CSharp.pdb" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Assembly-CSharp.pdb"
if exist "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Unprocessed\Assembly-CSharp-firstpass.pdb" copy /Y "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Unprocessed\Assembly-CSharp-firstpass.pdb" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Assembly-CSharp-firstpass.pdb"
echo Running AssemblyConverter...
"C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\MetroSupport\Tools\AssemblyConverter.exe" -platform=wsa81 "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Assembly-CSharp.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\bin\Store 8.1\x86\Master\Assembly-CSharp-firstpass.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\\P31MetroAzure.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\\P31MetroHelpers.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\\P31RestKit.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\\UnityEngine.dll" "C:\Users\DaveVoyles\Desktop\Adam\Prime31\Builds\Win81\Prime31\\WinRTLegacy.dll"
echo AssemblyConverter done.
" exited with code 1. Prime31


Open the MetroAzureDemoUi.cs file, and take a look at the sample I put together.


It may look overwhelming at first, but I've commented everything in there. In terms of variables, I've added a _leaderboardItem, which is exactly what it sounds like -- a container for the things you insert into your leaderboard. It simply holds a name, score, and unique ID for each object you insert into the board.

Following that, we have the list itself, which is just a collection of _leaderBoardItem(s).

The _minScoreToReturn is used during our query below, where we will ONLY want to return leaderboard items with a score which is less than or equal to (x). In this case, I've set it to 200, so we will ONLY return objects which have 200 points or less.

Endpoint and Application Key

You'll also notice:

 private string _azureEndPoint = "<your leaderboard service>";
 private string _applicationKey = "<your application key>";

You COULD hard code your end point and app key here, but I chose to just leave this string in. Instead, I've added the end point and app key within the Unity Editor.

If you write text in the editor on the right hand side, it should propagate the empty fields inside of the gameplay screen.

I've chosen to hardcode all of the buttons in the scene, to avoid having to use the heirarchy. I wanted everything to be seen within the code itself, for simplicity's sake.

Look at the endpoint as an address, or door that you need to reach. That's where we are going to store our leaderboard information. The application key is exactly that; a key to the door, to gain access to the leaderboard. We have a key in place, to prevent anyone from coming in and manipulating our leaderboard.

Moreover, you can set permissions to your Azure Mobile Service, which would only allow individuals who have authenticated with a service (Facebook, Azure Active Directory, etc.) to gain access to the leaderboard. For the sake of brevity, I haven't done that in this tutorial.


Beneath that, the OnGui() function is where all of our drawing occurs, for the buttons, text, and input fields.


I did it this way because I wanted to break the GUI functions into smaller functions so that they were easier to digest, and wanted to break them into columns on the screen so that they were easier to read. Take some time to read what each of the buttons do.It may seem overwhelming at first, but I broke them down into a logical order, and they are all laid out in the order that they appear on screen. I've still got some sorting to do, but hey, it works for now.

Insert and Delete

The two key functions you'll likely use more than any other in this sample are the Insert and Delete ones. Insert allows you to pass in a new _leaderBoardItem, which will contain a UserName and Score. Don't worry about the ID, that gets generated for you automatically.

NOTE: All of the functions in this sample require you to be connected to Azure before you do anything. You must hit the "Connect Azure Service" before anything can take place.

Let's test this out by running the project from Visual Studio and inserting a new object into our leaderboard. Deploy the sample, connect to the Azure service, then insert a new username and score.

You can see that I've entered "Unity Tutorial Test" as the user name and "70" as the score. Hit "Insert To Leaderboard" AFTER you connect, and you'll be good to go.

Not sure if it went through? Well let's check the console.

Successfully inserted!

That's our callback function, which is executed on a successful insertion. If it didn't work, we would't see anything at all! Here's the code for that, where we use a lambda function:

 if (GUILayout.Button("Insert To Leaderboard"))
      Azure.insert(_leaderBoardItem, () => Debug.Log("inserted" + " " + _leaderBoardItem.UserName + " " + "to leaderboard"));

Still don't believe me that 's actually in our board? Well let's go take a look at our Azure Portal and see  for ourselves.




In your portal, go back to Azure Mobile Services, click on the name of your service (UnityWin8Test in my case), and that will load the main options screen. You can hit the "Data" tab on the top of the screen to see your leaderboards. Click on "leaderboard" from there, and you can see everything we've inserted! Boomshakala!


To Be Continued...

We've got one of the functions out of the way. In the next tutorial, I'll cover the others and wrap everything up. Look for more next week!

RESOURCES: You can find the source for this project on my GitHub.