0

I'm working on an iOS app on Unity. Eventually, the app should be able to download, import and load .obj files saved on my website server. But I'm currently developing locally so the files are saved in my laptop file system (local server side of my website).

My question is what I should use to access those files. I used WWW to access it but it seems not working. Please see my code below.

public void OnClick()
     {  
        StartCoroutine(ImportObject());
     } 

IEnumerator ImportObject (){
           Debug.Log("being called");

    WWW www = new WWW("http://localhost:8080/src/server/uploads/user-id/file name");
    Debug.Log("being called");

    yield return www;

    Debug.Log("NOT BEING CALLED !");

    **//Everything below here seems not being called...**

    if (string.IsNullOrEmpty(www.error)) {
        Debug.Log("Download Error");
    } else {
        string write_path = Application.dataPath + "/Objects/";
        System.IO.File.WriteAllBytes(write_path, www.bytes);
        Debug.Log("Success!");
    }

    GameObject spawnedPrefab;
    Mesh importedMesh = objImporter.ImportFile(Application.dataPath + "/Objects/");
    spawnedPrefab = Instantiate(emptyPrefabWithMeshRenderer);
    spawnedPrefab.transform.position = new Vector3(0, 0, 0);
    spawnedPrefab.GetComponent<MeshFilter>().mesh = importedMesh;
}
WoShiNiBaBa
  • 257
  • 5
  • 19
  • 1
    You said you want to download the model from a url but why are you doing this: `WWW("file://" + filePath);`? – Programmer Sep 12 '18 at 21:00
  • What is the error? You're writing to a directory and have no filename on the end of your write_path – pale bone Sep 13 '18 at 01:30
  • @Programmer I'm sorry for the confusion. Please see the updated code. It's actually taking in the url of the file stored in my local host. – WoShiNiBaBa Sep 13 '18 at 18:50
  • When you write a question with confusion you'll likely not get any answer. See what pale asked you. Do you get any error? What's the log or of the request? "Download Error" or "Success!" ? – Programmer Sep 13 '18 at 18:54
  • I didn't get any error, and everything below 'yield return www;' is simply not being called. I tried to add Debug.Log right after 'yield return www' and it's not being called. – WoShiNiBaBa Sep 13 '18 at 19:18
  • You have to put `Debug.Log` in the first line of code of the `ImportObject` function to see if `ImportObject` is called at-all. – Programmer Sep 13 '18 at 19:22
  • Yes I did that. ImportObject is called. I just added some Debug.Log to the code example here to indicate. – WoShiNiBaBa Sep 13 '18 at 19:44
  • That's really weird. What's your Unity version? Before you answer that, try [`UnityWebRequest`](https://stackoverflow.com/questions/46003824/sending-http-requests-in-c-sharp-with-unity/46008025#46008025) and tell me what happens. – Programmer Sep 13 '18 at 19:50
  • I tried "UnityWebRequest www = UnityWebRequest.Get(filePath);" And it is the same result. Nothing being called after "yield return www.SendWebRequest();" The version of my Unity is 2017.4.3f1 – WoShiNiBaBa Sep 13 '18 at 20:09
  • It probably can't resolve localhost. What happens when you try by loopback ip address? `http://127.0.0.1:8080/src/server/uploads/user-id/file name`? If the-same problem, what happens when you put the url in a web browser to try it? Also, I noticed that you have a space in the "file name" Make sure there is no space in that – Programmer Sep 13 '18 at 20:13
  • The same result. The file is downloaded after I put the url in the web browser. That 'file name' is just a placeholder. – WoShiNiBaBa Sep 13 '18 at 20:17
  • Ok. I really want to pinpoint the issue but haven't yet. Can you try with WebRequest and with the-same url and tell me what happens? See [this](https://stackoverflow.com/questions/43673410/unity3d-www-class-is-very-slow-with-android/43678419#43678419) post for example – Programmer Sep 13 '18 at 20:20
  • @Programmer I appreciate your help! I will check out this post and try it. – WoShiNiBaBa Sep 13 '18 at 20:22

1 Answers1

0

I tried multiple solutions from the internet and finally find the correct method to download and save the file by using the code below:

IEnumerator DownloadFile(string url) {

        var docName = url.Split('/').Last(); 
        var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);

        string modelSavePath = Path.Combine(Application.dataPath, "Objects");
        modelSavePath = Path.Combine(modelSavePath, docName);

        //Create Directory if it does not exist
            if (!Directory.Exists(Path.GetDirectoryName(modelSavePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(modelSavePath));
            }

        var dh = new DownloadHandlerFile(modelSavePath);
        dh.removeFileOnAbort = true; 
        uwr.downloadHandler = dh;  

        yield return uwr.SendWebRequest();

        if (uwr.isNetworkError || uwr.isHttpError)
            Debug.LogError(uwr.error);
        else
            Debug.Log("File successfully downloaded and saved to " + modelSavePath);
    }
WoShiNiBaBa
  • 257
  • 5
  • 19