Anyone knows (has the actual code) how to sync the texture of the objects?! I have an objects in the scene so when I click it I can paste the URL of a picture and it will be loaded as a texture of that object! So now I’m trying how to share that texture with clients in the network?!
You’re most of the way there, but there is no direct way to do this in Unity networking. You could serialize the texture, send it, then deserialize it on the far end. You could convert it to a string object and send it that way, but I’m not entirely sure how well that would work with Unity’s RPCs. If you don’t mind saving it to disk and then loading the texture back from that, it would be easier; that would not work with a web player.
If this is for tags or something, such as those used in many FPS games, perhaps a central repository in a database would be better.
I thought about saving to disk and load it, but updating automatically is more easy (thinking of not “advanced” users).
I created a database (PHP MySQL) and where you can upload pictures and download into the disk, but then you had to write the link/path of that picture in the unity textfield so then it would be loaded as the texture! It’s a bit time demanding and nor really nice :(!
Send an RPC with the same texture link and have every client download the image from the URL.
Send the image data in a byte[ ] to the other clients. This is perfectly possible with unity networking, I do this in Cubelands.
I have not yet hit a limit in the byte[ ] length. (Strings are limited to 4096 chars but can be converted to byte[ ])
However!
Why are you using “while(true){”…this seems like a very heavy script to run: it keeps downloading and assigning the texture over and over again?
Please be more precise, what/which method would?
You dont need to update ‘trough the network’ as long as it’s the same everywhere right? (Synchronized)
He’s using a direct example from the scripting reference. I’m pretty sure its designed to constantly update, so in the event that the image on the server changes, the texture changes. (The example in the doc’s is using the url of snapshots from a camera that regularly change in front of a TGI Friday’s, lol).
And Leepo, the second suggestion of your is what I need (at least I think). Could you please send me the code, as I don’t know how to do it?!
And I use the while loop so I can update the link of the texture during game play as well, as the function Start() happens just once!
Assuming the other instances of this object in the other players’ games were running the same script (and therefore, have the same URL set for the www), then yes, the code I posted should work, although I didn’t test it or anything. If you changed the URL though, you’d have to set up an RPC to change it for the other players, too.
All the code I posted is doing is sending a message to all players to change the ball texture to the texture from that URL, so if all players are running the same script and updating the texture in in Start() from the same URL, then it should work.
Is it possible to download a texture from url using WWW class only when a new version is available? As mentioned before, downloading and assigning the texture using while(true) would be not desirable and very heavy in most situations.
yes. I do that actually, but you can’t use WWW (doesn’t support GETs with modified headers), and the server also needs to support the “If-Modified-Since” header.
//----------check the modification date/time of this file on the server-------
//get the last write time of the locally saved file
var lastWriteTime = File.GetLastWriteTimeUtc(localFilePath);
//Debug.Log("Sending request to check for new version of image");
var r = new HTTP.Request("get", WEB_IMAGE_LOCATION + filename);
r.AddHeader("IF-Modified-Since", Rfc822DateTime.ToString(lastWriteTime));
r.Send();
while (!r.isDone)
{
yield return null; //we're not done, so skip to the next frame.
}
//we should get a 304 if the file is not modified
//Debug.Log("got response " + r.response.message + " size: " + r.response.bytes.Length);
if (r.response.message == "OK") //the image has changed
{
//we have the bytes of the image now. Load it into the texture.
var saveTexture = new Texture2D(256, 256, TextureFormat.ARGB32, true);
saveTexture.wrapMode = TextureWrapMode.Clamp;
saveTexture.LoadImage(r.response.bytes);
// some other stuff.....
}
else
{
//file is up to date, or it doesn't exist, or something
}
The attached zip has the HTTP class, with a dll dependency (Ionic Zip library), as well as the RFC822DateTime class. I did not create the http class or its dependency, or the RFC822DateTime class - I can’t remember where I got them from specifically, but I went searching for similar to what you asked for
The nice thing about using the If-Modified-Since header is that it does exactly what you asked - it returns a 304 if the file isn’t modified, meaning it doesn’t download the image, saving on bandwidth. If the image has changed, then it DOES download the image, meaning you don’t have to make another web request to download it after checking.