I am loading textures from URL by using www in c# unity. As there are multiples textures, I want to load them asynchronously and show them in the unity 4.6 Image by changing them into sprite so for that I want to pass the Image reference as out parameter to the coroutine that in turn will yield the texture. I have tried callback/delegates but that doesn’t serve my purpose. help me solve this issue. i just want these images to be assigned to the UI element and don’t want to store them manually and assigning them later. As that is not the best approach. although the approach i am using is also not what it should, so suggest me how can i make this happen effortlessly.
Why don’t you use callbacks?
I have this class and I use it instead to ref parameter:
public class Result<T>
{
public T val;
}
You can take Result res
as a parameter to coroutine, set res.val
in the coroutine and use that value outside of coroutine when it returns.
This is the same idea (I think) as PL’s reply:
Texture2D[] BunchOTextures;
IEnumerator textureLoadCoroutine(int texNum) {
...
BunchOTextures[textNum] = ...
A ref variable “magically” points back to some variable in the main program. The trick to faking a ref parm is to add a middleman pointer. In my example, you can’t pass BunchOTextures[2]
as ref, but you can pass 2, and use 2 to go there.
PL’s example is known (in Java?) as Boxing,. You can’t pass val
as ref, but if val is in a class (which is like a box,) you can pass the class and use that to find val.