So I have a save method that can save data to a binary file and another function that saves just the texture. I would like to save both of them in a same file. I guess I would need to create a serialization surrogate for the Texture2D (I did this for some other types like Vector3 and Quaternion) but I can't find anywhere how would I do it for texture. I am currently saving texture like byte array and I guess that could help but I am not sure how to add it to other data.
Here is my code for saving the data:
public static bool Save(string fileLocation, string saveFilename,object saveData)
{
BinaryFormatter formatter = GetBinaryFormater();
if (!Directory.Exists(fileLocation))
{
Directory.CreateDirectory(fileLocation);
}
string path = fileLocation + "/" + saveFilename + ".save";
FileStream file = File.Create(path);
formatter.Serialize(file, saveData);
file.Close();
Debug.Log("Save to: " + path);
return true;
}
And here is the one for the texture:
public static bool SaveTexture(string fileLocation, string filename, Texture2D textureToSave)
{
if (!Directory.Exists(fileLocation))
{
Directory.CreateDirectory(fileLocation);
}
byte[] byteArray = textureToSave.EncodeToJPG();
string savedFilename = fileLocation + "/" + filename + ".jpg";
File.WriteAllBytes(savedFilename, byteArray);
Debug.Log("File saved at: " + savedFilename);
return true;
}