Category Archives: Programming

Unity3D IAPs

I’ve been upgrading my Unity3D system to use the new Unity3D IAPS.  I’d been using a mixture of my own stuff on iOS and Unibill for Android, and wanted to clean up my code and simplify the cross-platform process.

Upgrading my code to reference UnityEngine.Purchasing system wasn’t too hard, but there was one thing I came across that cost me a lot of wasted time, the selected Store on Android.

For an Android build, you have a selection of store fronts you can support – Google Play, Amazon & Samsung.  (They just added a ‘Select Store at Runtime’ option too).

You make this selection using the Editor, but there is nothing to indicate what this selection is.  I had selected Amazon while messing around in the project, and promptly forgot I’d done that.  So when I finally called the code to initialize the purchase tokens, they would never authorize and always return ‘product not found’ error.  Initially I blamed this on the fact it can take Google Play 24 hours to activate a new purchase token, so I took a break…

The next day, the same problem, but I thought maybe the 24 hours wasn’t technically up yet, or maybe Google was taking longer than normal.  I pushed on with some other tasks, and checked the products by running the App from time to time on an Android device and watching ADB for the results – still nothing.

That’s when I remembered the store selector! I selected Google Play, rebuilt and presto, it worked! However, it would have been nice if either (or both)

(a) The Editor menu had a check mark beside the selected store

(b) The system spat out an ADB message with the name of the store it was checking

It also turns out you can select the store via a script, which I decided was the better option for me.

UnityEngine.Purchasing.UnityPurchasingEditor.TargetAndroidStore(androidStore)

where:

public AndroidStore androidStore;

is a property on a MonoBehaviour which creates a nice drop down in the inspector.

Non-breaking space in Unity3D/NGUI

I was messing with some text line wrapping an an app today, and needed to stop NGUI inserting a line break between a number and a label in a long string.  Using the escape sequence ‘\u00A0’ did the trick.

return category + "\u00A0#" + indexInCategory;

Now the last word in the string category and the number preceded with a # stay on the same line!

Note: I read that pressing ALT-Space on the Mac will insert this character.  It may do that, but MonoDevelop/NGUI don’t seem to care.  The escape literal worked, the ALT-Space just acted like a regular space.

Note: To insert a non-breaking space in a XML document without using Document Entities, use the following:  

Updating the Unity Splashscreen

Looks like they didnt expose the splashscreen property, which is a pain… I have a script that allows me to switch between a Paid version and Free version of an App inside Unity.  Makes developing the App a lot simpler… just flip a toggle and presto, new icon, no ads and no ‘remove ad’ button!

I wanted to swap the splashscreen between the paid and free versions with this toggle, but the fine folks at Unity didn’t think of that, so after some searching, I ended up copying the correct screen over a generic splashscreen, but I had to use File.Copy as AssetDatabase.CopyAsset would reset the splashscreen in the build settings, which defeated the purpose!  I call to Refresh() when the copy was done updated the build setting and asset database.

	string srcPath = AssetDatabase.GetAssetPath(splashScreen);
	Debug.Log("srcPath: " + srcPath);
	string dstPath = Path.GetDirectoryName(srcPath);
	dstPath+="/splashScreen" + Path.GetExtension(srcPath);
	Debug.Log("Duplicating " + srcPath + " into " + dstPath);
	File.Copy(srcPath,dstPath,true);
	//AssetDatabase.CopyAsset(srcPath,dstPath); // doesnt work correctly
	AssetDatabase.Refresh();

Hope that helps someone else till they fix it.

Unity3D Application.persistentDataPath is null on Android

I’ve no idea why this is happening all of a sudden, but the static property Application.persistentDataPath is returning a zero length string and not the path to the storage folder on my Android devices.  From googling, I can see I’m not the only one with this problem.  It’s happening whether I build directly from Unity, or download it from the beta tab on Google Play.

I added the following method to my Android plugin JAR so at least I could move on and not get stuck:

    public static String PBgetFilePath(final Activity activity)
    {
        File path = activity.getApplicationContext().getFilesDir();
        Log.i(LOGTAG,"Internal FilesDir: " + path.getPath());
        return path.getPath();
    }

This returns the correct string and when I use it, I can read and write files as expected.  If persistentDataPath returns a null or a zero length string, then I call my java code and use the result.

ADB Logcat candy

Normally, when I’m debugging stuff on my android code, I use the command line version of adb in a terminal window on my mac.  I’ve been using various tags after the -S command to filter the output to just the stuff I care about, like so:

adb logcat -S Unity PBAndroid

This will filter the output to show only lines that have Unity or PBAndroid as tags, and I use PBAndroid for all my Log.? outputs in my java plugins.  Normally this has served me well, but I was running into an issue with using the new Unity IAP system and there were messages I was missing that would have lead me to a solution to the problem much sooner if I’d seen them, but I was filtering them out.

After some googling, I found the following awesome line on StackOverflow that is going to be my new go to ADB command:

adb logcat | grep `adb shell ps | grep com.example.package | cut -c10-15`

I replaced the com.example.package with com.purplebuttons. and it shows me ALL the output for my application.

I added the following to my /bin folder as ‘adblog’:

!/bin/bash
#call adb logcat on a specific bundle id
if [ $# -eq 0 ];
then
 echo "Syntax: $(basename $0) bundleid (e.g. com.purplebuttons)"
 exit 1
fi
adb logcat | grep `adb shell ps | grep $1 | cut -c10-15`

Now I can filter the output to my application by using:

adblog com.purplebuttons