Search Unity

load my files from local with UnityWebRequest

Discussion in 'Scripting' started by darkr00t23, Oct 14, 2019.

  1. darkr00t23

    darkr00t23

    Joined:
    Oct 14, 2019
    Posts:
    2
    Hello!
    I have a problem with UnityWebRequest.
    I'm trying to load the files from local.

    My code is this :

    Code (CSharp):
    1.         string url= "file:///C://test/image.png"
    2.         UnityWebRequest www = new UnityWebRequest(url);
    3.         DownloadHandlerTexture downloadHandler = new DownloadHandlerTexture(true);
    4.         www.downloadHandler = downloadHandler;
    5.         AsyncOperation request = www.Send();
    6.  
    7.         while (!request.isDone)
    8.         {
    9.             System.Threading.Thread.Sleep(1);
    10.             Debug.Log("Loading...." + (request.progress* 100) + " %");
    11.         }
    12.  
    13.          Texture2D tex = downloadHandler.texture;

    DownloadHandlerTexture.Process is already 100%. Nevertheless isDone is always false.
    i don't know why is this the case. do you have an idee?
     
    Last edited: Oct 14, 2019
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    I can spot 2 errors in your code.
    1. The url is invalid - two slashes after C:
    2. You are stalling main thread. Texture creation is finished on main thread, so it is correct, that isDone is never true. You should be using coroutines or completion events.
     
    darkr00t23 likes this.
  3. darkr00t23

    darkr00t23

    Joined:
    Oct 14, 2019
    Posts:
    2
    oh I see because of main thread. Much thanks!!!