- Home /
Streaming large texture through WWW or otherwise. (multiple passes)
I am developing an application for the web (flash and/or web player), and need to keep the size of the application to a minimum.
For this project I need a couple of large 4096x4096 png textures. I have to download these from the web at run time.
I have read around here that WWW.texture cannot "stream" a texture (as in, download an interlaced png in multiple passes). You must always yield until the whole image downloads. I need this functionality, otherwise the user will stare at nothing (or a very very low res placeholder) for quite a few seconds until the whole thing is done.
So I am trying to come up with alternatives.
Can anyone tell me how exactly does this WWW functionality work under the hood? It seems very odd to me that we don't have access to the partially downloaded data in this kind of www request.
What exactly does www.data
do? Could I read a partial stream of data from there, and encode it to an image/texture and show it to the user?
The only other alternative I can think of, is to load the images in the enclosing HTML page, then use periodic external calls from unity to fetch whatever data has been loaded so far in the <img/>. It feels like I shouldn't have to resort to this.
Is it at all possible to get access to a low level "downloadedBytes" container to fetch partially downloaded data in unity?
Answer by balloonanimals · Feb 25, 2017 at 05:16 PM
using System; using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; using UnityEngine.Networking;
[RequireComponent(typeof(UnityEngine.UI.Image))] public class ImageDownloader : MonoBehaviour { UnityEngine.UI.Image _img;
private Action del_action;
private UnityWebRequest wr;
private DownloadHandlerTexture texDl;
private Texture2D texture;
private Sprite sprite;
void Start () {
_img = GetComponent<UnityEngine.UI.Image>();
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
Debug.Log ("Started");
Download ("http://www.doctorsintraining.com/wp-content/uploads/2013/05/Woman-taking-test.jpg");
}
if (del_action != null) {
del_action ();
}
}
public void Download(string url) {
texture = new Texture2D (2, 2);
wr = new UnityWebRequest(url);
texDl = new DownloadHandlerTexture(true);
wr.downloadHandler = texDl;
wr.chunkedTransfer = true;
wr.Send();
del_action += Downloadupdate;
}
private void Downloadupdate()
{
if ( wr.downloadProgress > 0.1f ) {
//texture.LoadRawTextureData (texDl.data);
texture.LoadImage (texDl.data);
//texture = texDl.texture;
if ( sprite == null ) {
sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero, 1f);
_img.sprite = sprite;
}
}
if (wr.isDone) {
del_action -= Downloadupdate;
texture = texDl.texture;
}
}
}
Your answer
Follow this Question
Related Questions
Is WWW.movie supposed to be this slow? 2 Answers
Unity for showing a html stream 0 Answers
WWW Texture Error 302 1 Answer
Coroutine and set (big?) Texture / Sprite gets noticable Lag 0 Answers
Image Displaying Bleached Texture. 0 Answers