hi there,
i have some problems to load the saved texture. saving works fine, but loading not.
error is : Cannot implicitly convert type int' to
UnityEngine.Texture2D’
here is my code :
//SAVE TEXTURE
Texture2D save_s01_texture;
void SaveTextureToFile (Texture2D texture, string filename){
byte[] bytes;
bytes = texture.EncodeToPNG();
System.IO.FileStream fileSave;
fileSave = new FileStream(Application.dataPath + "/Save/"+filename,FileMode.Create);
System.IO.BinaryWriter binary;
binary = new BinaryWriter(fileSave);
binary.Write(bytes);
fileSave.Close();
}
//LOAD TEXTURE
string filename = "s01_texture.png";
Texture2D loaded_s01_texture;
void LoadTextureFromFile (Texture2D texture, string filename){
System.IO.FileStream fileLoad;
fileLoad = new FileStream(Application.dataPath + "/Save/"+filename, FileMode.Open, FileAccess.Read, FileShare.None);
loaded_s01_texture = fileLoad.ReadByte();
fileLoad.Close();
}
Eric5h5
December 19, 2014, 7:33am
2
Just use System.IO.File.WriteAllBytes and ReadAllBytes instead. That way you can remove 90% of that code. e.g.:
void SaveTextureToFile (Texture2D texture, string filename) {
System.IO.File.WriteAllBytes (filename, texture.EncodeToPNG());
}
I used this method, you can also use cloud save just by saving the string to cloud like the google cloud.
Hope this help! This is how I approach this kind of method, after all those reseach.
You could try some of my sample apps here specially “Bytes Crafter - Unity Debug”
It has a simple debug window what is currently on process in the script.
LINK: https://play.google.com/store/apps/dev?id=6365497209902498330
public RawImage prevUserPhoto = ; This must be not null! Put a texture wd here!
public RawImage newUserPhoto = ; Leave this null. This is where decoded texture go.
ToLoad Image saved on PlayerPrefs as “string”
Texture2D newPhoto = new Texture2D (1, 1);
newPhoto.LoadImage(Convert.FromBase64String(PlayerPrefs.GetString(“PhotoSaved”)));
newPhoto.Apply ();
newUserPhoto = newPhoto;
ToSave Image from a Texture 2D which must be a “readable” and RGBA 32 format.
string stringData = Convert.ToBase64String (prevUserPhoto .EncodeToJPG ()); //1
PlayerPrefs.SetString(PhotoSaved, stringData );
But it doesn’t work while running on my phone