How to split apk and generate obb for google play store using Unity 3.x and Windows. (Also works on Mac, haven’t tested it)

Hi guys,

I saw a lot of posts and I could not find this tutorial anywhere. So, I am posting it here for others. Scroll to the end to find my scripts.

I actually started with this great tutorial written for unity 4

Here are the steps for unity 3.x

0) Add the GoogleOBBDownloader package from AssetStore. Now go to GooglePlayDownloader.cs and change the PUBLIC_KEY in it to match yours

1) Create a new scene called “Scene 0”. Add a empty game object and attach the script “DownloadObbExample.cs”

2) go to build settings and select only “Scene 0” and build the apk file from it.

Note: Make sure you fill out public key, keystore, version and package name in player settings.

3) Now add the script BuildStreamedSceneAssetBundle.cs in Assets/Editor

4) Edit the script to add all the levels you want to package

5) You wil see the option BuildWebplayerStreamedLevels in Build menu of unity. Use that to build asset package which is a .unity3D file

Note: asset packages are not distributable. Hence, you have to edit this line in the BuildStreamedSceneAssetBundle.cs for each platform and rebuild the asset package

BuildPipeline.BuildStreamedSceneAssetBundle(levels, “Levels-AssetBundle.unity3d”, BuildTarget.Android);
//for pc and mac use BuildTarget.WebPlayer and for Iphone use BuildTarget.Iphone

6) Rename your asset bundle as ” main.versionNumber.packageName.obb” (versionNumber and packageName should match the ones in step 2)

Note: if you have multiple .unity3D files you can zip them together and then rename that zip file as above

7) Upload them both on google play and your game should load smoothly.

Here is my code from “DownloadObbExample.cs”
public class DownloadObbExample : MonoBehaviour
{
private string expPath;
private string logtxt;
private bool alreadyLogged = false;
private string nextScene= “StartingLevel”;
private bool downloadStarted;
private string progress=” “;
void log( string t )
{
logtxt += t + “\n”;
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, Screen.width-10, Screen.height-10),
logtxt+” “+progress);
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)
log (“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 );
if (mainPath != null)
StartCoroutine(loadLevel());
}
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);
while( !www.isDone )
{
progress = Time.time+” “+ http://www.progress;
yield return null;
}
AssetBundle ab=www.assetBundle;
if (www.error != null)
log (“wwww error ” + http://www.error);
else
{
if(ab==null)
log(” ab is null “);
else
{
ab.LoadAll();
log(Time.time+” loaded ab”);
Application.LoadLevel(nextScene);
}
}
}
}
}
here is my code for BuildStreamedSceneAssetBundle.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
public class BuildStreamedSceneAssetBundle : MonoBehaviour {
[MenuItem(“Build/BuildWebplayerStreamedLevels”)]
public static void MyBuild(){
string[] levels  = {“Assets/Scenes/level01.unity”,
“Assets/Scenes/level02.unity”,};
BuildPipeline.BuildStreamedSceneAssetBundle( levels,
“Levels-AssetBundle.unity3d”, BuildTarget.Android);
}
}
Advertisements

10 responses to this post.

  1. Posted by LT on May 7, 2013 at 12:40

    Hi, I am working on a game, pretty big (considering Google Play limit), around 200mb now (not a chance to make it under 50mb in any way). The problem is, I have free licence of Unity 3.5 (not! Unity Pro), and Unity Android and iOS (the ‘cheaper’ versions). Now I found out, that it’s not possible to use Asset Bundles in non-pro version of Unity =-O Does that mean, that will not be able to divide the app to .apk+.obb, meaning that I cannot get under 50mb -> can’t publish on Google Play? Thanks

    Reply

    • Posted by smartkiran on May 7, 2013 at 13:26

      Hey,

      On PC, it might be a little hard to do it. There are no tools AFAIK that will help you on this.
      If you have android license on mac, then you can use AndroidBuilderTool

      Reply

      • Posted by LT on May 7, 2013 at 13:34

        I’ll look into it, Thanks a lot! It’s my only hope I guess 😦

    • @LT, if you want I can compile your game on my machine which has UnityPro and give you the compiled resources.

      In return all I ask for is 3d models that you would have used that I can use in our games.

      Reply

  2. Posted by JJ on May 22, 2013 at 21:24

    Hi, may I know what does this following line mean? My editor treated the // as a comment and caused an error. Thanks.

    progress = Time.time+” “+ http://www.progress;

    Reply

    • Posted by JJ on May 23, 2013 at 03:16

      Another Question,
      I tried to build build my apk and obb and uploading it to google. It all went well. Then I install the apk file onto my android device, and when i launch the game, the log keep showing “waiting mainpath”. Please Help.

      Reply

  3. Posted by Nick on June 15, 2013 at 02:59

    Is it possible to open the OBB file with any software for Windows, or to join APK and OBB files in one single APK?

    Reply

  4. Posted by Angela on July 31, 2013 at 00:04

    Hii,

    Great Work! However I am not able to test before uploading it to the google play store. And I tried placing my obb file into my phone sd card, my game starts with a splash screen and then exits after 2 3 seconds delay. I wonder if assetbuilding part is correct or not. How can I verify this. Any help would be appreciated.

    Thanks!
    Angela

    Reply

  5. Thanks for the quick overview! I have a question though. I’m building a special purpose app, and like the commenter above mine is 150mb. Using this method, I assume it loads automatically on the first run, but what does it do on subsequent plays? Will it detect that/if the package file exists and just go straight into the app?

    Reply

  6. What’s the “package name”?

    Bundle Identifier = com.CompanyName.GameName
    Bundle Version = 1.0
    Bundle Version Code = 3

    “main.versionNumber.packageName.obb”

    Google says “main.314159.com.example.app.obb”

    I’m guessing the number after “main” is supposed to be the Version Code?

    So “main.3.com.CompanyName.GameName.obb”.

    Also, your DownloadObbExample code seems broken. What’s the “http://www.progress” part? Try them on pastebin.com instead.

    Do we have to put the .obb file anywhere special? And do we have to edit “GooglePlayDownloader” with anything more than just the PUBLIC_KEY?

    I’m having issues right now with “GetMainOBBPath” not finding the obb.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: