- Home /
Save Texture Variable?
is it possible to save a texture variable and load it later?
What platform? Where does the texture come from? It is possible on most platforms. You'll have to give more information before someone can give you a more precise answer.
pc platform
it is a texture assigned by player (var user_texture : texture)
i want to save it on disk and load it later
How is this not relevant ? Do you know how complicated something this simple has been made ?
Because of the read/write flag, when you save your textures, you cant reopen them anymore at runtime. You need to have "using UnityEditor" to change the flag.
So, its a very relevant question, even for experienced users.
Answer by robertbu · Apr 26, 2013 at 03:47 PM
For PC platform (not web), you can use Texture2D.EncodeToPng() and WriteAllBytes().
var bytes = tex.EncodeToPNG();
System.IO.File.WriteAllBytes(pathAndName, bytes);
Note if the texture originally came from inside Unity, then you likely have to check the Read/Write enabled flag in the Texture Importer. As a good place to store files, you might want to take a look at Application.persistentDataPath.
As for reading it back in you can use the WWW class. Typically you would yield (see WWW for example) but disk loads are often quick enough to just wait:
WWW www = new WWW("file://" + pathAndName);
while (!www.isDone) {}
Your answer