There’s an example in the docs about how to load an image from a web cam (the WWW main page, scripting ref.).
What if I want to keep updating that image? But I don’t want to do it on every ‘tick’, just when the image is newer than the last. Is there any way to get info on that?
Also, any memory management issues with creating WWW objects and textures, like, 15 fps?
I will try that, do you think it might help with this problem (I currently have it in an Update() function which only reloads every 1/20th second at most):
The incoming image has the same name each time. When it loads, it loads the first frame, but reloads do not change. In fact, restarting does not load newer images. It’s like it’s cached and it’s just not getting fresh versions of it.
Is there a way to force WWW to reload? Not cache? or is it storing the texture in the library or something?
Very odd.
I would be interested to see how you’re handling the WWW call in your code, if it’s being done with a coroutine, or in the Update.
There are more sophisticated methods of getting the picture. It’s complicated, but it would work like this, and you’d never have to worry about the cache.
Every time you image is generated, give it an md5 hash name. Store the name and a timestamp in a mysql database.
Then Unity would send via a wwwform, it’s latest timestamp. The request would compare the timestamp being sent by Unity to the latest one in the database. It would then respond by sending the most up-to-date image back.
Every time you request, you should be getting a completely different image as a response, with a completely different name.
Again, that is very complicated, but it would assure no caching could ever occur. I am, however, more interested in your Unity code, because it shouldn’t have to be this complicated.
Thanks for the ideas. Do I need a hash? Couldn’t the cam server just name its file using the timestamp? Say UNIX. Ex …/281818338.jpg Then my loop could just try loading files named the same way.
Not exactly what I need. In my case, the cam is local. Saves to same jpg file each snap. So my code looks something like this (from the WWW article in the Scripting Reference):
// Get the latest webcam shot from my camera
var url = "file://c:\\snaps\\snap.jpg";
var lastTime = 0;
function Update () {
// update only so often
if ((Time.time - lastTime) < .1)
return;
lastTime = Time.time;
// Start a download of the given URL
var www : WWW = new WWW (url);
// get the game obj to paint it on
var go = GameObject.Find("viewscreen");
// assign texture
go.renderer.material.mainTexture = [url]www.texture;[/url]
}
Of course, the ‘find’ should go in Start, but I’m just trying to get it working at all right now.
I suppose I could make it a bit more sophisticated using the timestamp naming convention, and the web cam software would need to delete files as well as create them. Still seems overly complicated.