Tutorial Unity 4 apk splitting into OBB for google play

Here is a quick step guide about how to use the Google Play OBB hosting feature for apk files larger than 50Mb, and how to use them in your game ! Why this guide ? If you’re there, that’s probably because, like me, you are bored about the lake of informations on this ! That’s right, you have scoured the forums to find how to use this huge feature, and you’re now probably disapointed. I hope my post will be helpfull for you ! Before continuing reading, if you’re on a MAC (arggh), there is another solution for you here http://forum.unity3d.com/threads/129042-Android-Builder-Tool, this one is not yet compatible with Google Play OBB, but the main thing is that it enable you to not touch your code at all, and forget about all those shitty obb and assetbundle files. Ok let’s talk about this OBB thing. What we are gonna do here is creating a « preload » scene that will download the rest of your game, store it and launch it.

Requirements :

– Unity 4, for the splitting feature. You can do obb files yourself with any unity3d packages and unity 3.4+, but then you will have to load those assetbundles inside manually. – The latest Google OBB Downloader http://u3d.as/content/unity-technologies/google-play-obb-downloader/2Qq – A Google Play developer account.

Creating the project

If you already have a Unity project, and I’m sure you have one, just open it. Create a scene named « sceneLoader » or anything you want, with a simple empty GameObject inside. Import the Google OBB Downloader package. Take care of the AndroidManifest file if you already have one for external plugins. You must merge them ! For example here is mine, merged with Prime31’s plugins Manifest :


Now that you have the Google OBB Downloader plugin imported, just put the DownloadObbExample script on your controller GameObject.

Setting the LVL public key

Go to your Google Play account under « Edit Profile » and copy you PUBLIC KEY. Then open GooglePlayDownloader.cs and add a variable like this for later editing, and paste you key there :

	
public class GooglePlayDownloader
{
	private static string PUBLIC_KEY = "YOUR PUBLIC KEY";

Then line 30, edit the line :

dl_service.SetStatic("BASE64_PUBLIC_KEY", PUBLIC_KEY);

 

Editing the Preloader

You can modify the script attached on your controller like my crap example here :

	
using UnityEngine;
using System.Collections;

public class DownloadObbExample : MonoBehaviour 
{
	private string expPath;
	private string logtxt;
	private bool alreadyLogged = false;
	private string nextScene = "SceneMenu";
	private bool downloadStarted;

	public Texture2D background;
	public GUISkin mySkin;

	void log( string t )
	{
		logtxt += t + "\n";
		print("MYLOG " + t);
	}
	void OnGUI()
	{
		GUI.skin = mySkin;
		GUI.DrawTexture(new Rect(0,0,background.width,background.height),background);

		if (!GooglePlayDownloader.RunningOnAndroid())
		{
			GUI.Label(new Rect(10, 10, Screen.width-10, 20), "Use GooglePlayDownloader only on Android device!");
			return;
		}

		expPath = GooglePlayDownloader.GetExpansionFilePath();
		if (expPath == null)
		{
			GUI.Label(new Rect(10, 10, Screen.width-10, 20), "External storage is not available!");
		}
		else
		{
			string mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
			string patchPath = GooglePlayDownloader.GetPatchOBBPath(expPath);
			if( alreadyLogged == false )
			{
				alreadyLogged = true;
				log( "expPath = "  + expPath );
				log( "Main = "  + mainPath );
				log( "Main = " + mainPath.Substring(expPath.Length));

				if (mainPath != null)
					StartCoroutine(loadLevel());

			}
			//GUI.Label(new Rect(10, 10, Screen.width-10, Screen.height-10), logtxt );

			if (mainPath == null)
			{
				GUI.Label(new Rect(Screen.width-600, Screen.height-230, 430, 60), "The game needs to download 200MB of game content. It's recommanded to use WIFI connexion.");
				if (GUI.Button(new Rect(Screen.width-500, Screen.height-170, 250, 60), "Start Download !"))
				{
					GooglePlayDownloader.FetchOBB();
					StartCoroutine(loadLevel());
				}
			}

		}

	}
	protected IEnumerator loadLevel() 
	{ 
		string mainPath;
		do
		{
			yield return new WaitForSeconds(0.5f);
			mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);	
			log("waiting mainPath "+mainPath);
		}
		while( mainPath == null);

		if( downloadStarted == false )
		{
			downloadStarted = true;

			string uri = "file://" + mainPath;
			log("downloading " + uri);
			WWW www = WWW.LoadFromCacheOrDownload(uri , 0);		

			// Wait for download to complete
			yield return www;

			if (www.error != null)
			{
				log ("wwww error " + www.error);
			}
			else
			{
				Application.LoadLevel(nextScene);
			}
		}
	}

}

google play obb In my case I have a sceneLoader at position 0, and a SceneMenu at position 1. Your scene 0 will be contained in the little apk, and all the others will be on the obb file. Open the « build settings » panel and add your scene like that. Then on the « player settings » you should check the new magical option « split binary » at the bottom. Then press « Build » not « Build and run » because with this one, your obb will be pushed on your device directly and you will not be able to test the download thing.google play obb Great ! You now have a build.apk with a small size (7MB in my case) and a huge main.build.obb. Rename the obb like this :

main.[A_VERSEION_NUMBER].[YOUR_GAME_BUNDLE_ID].obb

Your game bundle identifier should be the same as the one you set on the Player settings. In my case the name is :

main.5.com.exoa.ZombieSurvival.obb

Google Play Configuration

Okay now you must upload your APK and your OBB into your Google Play project. Upload first your APK, wait until it’s complete. Then select his expansion and upload it as well. Be careful, if the file is missnamed, the expansion will not be added and no error will display !google play obb Now activate your APK but do not publish the project ! You can test everything without making it publicly available ! You can see in the details that the expansion is taking place. google play obb Now let’s have a break, you must wait one or two hours for your expansion to be available 🙁

Let’s try it !

Connect your device to your computer and copy the build.apk without his obb into your sdcard. Install it, and launch it. The first time you load, the app is not finding any local obb, so press the fetch OBB button.google play obb If you have waited enough time, you should be able to see the download beginning ! Yeah ! Or else, a message will tell you that we could not find the file.google play obb After the download is done, The obb can be loaded, the SceneMenu can be called, and the game is running !

Final note

Once all the download process is working and that you’re okay with your preloader scene. You can just « Build and run » and the obb will be pushed on your device at the same time that your apk file. In that way you don’t have to do all the Google Play steps to continue developing. Be careful, if you push an obb file and modify the apk itself afterward, they will not correspond and you will maybe encounter some troubles to load scenes. To check errors, you can open a terminal and type

adb logcat -s Unity

while your application is running and your device is connected. To try again the download you can simply delete the obb file on your sdcard, and go back to your app ! google play obb Be careful #2, on most cases, when you will download the game on Google Play. It will automatically download the OBB at the same time. To avoid this, you must host the obb on your own servers. Feel free to correct me if you have more precise informations regarding that !

More infos