Tuesday, August 23, 2022
HomeGame Developmentc# - How would I save texture together with different knowledge in...

c# – How would I save texture together with different knowledge in binary file in Unity?


So I’ve a save methodology that may save knowledge to a binary file and one other perform that saves simply the feel. I wish to save each of them in a similar file. I assume I would want to create sterilization surrogate for the Texture2D (I did this for another varieties like Vector3 and Quaternion) however I can not discover wherever how would I do it for texture. I’m at present saving texture like byte array and I assume that might assist however I’m not positive how one can add it to different knowledge.

Right here is my code for saving the information:

 public static bool Save(string fileLocation, string saveFilename,object saveData)
{
    BinaryFormatter formatter = GetBinaryFormater();

    if (!Listing.Exists(fileLocation))
    {
        Listing.CreateDirectory(fileLocation);
    }

    string path = fileLocation + "/" + saveFilename + ".save";
    FileStream file = File.Create(path);
    formatter.Serialize(file, saveData);
    file.Shut();

    Debug.Log("Save to: " + path);
    return true;
}

And right here is the one for the feel:

 public static bool SaveTexture(string fileLocation, string filename, Texture2D textureToSave)
{
    if (!Listing.Exists(fileLocation))
    {
        Listing.CreateDirectory(fileLocation);
    }

    byte[] byteArray = textureToSave.EncodeToJPG();
    string savedFilename = fileLocation + "/" + filename + ".jpg";

    File.WriteAllBytes(savedFilename, byteArray);
    Debug.Log("File saved at: " + savedFilename);

    return true;
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments