Using expansion files .obb
I need some help I have been fighting this for the passed week and I am at the end of my rope. If i can't get this to work this is pretty much a make a break for my game so any help with be appreciated. I have done plenty of research online but everything i find is either out of date of gives me bits and pieces of the information i need.
I downloaded the unity plugin that helps to download the .obb file from google play. I tried to edit my android manifest file I have two inside the temp folder of my application one regular and one main which should i be editing I have no clue.
I followed the instructions on this web page: https://www.exoa.fr/tutorial-unity-4-apk-splitting-google-play-obb/ but it really only gives you bits and pieces of information.
So here is what I got here is the regular android manifest file:
<activity android:label="@string/app_name" android:name="com.unity3d.player.UnityPlayerActivity" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<activity android:name="com.unity3d.plugin.downloader.UnityDownloaderActivity" />
<service android:name="com.unity3d.plugin.downloader.UnityDownloaderService" />
<receiver android:name="com.unity3d.plugin.downloader.UnityAlarmReceiver" />
and here is the main andriodmanifest file:
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
Here is DownloadOBBexample edited:
using UnityEngine; using System.Collections; using TMPro; using MadLevelManager;
public class DownloadObbExample : MonoBehaviour {
private string expPath;
private string logtxt;
private bool alreadyLogged = false;
private string nextScene = "Splash";
private bool downloadStarted;
public string line00;
public string line01;
public string line02;
public string line03;
public TextMeshProUGUI words;
//public Texture2D background;
//public GUISkin mySkin;
void Start ()
{
words.GetComponent<TextMeshPro> ();
words.text = "";
}
void Update()
{
if (!GooglePlayDownloader.RunningOnAndroid ())
{
words.GetComponent<TextMeshPro> ();
words.text = line00;
}
string expPath = GooglePlayDownloader.GetExpansionFilePath();
if (expPath == null)
{
words.GetComponent<TextMeshPro> ();
words.text = line01;
//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, 20), "Main = ..." + ( mainPath == null ? " NOT AVAILABLE" : mainPath.Substring(expPath.Length)));
//GUI.Label(new Rect(10, 25, Screen.width-10, 20), "Patch = ..." + (patchPath == null ? " NOT AVAILABLE" : patchPath.Substring(expPath.Length)));
if (mainPath == null || patchPath == null)
words.GetComponent<TextMeshPro> ();
words.text = line02;
GooglePlayDownloader.FetchOBB();
StartCoroutine(loadLevel());
//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.");
}
}
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;
//}
//string 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, 20), "Main = ..." + ( mainPath == null ? " NOT AVAILABLE" : mainPath.Substring(expPath.Length)));
//GUI.Label(new Rect(10, 25, Screen.width-10, 20), "Patch = ..." + (patchPath == null ? " NOT AVAILABLE" : patchPath.Substring(expPath.Length)));
// if (mainPath == null || patchPath == 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;
//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
{
MadLevel.LoadLevelByName (nextScene);
}
}
}
}
Here is my GooglePlayDownloader script edited:
using UnityEngine; using System.Collections; using System.IO; using System;
public class GooglePlayDownloader { private static AndroidJavaClass detectAndroidJNI; public static bool RunningOnAndroid() { if (detectAndroidJNI == null) detectAndroidJNI = new AndroidJavaClass("android.os.Build"); return detectAndroidJNI.GetRawClass() != IntPtr.Zero; }
private static AndroidJavaClass Environment;
private const string Environment_MEDIA_MOUNTED = "mounted";
static GooglePlayDownloader()
{
if (!RunningOnAndroid())
return;
Environment = new AndroidJavaClass("android.os.Environment");
using (AndroidJavaClass dl_service = new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderService"))
{
// stuff for LVL -- MODIFY FOR YOUR APPLICATION!
dl_service.SetStatic("BASE64_PUBLIC_KEY", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAohYcx5dRdrmzZk3wWv2Ao01NzsfMkzWENaYNme+246avxyu6v2a+cxvq6XtKTh062d5IRQVRU7pXfmyeMNTugnd4b5KqCS3t6Gx7yxGfLY831P+hYgwxCw3NsvBrCnribNDLlAiyC/l0naDQW+Iw7ZhTLprfU/BrpyCRQjkQUGcXVgxMLJjFwJa3jKjKTxESZnKZlHwcP48DCxromQ1brymvtUbiMVpPWte3z1FNbpj6ZW4a5h10LHYJ+bfpOtQUhwuKe7624TWhkZaA534rUSusZzAfGQ/JNc3S3Q5DG2/objLe+fKOSRlzHU6Hu6ww2Inl3owDmPEC+ItHOh3V/wIDAQAB");
// used by the preference obfuscater
dl_service.SetStatic("SALT", new byte[]{1, 43, 256-12, 256-1, 54, 98, 256-100, 256-12, 43, 2, 256-8, 256-4, 9, 5, 256-106, 256-108, 256-33, 45, 256-1, 84});
}
}
public static string GetExpansionFilePath()
{
populateOBBData();
if (Environment.CallStatic<string>("getExternalStorageState") != Environment_MEDIA_MOUNTED)
return null;
const string obbPath = "Android/obb/main.2.com.SwampWare.JeanPierre.obb";
using (AndroidJavaObject externalStorageDirectory = Environment.CallStatic<AndroidJavaObject>("getExternalStorageDirectory"))
{
string root = externalStorageDirectory.Call<string>("getPath");
return String.Format("{0}/{1}/{2}", root, obbPath, obb_package);
}
}
public static string GetMainOBBPath(string expansionFilePath)
{
populateOBBData();
if (expansionFilePath == null)
return null;
string main = String.Format("main.7.com.SwampWare.JeanPierre.obb", expansionFilePath, obb_version, obb_package);
if (!File.Exists(main))
return null;
return main;
}
public static string GetPatchOBBPath(string expansionFilePath)
{
populateOBBData();
if (expansionFilePath == null)
return null;
string main = String.Format("main.7.com.SwampWare.JeanPierre.obb", expansionFilePath, obb_version, obb_package);
//string main = String.Format("{0}/patch.{1}.{2}.obb", expansionFilePath, obb_version, obb_package);
if (!File.Exists(main))
return null;
return main;
}
public static void FetchOBB()
{
using (AndroidJavaClass unity_player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject current_activity = unity_player.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent",
current_activity,
new AndroidJavaClass("com.unity3d.plugin.downloader.UnityDownloaderActivity"));
int Intent_FLAG_ACTIVITY_NO_ANIMATION = 0x10000;
intent.Call<AndroidJavaObject>("addFlags", Intent_FLAG_ACTIVITY_NO_ANIMATION);
intent.Call<AndroidJavaObject>("putExtra", "unityplayer.Activity",
current_activity.Call<AndroidJavaObject>("getClass").Call<string>("getName"));
current_activity.Call("startActivity", intent);
if (AndroidJNI.ExceptionOccurred() != System.IntPtr.Zero)
{
Debug.LogError("Exception occurred while attempting to start DownloaderActivity - is the AndroidManifest.xml incorrect?");
AndroidJNI.ExceptionDescribe();
AndroidJNI.ExceptionClear();
}
}
}
// This code will reuse the package version from the .apk when looking for the .obb
// Modify as appropriate
private static string obb_package;
private static int obb_version = 0;
private static void populateOBBData()
{
if (obb_version != 0)
return;
using (AndroidJavaClass unity_player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
AndroidJavaObject current_activity = unity_player.GetStatic<AndroidJavaObject>("currentActivity");
obb_package = current_activity.Call<string>("getPackageName");
AndroidJavaObject package_info = current_activity.Call<AndroidJavaObject>("getPackageManager").Call<AndroidJavaObject>("getPackageInfo", obb_package, 0);
obb_version = package_info.Get<int>("versionCode");
}
}
}
I am not sure whats going on but I have the application published and when I install it on my device it makes it to the first scene and the unity screen blinks like its downloading something and it just sits there. Its kind of hard to trouble shoot when i cannot see what is in the console any recommendations on troubleshooting options or tutorials on this would be greatly appreciated.