I don’t know, but I have this function in my old code:
(please note that is an old code writed in javascript… XD )
function checkPath(){
while(true){
Debug.Log(Application.dataPath);
yield WaitForSeconds(0.5);
}
}
It’s called before resetPath() function (it’s only for debug purpose). If you view the jar file, it sends the message “__loadMainLevel”, it waits 2 seconds and load the first scene on the obb file.
Here is the complete script:
#pragma strict
private var expPath : String;
private var serverUrl : String = "https://dl.dropboxusercontent.com/u.......squest.obb";
public var loadingBar : Transform;
public var screen01 : GameObject;
public var screen02 : GameObject;
public var loadingObject : GameObject;
public var loadingObject2 : GameObject;
public var textObject : GameObject;
public var loadFromOwnServer : boolean = false;
public var background :Texture2D;
function OnGUI(){
if( loadFromOwnServer == false)
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),background, ScaleMode.ScaleToFit,true,0);
}
function Awake(){
if( loadFromOwnServer == false) this.GetComponent(Camera).enabled = true;
else{
screen01.active = false;
screen02.active = false;
loadingObject.active = false;
loadingObject2.active = false;
textObject.active = false;
}
}
function Start () {
if(Application.platform != RuntimePlatform.Android){ //Editor loading
yield WaitForSeconds(5);
Application.LoadLevel("Main_GUI");
return;
}
var obbState : int = checkOBBFile();
if(obbState== 1)
Application.LoadLevel("Main_GUI");
else if(obbState == -1){
if( loadFromOwnServer == false) DownloadFromGooglePlay();
if( loadFromOwnServer == true) DownloadFromOwnServer();
}
else if(obbState == -2)
Debug.Log("External Storage is not available");
}
function DownloadFromGooglePlay() {
/*screen01.active = true;
screen02.active = true;
loadingObject.active = true;
loadingObject2.active = true;
textObject.active = true;*/
GooglePlayDownloader.FetchOBB();
var mainPath : String;
do{
yield WaitForSeconds(0.5);
mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
Debug.Log("Waiting mainPath "+mainPath);
}
while(mainPath == null);
var uri : String = "file://" + mainPath;
Debug.Log("downloading " + uri);
var www: WWW = WWW.LoadFromCacheOrDownload(uri , 0);
//Wait for download to complete
while(www.isDone == false){
//textObject.GetComponent(TextMesh).text = "Downloading... " + (www.progress*52000) + "/52000Kb";
loadingBar.localScale.z = www.progress;
yield;
}
if (www.error != null) {
Debug.Log (www.error);
return;
}
else{
Application.LoadLevel("Main_GUI");
}
}
function DownloadFromOwnServer () {
screen01.active = true;
screen02.active = true;
loadingObject.active = true;
loadingObject2.active = true;
textObject.active = true;
var uri : String = serverUrl;
Debug.Log("downloading " + uri);
//var www: WWW = WWW.LoadFromCacheOrDownload(uri , 0);
var www: WWW = new WWW(uri);
//Wait for download to complete
while(www.isDone == false){
textObject.GetComponent(TextMesh).text = "Downloading... " + (www.progress*52032) + "/55032Kb";
loadingBar.localScale.z = www.progress;
yield;
}
if (www.error != null) {
Debug.Log (www.error);
return;
}
else{
textObject.GetComponent(TextMesh).text = "Loading... ";
//Put file in the correct directory
Debug.Log("About to create directory "+expPath);
System.IO.Directory.CreateDirectory(expPath);
var futureFile : String = GooglePlayDownloader.GetMainOBBPath_ifNoExist(expPath);
Debug.Log("About to create file " + futureFile);
var bytes = www.bytes;
if (bytes != null){
System.IO.File.WriteAllBytes(futureFile, bytes);
Debug.Log("file created" + futureFile);
}
/////////////////////
////////////////////
GooglePlayDownloader.resetPath();
checkPath();
}
}
function __loadMainLevel(param : String){
yield WaitForSeconds(2);
Application.LoadLevel("Main_GUI");
}
function checkOBBFile() : int{
expPath = GooglePlayDownloader.GetExpansionFilePath();
if (expPath == null){
//Debug.Log("External Storage is not available");
return -2;
}
else {
var mainPath : String = GooglePlayDownloader.GetMainOBBPath(expPath);
var patchPath : String = GooglePlayDownloader.GetPatchOBBPath(expPath);
Debug.Log("expPath " + expPath);
Debug.Log("Main " + mainPath);
//Debug.Log("Main_ " + mainPath.Substring(expPath.Length));
if(mainPath != null) {
Debug.Log("Main " + mainPath);
return 1;
}
else{
Debug.Log("mainPath not available");
return -1;
}
}
}
function checkPath(){
while(true){
Debug.Log(Application.dataPath);
yield WaitForSeconds(0.5);
}
}
And this is the GooglePlayDownloader.cs with some changes:
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", "MIIBIjANBgkqhkiG9w0BAQ.....EleWfyliTpVzUUwvPY00/Mc6WaYa34Inju0TB6VeUA6b64pa2uHv08KEsAGoDwIDAQAB");
// 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";
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("{0}/main.{1}.{2}.obb", expansionFilePath, obb_version, obb_package);
if (!File.Exists(main))
return null;
return main;
}
public static string GetMainOBBPath_ifNoExist(string expansionFilePath)
{
populateOBBData();
if (expansionFilePath == null)
return null;
string main = String.Format("{0}/main.{1}.{2}.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("{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();
}
}
}
public static void resetPath()
{
AndroidJavaClass resetClass = new AndroidJavaClass("com.pixelratio.resetapplicationpath.DummyActivity");
resetClass.CallStatic("static__reload");
}
// 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");
}
}
}