Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
wow what a coincidence, I just wrote about this earlier this week for the newest chapter in my book. Anyway the already existing answers should cover what you need (although the last chunk of erik's code looks more complicated than it needs to be) –  jhocking Oct 24 at 15:50

2 Answers 2

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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