The question is answered, right answer was accepted
Trouble downloading OBB files with Android split binary application deployment
Hi.
First of all, I'd like to clear up that the split binary is not intended to be uploaded to Google Play, but to another distribution service that's similar to it.
Here's the problem: I can already download and move the file to the Android/obb/com.companyname.productname path, but when going I try to go to the next scene it just stays in the initial scene. Upon restarting the app it just stays frozen on the Unity splash screen and doesn't do anything. I've already checked multiple times and the OBB file is indeed correctly downloaded in the internal storage.
I have tried downloading the file to external storage as well, but to no avail, so I have no idea if that's even the problem.
However, moving the file manually to the obb folder before starting the game seems to make it work.
Here's my code:
void Start ()
{
if (currentVersion != "13")
notCurrent = true;
else
notCurrent = false;
#if UNITY_ANDROID
if (!needsOBB)
{
SceneManager.LoadScene("Splash");
}
#else
SceneManager.LoadScene("Splash");
#endif
if (needsOBB)
{
bundleIdentifier = Application.bundleIdentifier + "/";
fileName = string.Format("main.{0}.{1}.obb", 13, Application.bundleIdentifier);
fullTargetUrl = targetUrlPath + fileName;
obbFullPath = obbPath + bundleIdentifier + fileName;
if (File.Exists(obbFullPath))
{
SceneManager.LoadScene("Splash");
}
directories.text = string.Format("File Path = {0} \n", obbFullPath);
directories.text += string.Format("Target download URL = {0}", fullTargetUrl);
if (!testDownload)
StartCoroutine(DownloadAndroid());
else if (testDownload)
StartCoroutine(DownloadPC());
}
}
private IEnumerator DownloadAndroid()
{
//if (File.Exists(obbFullPath) && notCurrent)
//{
// File.Delete(obbFullPath);
// yield return null;
//}
WWW www = new WWW(fullTargetUrl);
string obbPathPlusIdentifier = obbPath + bundleIdentifier;
Directory.CreateDirectory(obbPathPlusIdentifier);
while (!www.isDone)
{
if (!File.Exists(obbFullPath))
{
progress = ((int)(www.progress * 100)).ToString() + "%";
progressBar.GetComponent<Image>().fillAmount = www.progress;
progressText.GetComponent<Text>().text = progress;
yield return null;
}
}
if (!File.Exists(obbFullPath))
{
progressBar.GetComponent<Image>().fillAmount = 1;
File.WriteAllBytes(obbFullPath, www.bytes);
progress = "Done";
progressText.GetComponent<Text>().text = progress;
SceneManager.LoadScene("Splash");
}
}
Answer by Milfeulle9 · Apr 20, 2016 at 02:57 PM
Yes, I have, actually.
The problem was that I was trying to copy the file asynchronously while also loading the next scene. This would cause the .obb file to be copied over, but be corrupted.
To solve this, just make sure you do the file copying in Update, so the sequence of events isn't asynchronous and it goes step by step, like this:
void Update()
{
if (needsOBB)
{
if (activated)
{
if (File.Exists(obbFullPath))
{
SceneManager.LoadScene(1);
}
else
{
if (!testDownload)
StartCoroutine(DownloadAndroid());
else if (testDownload)
StartCoroutine(DownloadPC());
activated = false;
}
}
}
if (FileIsSaved)
{
StopCoroutine("DownloadAndroid");
SceneManager.LoadScene(1);
}
}
private IEnumerator DownloadAndroid() {
while (!www.isDone)
{
if (!File.Exists(obbFullPath))
{
if (!string.IsNullOrEmpty(www.error))
{
downloadFailedDialog.SetActive(true);
progressText.GetComponent<Text>().text = "Download Fail";
progressBar.GetComponent<Image>().fillAmount = 1;
www.Dispose();
StopCoroutine("DownloadAndroid");
}
elapsedTime = 0.0f;
isDownloading = true;
progress = ((int)(www.progress * 100)).ToString() + "%";
progressBar.GetComponent<Image>().fillAmount = www.progress;
progressText.GetComponent<Text>().text = progress;
yield return null;
}
}
if (www.isDone)
{
if (!File.Exists(obbFullPath))
{
File.WriteAllBytes(obbFullPath, www.bytes);
progressBar.GetComponent<Image>().fillAmount = 1;
...
yield return new WaitForSeconds(timeToRestart);
FileIsSaved = true;
}
}
}
You can see that instead of switching scenes in the coroutine I've made it so it's done in Update instead.
Hope this helps anyone else with this problem.