I have a need to dynamically load textures from the player's filesystem. WWW provides the functionality I need, but I can't get the example code to compile.

"yield www" causes my code to not compile. Here, the code is short:

    private List<Texture> LoadTextures (String prefix, int numTextures)
    {
        List<Texture> textures = new List<Texture> ();
        for (var i = 0; i < numTextures; i++ )
        {
            var www = new WWW(prefix + i);
            yield www;
            textures.Add(www.texture);
        }
        return textures;
    }

Why doesn't yield work here, like the code sample on the Unity docs suggests?

EDIT:

In response to Lasse:

    private IEnumerable<Texture> LoadTextures (String prefix, int numTextures)
    {
        for (var i = 0; i < numTextures; i++ )
        {
            var www = new WWW(prefix + i + ".png");
            yield return www.texture;
        }
    }

The above compiles, but I now get this error:

You are trying to load data from a www stream which has not completed the download yet. You need to yield the download or wait until isDone returns true.

It's as if it is not even yielding. What gives? The file does exist in this directory. I tried prepending "file:///" but that didn't do anything. Do I need to provide an absolute path, or is relative OK?

  • first off, it should be "yield return www" not just "yield www" – jhocking Nov 1 '14 at 0:05
up vote 3 down vote accepted

The code in the link you pasted seems to be UnityScript code. In C# yield works a bit differently than it does in UnityScript, which works like in javascript. Yield reference docs.

You are probably looking for this code.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public string url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
    IEnumerator Start() {
        WWW www = new WWW(url);
        yield return www;
        renderer.material.mainTexture = www.texture;
    }
}

Then adapting that you could do something like this:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {

    List<Texture> textures;
    List<string> urls;
    bool done = false;

    void Start() {
        StartCoroutine("LoadTextures");
    }

    IEnumerator LoadTextures() {
        foreach(var url in urls)
        {
            WWW www = new WWW(url);
            yield return www;
            textures.add(www.texture);
        }
        done = true;
        yield break; // May be unneeded
    }
}

Note: I have not used this myself, so the code might not be working. I am not currently able to test it out.

Also, if you try to load external (local on the computer) files that are not assets, Unity itself does not support that. You need to load the files by some other means, like System.IO.

EDIT: Apparantly you can with "file://", but it needs to be absolute path I guess. You can try to find the absolute path with help of .net and Path class.

  • I updated my post with a response to your original answer. Give me a moment to review what you just edited in. – Connor Clark Oct 31 '14 at 23:24
  • You know, I would prefer to use System.IO and just load the bytes. Is there a way to create a texture from a byte array in Unity? – Connor Clark Oct 31 '14 at 23:35
  • Issue resolved, all works now. Thanks for the help, Lasse. Now I can happily leave my computer and go enjoy Halloween...have a good evening! – Connor Clark Oct 31 '14 at 23:41

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.