I am currently wondering if this would be a huge performance hit or not. I would like to serialize a Texture2D list which contains 50 Texture2D’s. I have never serialized textures before and I am thinking that this will result in slow performance and very large saved data.
If someone here could point me in the right direction that would be wonderful! The project that I am currently working on could have basically an unlimited number of these lists and I am playing around with the idea of saving them all for later reference. But for starters I am looking to save one of them to see how big the serialized list is and if it gives me a performance hit when serializing!
If this is not a good approach please tell me I am all ears!
It depends on how big your Texture2Ds are, but yes, there will be some performance impact depending on how often you do this.
You could spread them out by serializing one Texture2D per frame instead of trying to do them all at once.
You could also convert them into byte[ ] arrays using Texture2D.EncodeToJPG(), then save them out in another thread so the main (Unity) thread won’t be stuck waiting on the serialization.
Okay great! Thanks for the reply! Yea I was thinking something like threading and or saving each frame at a time with some kind of delay to lessen processing. So if I went the EncodeToJPG route, would I easily be able to then decode them from JPG to texture2d’s again? Will the JPG’s be saved? I don’t want to bog down the game directory with temporary files if I don’t have to.
Texture2D.EncodeToJPG just gives you a byte array in memory. It’s still up to you to save it to a file if you wanted to. That’s something you could do on another thread. Just keep in mind that you shouldn’t/can’t use Unity’s API on another thread, hence the need to convert to a byte array before passing to another thread before saving to a file.
You should be able to use Texture2D.LoadImage to convert back from byte array to Texture2D.