Skip to content

Instantly share code, notes, and snippets.

@inear
Created June 13, 2017 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inear/0635a4bb76f777887a5e6cb96fef6c46 to your computer and use it in GitHub Desktop.
Save inear/0635a4bb76f777887a5e6cb96fef6c46 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class DownloadObbExample : MonoBehaviour {
// http://yuandraismiraldi.net/code/2013/12/15/splitting-apk-to-expansion-package-for-google-play-in-unity-4
#if UNITY_ANDROID && !UNITY_EDITOR && USE_SPLIT_BINARY && !DONT_USE_SPLIT_BINARY
/*
void OnGUI() {
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);
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)
if (GUI.Button(new Rect(10, 100, 100, 100), "Fetch OBBs"))
GooglePlayDownloader.FetchOBB();
}
}
*/
private bool downloadStarted = false;
void Start() {
var expPath = GooglePlayDownloader.GetExpansionFilePath();
if (expPath == null) {
//EtceteraAndroid.showAlert ("Alert", "Storage is not available!","Ok");
return;
}
string mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
string patchPath = GooglePlayDownloader.GetPatchOBBPath(expPath);
if (mainPath != null) {
Application.LoadLevel(1);
} else {
GooglePlayDownloader.FetchOBB();
StartCoroutine(loadLevel());
}
}
void Update() {
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit();
}
}
protected IEnumerator loadLevel() {
var expPath = GooglePlayDownloader.GetExpansionFilePath();
string mainPath;
do {
yield return new WaitForSeconds(0.5f);
mainPath = GooglePlayDownloader.GetMainOBBPath(expPath);
} while (mainPath == null);
if (downloadStarted == false) {
downloadStarted = true;
string uri = "file://" + mainPath;
WWW www = WWW.LoadFromCacheOrDownload(uri, 0);
yield return www;
if (www.error == null) {
Application.LoadLevel(1);
}
} else {
Application.LoadLevel(1);
}
}
#else
void Start() {
Application.LoadLevel(1);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment