1
\$\begingroup\$

I am bit new to Unity3d. I am fine with any solution either of the scripts.

Can we render an image in unity3d (which we have to download from HTTP path)? If yes then how or do you have any reference?

Thanks Vandana

\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

You should be able to download images using HTTP with WWW.texture

WWW.texture

Should be straight forward. Just provide the path to an image and apply the texture to a material.

\$\endgroup\$
2
\$\begingroup\$

I recently faced the same problem. I was creating an scrollable image gallery in Unity, where images are downloaded in real-time from the a web server. On iOS devices I was reaching very fast the memory limit (with consequent app crash). That was caused due to a very big number of www objects leaved in the memory and never deleted or released. Also a very big number of Texture2D objects leaked. The main misunderstanding is considering a Texture2D like a variable of an object that self-contained the image information. Texture2D only point to a referenced asset so we need to create one before assigning to our GUI object. Without creating one we will overwrite the referenced asset in our project, leading to very crazy behaviour. So this the code that works for me on iOS and just use the minimum of memory

    public void DownloadImage(string url)
    {   
        StartCoroutine(coDownloadImage(url));
    }

    IEnumerator coDownloadImage(string imageUrl)
    {

        WWW www = new WWW( imageUrl );

        yield return www;

        thumbnail.mainTexture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
        www.LoadImageIntoTexture(thumbnail.mainTexture as Texture2D);
        www.Dispose();
        www = null;
    }

A brief comment: You receive a url you will download the image from. Start the coroutine that manage the download Create the www object in the local scope yield waiting the download to complete Create a new Texture2D object/asset with parameters and assign to your final object mainTexture or what do you want Use the "www.LoadImageIntoTexture()" function to COPY the image inside the created asset (this is the fundamental part) Dispose and set to null the www object to prevent memory leaking or orphans

Hope it helps who will face the same problem.

An interesting lecture is this website where they implement a WebImageCache system to avoid re-downloading the same image many times.

http://studiofive27.com/index.php/unity-cached-web-images/

ADD-ON Also try to avoid the direct assignment like:

myGameObjectMaterial.mainTexture = www.texture;

Unless you are conscious that you want to overwrite your asset and you don't to do this inside a loop or update.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .