How can force this method to return a texture?

I am using unitywebrequest to load a texture from disk with this code

    public Texture2D LoadTextureFromFile( string path)
    {
        if (File.Exists(path)==true)
        {
            texzita = null;
            textRetrieved = false;
            StartCoroutine(LoadTextureRequest(path));
        
            return texzita;
        }
        else
        {
            Debug.Log("img doesnt exist");
            return null;
        }
      
    }



    public IEnumerator LoadTextureRequest ( string path)
    {
      

        using (UnityWebRequest dlreq = UnityWebRequestTexture.GetTexture(path))
        {

            yield return dlreq.SendWebRequest();


            if (dlreq.isNetworkError || dlreq.isHttpError)
            {
                Debug.Log(dlreq.error);
                textRetrieved = true;
            }
            else
            {
                Debug.Log("text retrieve success");
                texzita = DownloadHandlerTexture.GetContent(dlreq);
                textRetrieved = true;
            }

        }


        yield return null;



    }

However because the sendwebrequest yields, the texture is returning null.

How do I force the code to stall until the request goes through?

You don’t want the code to stall. If the code “stalls” your application will become nonresponsive. Your users (and the operating system) will think your game crashed until the file is done loading. What you want to do is take whatever you were going to do with the return value, and do it when the coroutine finishes. You could write another coroutine to wrap it:

IEnumerator LoadTextureAndDoSomething(string path) {
  texzita = null;

  // After this line, your texture will have been assigned
  yield return StartCoroutine(LoadTextureRequest(path);

  // Do something with the texture here.
}

Then start this coroutine also with StartCoroutine. Alternatively, you could add a callback function (probably an Action) as a parameter to LoadTextureRequest and invoke that function at the end of LoadTextureRequest.

Adding to what Praetor said above, the traditional way is to additionally supply a delegate that the coroutine calls to supply the texture to whoever wants it, or perhaps another delegate reporting whatever error it might have encountered.

I realize that you actually “intend” for that LoadTextureFromFile() to return a texture, but that just isn’t how modern software is best set up. Some resources are not always available instantly, so a function that asks for, waits and then returns the result is not practical in modern computing contexts.

Welcome to asynchronous programming! Here’s what it would look like to pass in a callback function:

public IEnumerator LoadTextureRequest ( string path, Action<Texture> callback) {
   // all your code to get a texture with a web request goes here
  Texture tex = DownloadHandlerTexture.GetContent(dlreq);
  callback(tex);
}

thank you for the replies, is there any alternative way for me to get the texture instantly? Maybe without using unitywebrequest? And also without resources.load since i need a file from outside the project folder

I dont mind the non responsiveness in this case Im just loading the texture to store pixel data in a file. I was using resources.load but I need to get my resources folder out of the project so I just need something that can replace resources.load.

Unfortunately Unity does. You need to yield so Unity can serve your request and give it back. Look up asynchronous programming if you doubt me.

I dont doubt you, so there isnt anything that can emulate resources.load for files outside of the project folder?

The closest is if you can open the target file using the System.IO hierarchy of calls, yes, you can do that substitution. But that won’t work for files on the net or anywhere else, just files present in your visible filesystem.

Another approach is to make your web requester load it, then write it to a temp file, then somewhere else your original code can just open it with the System.IO calls, once it has been signalled that it is ready.

that’s good! how could I get a conversion from filestream to texture2d?

You can convert an array of bytes to a Texture2D with this:

like this?

     public Texture2D LoadTexture( string path)
            {
                if (File.Exists(path) == true)
                {
                    using (Filestream fs = File.Open(path, FileMode.Open))
                    {
                        Texture2D tex;
                        tex.LoadImage(fs);
                        return tex;
                    }

                }
            }

Read the data first into an array of bytes, THEN pass it to that LoadImage function.

hmm like this then?

 public Texture2D LoadTexture( string path)
            {
                if (File.Exists(path) == true)
                {
               
                        Texture2D tex;
                        tex.LoadImage(File.ReadAllBytes(path));
                        return tex;
                 
                }
            }

Guessing how to use an API is something we did in the 1980s and earlier.

Please go review how to read an array of bytes in C#. It’s easy, but there may be considerations related to your specific needs.

hmm it looks correct to me though