Hi, im trying to load an image with a coroutine but i can not manage to fix this
private IEnumerator LoadAvatar(string URL,ref Texture result)
{
yield return "";
WWW www = new WWW(URL);
Texture2D texTmp = new Texture2D(128, 128);
www.LoadImageIntoTexture(texTmp);
result = texTmp;
}
StartCoroutine(LoadAvatar(UserPhotoUrl,ref Photo));
Do you have an alrternative? thanks.
Solved thanks! robertbu
private IEnumerator LoadAsset(string url, System.Action<Texture2D> result)
{
WWW www = new WWW(url);
float elapsedTime = 0.0f;
while (!www.isDone)
{
elapsedTime += Time.deltaTime;
if (elapsedTime >= 10.0f) break;
yield return null;
}
if (!www.isDone || !string.IsNullOrEmpty(www.error))
{
Debug.LogError("Load Failed");
result(null); // Pass null result.
yield break;
}
Texture2D texTmp = new Texture2D(128, 128);
www.LoadImageIntoTexture(texTmp);
result(texTmp); // Pass retrieved result.
}
StartCoroutine(LoadAsset(UserPhotoUrl,value=> Photo = value ));
that code does not compile because you can not pass a ref parameter to a iterator method, i put it that way so you can understand what i want to do
What is this?
yield return "";
Get rid of that…you need to yield on www, as the examples in the docs show.