Category Archives: Android

Unity 5.6 and Android overlays – Updated

Unity just released 5.6.2f1 which rolls back the change they made to their Android player.  While the method described below works, it’s no longer needed.  There was also some performance issues using the PopupWindow, so it’s better just to go back to using a view added to the Unity view.

I updated the Unity version I’ve been using to 5.6 when it came out of beta, but when I built my android app, I noticed a major problem – the ad banners no longer appeared!  So glad I made sure to test my app before updating in to GPlay 😀

Turns out 5.6 uses a feature in Android to make their view sit on top of all other views for that activity, and when my plugin would create a view to hold both my AdMob view and Amazon adView, it wouldn’t display them on top of the Unity view as before.

I tried a few different methods before seeing that the folks who maintain the AdMob plugin for Android on Unity had  updated their method to use a PopupWindow.  I did some research on this method, and got it to work for both AdMob and Amazon ads.

The one thing that had me stuck for a bit was that you cannot show a PopupWindow right after creating it in the application onCreate method, you need to wait for a bit.  This was also true when I created the PopupWindow during my plugin initialization.

I used the post() method on the rootView to get the delay I needed.

    if (mPopupWindow!=null)
        activity.getWindow().getDecorView().getRootView().post(new Runnable() {
        public void run() {
            if (isDebug)
                Log.i(LOGTAG,"Showing POPUP window...");
                            Gravity.TOP, 0, 0);

At some point, I’ll update my plugin to allow the PopupWindow to be either at the top or bottom of the display, right now it’s hard-coded to appear at the top.

Android ScreenCap with ADB

I found a great blog post on how to capture a screenshot from a connected device using ADB – I know I can use eclipse or android studio to do it, but I wanted a quick grab from the command line, and I didn’t want to be bothered with capping it to the local disk of the android device.

Anyway, here’s the one liner that works on MacOS:

adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png

Clear GooglePlay InApp Test Purchases

Google has no helpful documentation on how to clear test purchases you’ve made on your device with a Alpha/Beta build.  If I clear the App’s local cache, and re-run it, then it just automatically remakes the purchases (which is a good thing – no restore purchase option required).

I found this on StackOverflow, and posted it here as a reminder to myself:

adb shell pm clear

Does the trick – remember to clear the App’s cached data store too.

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);
	//AssetDatabase.CopyAsset(srcPath,dstPath); // doesnt work correctly

Hope that helps someone else till they fix it.

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’:

#call adb logcat on a specific bundle id
if [ $# -eq 0 ];
 echo "Syntax: $(basename $0) bundleid (e.g. com.purplebuttons)"
 exit 1
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