Does anyone know of any documentation or tutorials for saving texture changes during runtime?

I’m on the lookout for some tutorials or anything really that explains exactly how to save your textures in runtime, so for instance, if I create a customisation menu and have the buttons change various textures or even sprites on the gameobject in question I’d like to be able to keep those changes the next time I load up the game.

If you could point me in the right direction i’d appreciate it.

Is the sort of thing I should be looking at? Converting Textures to string?

There is a ‘EncodeToJPG’ and a ‘EncodeToPNG’ method on Texture2D:
http://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html
(I’d personally use PNG since it’s lossless)

They generate a byte array, that can be saved however you want… usually by creating a FileStream and writing the byte array to it:

Then when loading you read in your file to a byte array, again usually with a FileStream, and you call LoadImage to get a Texture2D:
http://docs.unity3d.com/ScriptReference/Texture2D.LoadImage.html

You could technically B64 encode the bytearray to then save it as a string… but that’s not really necessary. This is really only ever done if you need to embed binary data into some transport encoding that is string delimited. For example, embedding it in a json string, or xml, or hl7, or anything like that.

The downside to it though is that the resulting string takes up more space than the binary data would have in the first place.

1 Like

There is also File.WriteAllBytes and File.ReadAllBytes which can be a little easier to work with if you’re writing/reading the entire file each time.

2 Likes

Wow thanks! A whole new bunch of code to look through!