c# - Download and save image into Application.persistentDataPath hangs the app -
i wrote game can download images server , store them inside application.persistentdatapath.
my problem when saving few images scene hangs , when done saving, executes rest of code.
how can solve issue?
saving image device local storage:
if (file.exists (application.persistentdatapath + "/layoutimages/")) { debug.log (imagespathprefix + " exists."); return; } file.writeallbytes (application.persistentdatapath + "/layoutimages/abc.jpg", image);
you can create thread
performing operation on side. here example :
class filedownloader { struct parameterobject { public string url; public string savepath; } static void downloadfunction(object data) { parameterobject obj = (parameterobject)data; if (file.exists(obj.savepath)) return; using (webclient client = new webclient()) { client.downloadfile(obj.url, obj.savepath); } } public static void downloadfromurl(string url, string savepath) { parameterobject obj = new parameterobject(); obj.url = url; obj.savepath = savepath; thread thread = new thread(filedownloader.downloadfunction); thread.start(obj); } }
note: if use image download, not use threading. unity3d not thread safe.
Comments
Post a Comment