Handling coins, virtual currency, points, beans(?!) etc in the In App Purchasing Plugin

I’ve had a few developers ask me how to handle stuff like virtual currency in my In-App Purchasing plugin. Out of the box it handles ‘Managed’ items like extra game levels, new swords, santa hats of health 😉 etc with ease. However ‘UnManaged’ items that can be purchased multiple times tend to be a little more specific to your game. You probably already have a virtual currency variable, like ‘coins’, somewhere in your code and you just want to increment this by say 100 when someone purchases a productid of ‘coins_100’.

Well luckily this is really simple to do. After a person purchases a productid in your game, the plugin will automatically call ‘OnPurchaseResponse’ on a script attached to the ‘UnityGameObjectReceiver’ gameobject. In the demo scene I use this fact to call ‘UpdatePurchase’ to record when a ‘Managed’ item has been purchased. This can be easily extended to handle your ‘UnManaged’ items in the following way:

public static void UpdatePurchase(PurchaseInfo purchaseInfo){
  if(purchaseInfo==null) return;
  Debug.Log("OrderID:" + purchaseInfo.orderId + " productId:" + purchaseInfo.productId + " purchaseState:" + purchaseInfo.purchaseState);
  if(purchaseInfo.purchaseState == "PURCHASED"){		//If a purchase then add to list of purchased items
    //Put any Unmanaged items here
    if(purchaseInfo.productId == "coins_100"){
      GlobalStuff.coins += 100;
    //Managed Items
    if(!Instance.PurchaseInfos.Contains(purchaseInfo)){//Don't add again if this orderid already exists
      Save();//When you purchase an item, immediately save to file

Where ‘GlobalStuff.coins’ is your virtual currency variable, for example I tend to stick all my ‘globals’ in a ‘GameStateManager’ static class and expose them as static properties, so mine would actually be ‘GameStateManager.Coins’.

Note that you will probably want to save those precious coins for your players in between game sessions. PlayerPrefs is a good way to do this…

Hope that all makes sense. Of course this post covers the Android version of the plugin, however the iOS version is almost identical so can be amended in a similar way.

One Game A Month!?#

So I’ve decided to take the One Game A Month challenge. I’ve dedicated a whole menu item to it (yes, that serious 😉 ) and hopefully that will slowly fill out with 12 games…

Still going to work on larger games as well, in fact I may use the 12 smaller games to test out ideas that will go into the larger games.

The sort of things I need to test out and learn are:

  • A* Path finding
  • Turn-based style game structure
  • More complex AI
  • Emergent behaviours from simple rulesets
  • Procedural Content Generation

There’s probably more but that will do for the moment. So hopefully my smaller games will incorporate some of the above so I can learn and create at the same time.

As you can probably tell from the above list, my interests like firmly in roguelikes at the moment. Some examples of roguelikes are:

To find out more, a good podcast is Roguelike Radio. Worth checking out if you are a budding game developer.

Of course after saying all that, my first game is a simple tower building game, I wanted to start simple and just get used to a newer, more open way of working…


Test Ads in the Android Admob Unity Plugin

If you have an application all set up with the Android Admob Unity Plugin and you want some test ads so you can test clicking and such without upsetting Admob, here is how to do this.

First you will need your ‘Test Device ID’. The only way I know of to get this is to check the logcat in Eclipse while you device is plugged in (and running your game with Ads showing), search for a line that looks like:

To get test ads on this device, call adRequest.addTestDevice(“Your Test Device ID”);

After you have the device id, add the following method to the AdMobUnityPlugin class:

public static void SetTestMode(string test_device_id){
  AndroidJavaClass jc = new AndroidJavaClass(classname);

Then this should be called before you call StartAds(), i.e.

void Start(){
  AdMobUnityPlugin.SetTestMode("My Test Device ID in here");

That’s it, you should now get test ads. Obviously please remember to remove the call to the SetTestMode method before you deploy for real, however if you forget you can disable test mode in your application settings on the Admob website (just below where you can set the refresh rate).

The Facebook Graph API Part 1

I’ve had a few requests for a tutorial on using the facebook graph api with my social plugin, so here it is!

My Social Unity Plugin for Android supports the Facebook Graph API. So what does that mean exactly? Well facebook expose their functionality via a rather cool but perhaps slightly confusing web interface. This interface is driven by HTTP requests and the data you get back is in the JSON format. Quickly check those links so you know what both of these are then come back here…

The Graph API is well documented and you can easily test a query by using the Graph API Explorer. So if I said you can get the current user’s profile by using a graph path of ‘me’, you can easily check this by going to the Graph API Explorer and doing this:

In my plugin you can do the same thing on the demo screen:

Or in code:


In all the above cases, the response you would get will be in JSON, similar to:

  "id": "1234565", 
  "name": "Fred Bloggs", 
  "first_name": "Fred", 
  "last_name": "Bloggs", 
  "location": {
    "id": "111112222", 
    "name": "London, United Kingdom"
  "gender": "male", 
  "timezone": 0, 
  "locale": "en_GB", 
  "verified": true, 
  "updated_time": "2012-07-24T09:52:15+0000"

So how do we extract all that information out in C# within Unity? Well the latest version of my plugin includes ‘LitJSON‘ which is remarkably good at doing this, for example if the above JSON was in a string variable ‘response’, you could get the user’s last name by:

JsonData jdata = JsonMapper.ToObject(response);
lastname = (string)jdata["last_name"];

Nice and simple! In my plugin, all responses from a GenericRequest come back via the gameobject ‘UnityGameObjectReceiver’, and you need a script attached to this gameobject that has a ‘OnRequestResponse’ method (see the demo for an example).

Part Two of this tutorial will cover more complex queries…

Merging Android Manifest Files

So you’ve purchased a couple of Android Unity plugins and want to use them both within your game, however they both come with their own AndroidManifest.xml file so you need to combine them somehow. Hopefully the next few tips will help you out.

The first thing to find out is whether the plugin has to set up it’s own activity as the main launcher activity. For example, all my plugins don’t have to be the main launcher activity, however my facebook(social) plugin loses SSO capability if not set up as the main launcher activity. The line in the manifest file that sets this up will look something like this:

<activity android:name="com.platoevolved.socialpluginunity.SocialPluginUnityActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

Also note that at least one of the plugins will need to set itself up as the main launcher activity otherwise the plugins will not be able to respond to user clicks (but will appear to work in all other ways).

So once you have decided which plugin gets the main launcher activity, you now need to merge the important information from the other manifest file. So for example if the other manifest file has extra activities,services, meta-data lines, uses-permissions etc, you should merge these in. Make note whether these should appear before or after the /application tag.

For example here are the important lines from my Admob manifest that should appear just before the tag:

<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<meta-data android:name="admob_pub_id" android:value="Enter AdMob Publisher ID here"/>
<meta-data android:name="adposition_x" android:value="right"/>
<meta-data android:name="adposition_y" android:value="top"/>
<meta-data android:name="start_hidden" android:value="false"/>

And here are the lines that should appear just after the /application tag:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Note that for my AdMob plugin, if it is not the main launcher activity you will now need to call StartAds() somewhere in your code so that the plugin can get itself initialised.

For an example of three of my plugin manifests getting merged, see here.

For an example of the Admob and InAppBilling plugins merged see here.

Android Unity Plugin Bundle Released!

Fancy an Android Unity plugin that does everything?! Washes dishes, makes a nice cup of tea, tucks you in at night…. erm… This isn’t it but…

If you want ad banners, in-app billing and facebook integration added to your game in one fell swoop here is the bundle for you! Avoid the hassle of having to merge manifest files or integrate with other conflicting plugins. This bundle is ready to go and does it all! (note that I have redefined ‘all’ here to mean ads, in-app billing and facebook)

Find this awesome plugin bundle here.