Reputation: 75
I googled my way through lots of questions all day but I didn't seem to find an answer fitting my problem, so I hope somebody can help me.
I want to display several image streams in a Unity scene. I have several GameObjects with the loading-script attached, e.g.
GameObject1 with script Loader.cs (calls coroutine in Start)
GameObject2 with script Loader.cs (calls coroutine in Start)
GameObject3 with script Loader.cs (calls coroutine in Start)
and load my images via a coroutine in this script:
IEnumerator Load()
{
Texture2D tex = new Texture2D(4, 4, TextureFormat.DXT1, false);
while (true)
{
WWW www = new WWW(url);
yield return www;
www.LoadImageIntoTexture(tex);
img.texture = tex;
}
}
(Where img.texture is my image in the scene and url is the url i'm loading from).
This works fine for loading the images, but I notice the images load/ stream faster once I start more coroutines. To clarify, if i have 1 image stream it's updating the images at a certain speed but if i have, e.g. 3 image streams (each having a coroutine for loading) suddenly all 3 streams load images faster than with 1 stream.
I tried adding a yield return new WaitForFixedUpdate();
at the end of the coroutine but it didn't help, and i have no idea how to achieve a regular pace in loading new images, independent how many streams/coroutines i have at the same time?
I hope it's clear what i mean and any help is appreciated!
Upvotes: 1
Views: 1582
Reputation: 125435
Starting multiple coroutines updates the image faster because what you have is downloading the image at different times from different Threads. The native side of the WWW
API is implemented with Threads. Note that coroutine is not the-same as Thread
.
The use of coroutine with the WWW
API is to be able to wait for that WWW
request to finish before accessing the output(image).Basically, you have 3 coroutines that runs forever. While each one is downloading the image, the other one is likely uploading the one it has already downloaded to Texture2D
.
Regardless, this is not the correct way to stream image. The WWW
API cannot be used for this purpose. It may look good to you but it is a bad hack.
You have two options:
1.Use new Unity's UnityWebRequest
with its DownloadHandlerScript
extension.
2.Use C# standard HttpWebRequest
API with Thread
then send the result to the main Thread using this technique.
No matter which option you chose, the technique to read from jpeg still remain the-same.
1.Make connection to the url
2.Read from that stream
3.Search for byte 0xFF
followed by 0xD8
4.When you find byte 0xFF
followed by 0xD8
, start reading the stream as and keep storing them into a byte array/List.
5.While reading the jpeg stream, keep searching for byte 0xFF
and 0xD9
.
6.When you find byte 0xFF
followed by 0xD9
, stop reading.
7.Your jpeg image is now byte array from constructed from 0xFF
, 0xD8
followed by the whole byte
array from step #4 and then 0xFF
, 0xD9
.
8.Finally, use Texture2D.LoadImage
to load the byte array into Texture2D and display it to the screen.
9.Jump back to step #3 and keep repeating that since a stream does not have an end unless the server is down.
If you run into problems, you can always create a new post with a code you made from this answer. Here is a post that should also get you stated.
Upvotes: 0