- Home /
How to reduce an image to the most smallest size possible
Hello,
I'm not that familiar with compression types in Unity. I'm looking for a way to compress an image created on runtime to the smallest size possible, so it can be send over a local network using UDP. I also want to keep the quality to HD.
Right now I'm just using
Texture2D.EncodeToJPG()
But is there a more efficient way?
Thanks a lot!
Hey, did you ever find a solution? I am trying to send an image that the player takes over the network so I need to compress it but I could not find a good way to do so.
Answer by NickP_2 · Feb 16, 2016 at 09:40 AM
So, as for now, what I did:
Encode Texture2D to byte array.
Compress byte array using a compression script found here (This is actualy a pretty good life saver, as it works fast and efficient, even on mobile
Do you have a sample of your code? I'm using the algorithm to upload JPGs into a game and it's compressing by maybe 10-20%. I just wondering if this can be taken further to allow me to increase the quality of the image.
Answer by phil_me_up · Feb 15, 2016 at 02:03 PM
To get it down to the smallest possible fixe size, you'll want to look at the compression options by either setting the quality in EncodeToJPG(x) or by calling .compress.
Compression will actually depend on what the image is. In some cases JPG will be better, in others PNG. You might be able to apply other compression too by setting the format, but again the best format and method depends on the quantity of the image (you don't want to go compressing something with gradients as PVR for example).
You might find that if you're letting the player create the image through some sort of pain, you're better off sending data which describes this image in a way your game can understand, which will often be much more efficient.
Finally, I assume you're aware that sending via UDP will have it's flaws. If there is any noise or data loss / corruption in the stream then the data you receive may not be readable, or if it is may have errors. TCP has error checking at its core (which is why in most cases it is a bit slower). UDP works in cases like character movement because you can perform filtering / smoothing / validation as you receive packets.
Thanks for the reply,
I'm using this for sharing a camera view, so I can take a few frame losses :)
I basically already applied what you described (but unknowningly) so thanks for figuring that out!
Now, it would be awesome to compress the byte array, I found a few compression libraries, bit somehow my byte array got bigger ins$$anonymous$$d of smaller. I don't think it's possible to compress a compressed image byte array, or am I wrong?
If you're sharing a camera view, a better way might be to simply transmit the parameters of the camera across the network (position, fov, clipping planes etc) and have some camera on the received end adopt those parameters (i.e. Player1 camera position etc gets sent to Player2. Player2 main camera (or some other camera) then adopts those positions and they will then see the same thing)
Doing this would be ALOT more efficient and allow you to compensate for data loss.
that is indeed a good solution, altho I don't have the resources on the other client. I have to rely on images over the network :/
Answer by BennyBurke · Aug 13, 2020 at 09:43 AM
You might find that if you're letting the player create the image through some sort of pain, you're better off sending data which describes this image in a way your game can understand, which will often be much more efficient.
Your answer
Follow this Question
Related Questions
Issues getting WWW.LoadImageIntoTexture to use DXT1 compression at runtime 0 Answers
How can I change the default max size of a texture from an editor script (in import settings)? 1 Answer
Does Unity decompress Truecolor RGBA format textures when loading? 0 Answers
Texture Compression format for UWP,Texture 0 Answers
Only RGBA32bit works when setting Texture2D pixels on iOS? 0 Answers