Hey guys. I’m trying to write a binary file which downloaded from remote server to Application.persistentDataPath on my android phone.But I don’t know why my code doesn’t work.Is there someone know what’s the problem?Thanks
public class NBAssetBundleDownLoadOperation : NBAssetBundleLoadOperation
{
public WWW Request { get; protected set; }
bool done = false;
protected bool downloadIsDone {
get {
return Request == null || Request.isDone;
}
}
public GameConfigAssetBundle Config { get; protected set; }
public bool IsManifest {
get {
return Config == null;
}
}
//this constructor is just for manifest file
public NBAssetBundleDownLoadOperation()
{
}
public NBAssetBundleDownLoadOperation(string name)
{
Config = NBAssetBundleManager.GetSingleton ().GetAssetBundleConfig (name);
}
public NBAssetBundleDownLoadOperation(GameConfigAssetBundle config)
{
Config = config;
}
public WWW Load()
{
if (Request == null) {
string path = string.Empty;
if (IsManifest) {
path = NBAssetBundleManager.GenerateAssetBundleManifestDownloadUrl ();
Debug.Log ("Load Main Manifest Begin!" + path);
} else {
path = NBAssetBundleManager.GenerateAssetBunldeDownloadUrl (Config);
Debug.Log ("Load AssetBundle:" + Config.name + " Begin!" + path);
}
Request = new WWW (path);
}
return Request;
}
public override bool Update()
{
if (!done) {
if (ProgressDelegate != null) {
FinishDelegate (this);
}
if (downloadIsDone) {
done = true;
FinishDownload ();
if (FinishDelegate != null) {
FinishDelegate (this);
}
}
}
return !done;
}
protected void FinishDownload()
{
string path = string.Empty;
//if file compressed then uncompress first
//for now write bytes into persistent folder directly
if (IsManifest) {
path = NBAssetBundleManager.GenerateAssetBundleFullMainifestName ();
} else {
path = NBAssetBundleManager.GetSingleton().GetAssetBundlePath(Config);
}
Debug.Log ("FinishDownload : Write Local File : " + path);
try
{
FileStream fs = new FileStream (path, FileMode.CreateNew);
BinaryWriter bw = new BinaryWriter (fs);
bw.Write (Request.bytes);
bw.Flush ();
fs.Flush ();
bw.Close ();
fs.Close ();
Request.Dispose ();
Request = null;
}catch( System.Exception e ) {
Debug.Log (e.ToString ());
}
if (Directory.Exists (path)) {
Debug.Log ("Write local file success:" + path);
} else {
Debug.Log ("Write local file fail:" + path);
}
}
public override bool IsDone()
{
return done;
}
}
Logs:
Unity :Load AssetBundle:audios Begin!https://s3.amazonaws.com/infi.nbdev.cc/testassetbundle/Android/audios
Unity : FinishDownload : Write Local File :
Unity : /storage/emulated/0/Android/data/com.Unity.AssetBundleDemo/files/audios-v0
Write local file fail:/storage/emulated/0/Android/data/com.Unity.AssetBundleDemo/files/audios-v0