- Home /
Using WWW class to get multiple urls
I have several models (.obj) in my server and I want to get the URLs of all of them. I know in c# there's Directory.GetFiles but that's not usable, i also know WWW can get one of the models with a URL but I want all of them.
The models are stored inside a folder called Models, is there anyway to get all of their urls or something similar so that I can load them using an .obj loader.
can you not put an xml file or a txt file on the server which contains the urls of all the models?
Or use a something like a php script on the server which scans the folder and returns the file list upon requesting it via WWW class.
I'm not very good at server based coding, but I think the php script suits my needs, do you have any examples or links that I could go see how to go about doing?
Also my first time working with the WWW class :P
Answer by fafase · May 24, 2016 at 08:28 AM
Your best shot would be to download a json file from the server with all the urls you need to download.
As a result, your application only knows one url, for the json file. This also allows to modify the models without the need to ship a new version of the app.
{
"models":[ "models/modelA.obj", "models/modelB.obj",]
}
[Serializable]
public class RootObject{
public string [] models;
}
public class ParseJson:MonoBehaviour{
string url = "http://myurl.com/"
IEnumerator Start()
{
WWW www = new WWW(url+"data.json");
yield return www;
// Check if all valid
RootObject ro = JsonUtility.Deserialize<RootObject>(www.text);
foreach(string modelUrl in ro.models){
WWW newWWW = new WWW(Path.Combine(url, modelUrl));
yield return www;
// Continue with models and checking
}
}
}
This is quite of a fastened version. The top is the json file that you download. It contains the two urls for two models. This is the file you would modify to add, remove models or other type of data.
Then the code simply reads the content to download the required assets.
The other way is AssetBundle:
Yup I know about the assetbundle but doesn't suit the project. I think I can use your answer, I'll give it a shot and let you know. Thanks.
As I mentioned, this is fairly quickly done and should benefit of a more modular pattern where download is handled in one class and the json somewhere. But that was just for the idea.
If you need more help, well, just ask.
Answer by schashm3 · Apr 28, 2019 at 03:29 PM
zip your files and upload to your database and download the file zip in your app and unzip it...it's very simple... here is the unzip code... you must put the ZipFile.cs in your project and after that you get your zip file form url and just write ZipFile.Unzip(path,www.byte);
public class unzip : MonoBehaviour{
IEnumerator DownloadandUnzip(){
UnityWebRequest www = UnityWebRequest.Get("yourURL.zip");
yield return www.SendWebRequest();
if (www.error!=null)
{
Debug.Log(www.error);
}
else
{
byte[] data = www.downloadHandler.data;
ZipFile.UnZip(Application.persistentDataPath, data);
}
}
this is the GitHub link for asset: https://github.com/kimsama/Unity-ZipDownload
Your answer
Follow this Question
Related Questions
How to assign texture from url 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to download in the background using WWW class? 4 Answers