using System; using System.IO; using UnityEngine; namespace SDev { /// /// Save byte array, Texture2D to file on current platform's Application data path or Windows/Mac special folder. /// public class FileSaveUtil { public enum AppPath { /// The directory path where you can store data that you want to be kept between runs. PersistentDataPath = 0, /// The directory path where temporary data can be stored. TemporaryCachePath, /// The folder located at /Assets/StreamingAssets in the project. (Not work with System.IO methods when running on Android/WebGL) StreamingAssetsPath, /// The folder located at /Assets in the project. (Work on the Unity editor only) DataPath } public string GetAppPath(AppPath appPath) { string directory = ""; switch (appPath) { case AppPath.PersistentDataPath: directory = Application.persistentDataPath; break; case AppPath.TemporaryCachePath: directory = Application.temporaryCachePath; break; case AppPath.StreamingAssetsPath: directory = Application.streamingAssetsPath; break; case AppPath.DataPath: directory = Application.dataPath; break; } return directory; } #region ----- Instance ----- private static FileSaveUtil _instance = null; public static FileSaveUtil Instance { get { if (_instance == null) _instance = new FileSaveUtil(); return _instance; } } #endregion #region ----- Save Texture2D as EXR ----- /// /// Saves Texture2D as EXR to accessible path (e.g. Application data path). /// public string SaveTextureAsEXR(Texture2D texture2D, string directory, string fileNameWithoutExtension, Texture2D.EXRFlags exrFlags = Texture2D.EXRFlags.None) { return SaveBytes(texture2D.EncodeToEXR(exrFlags), directory, fileNameWithoutExtension + ".exr"); } /// /// Saves Texture2D as JPG to Application data path. /// public string SaveTextureAsEXR(Texture2D texture2D, AppPath appPath, string subFolderName, string fileNameWithoutExtension, Texture2D.EXRFlags exrFlags = Texture2D.EXRFlags.None) { string directory = GetAppPath(appPath); if (!string.IsNullOrEmpty(subFolderName)) directory = Path.Combine(directory, subFolderName); return SaveTextureAsEXR(texture2D, directory, fileNameWithoutExtension, exrFlags); } /// /// Saves Texture2D as JPG to Windows/Mac special folder. /// public string SaveTextureAsEXR(Texture2D texture2D, Environment.SpecialFolder specialFolder, string subFolderName, string fileNameWithoutExtension, Texture2D.EXRFlags exrFlags = Texture2D.EXRFlags.None) { string directory = Environment.GetFolderPath(specialFolder); if (!string.IsNullOrEmpty(subFolderName)) directory = Path.Combine(directory, subFolderName); return SaveTextureAsEXR(texture2D, directory, fileNameWithoutExtension, exrFlags); } #endregion #region ----- Save Texture2D as JPG ----- /// /// Saves Texture2D as JPG to accessible path (e.g. Application data path). /// public string SaveTextureAsJPG(Texture2D texture2D, string directory, string fileNameWithoutExtension, int quality = 90) { return SaveBytes(texture2D.EncodeToJPG(quality), directory, fileNameWithoutExtension + ".jpg"); } /// /// Saves Texture2D as JPG to Application data path. /// public string SaveTextureAsJPG(Texture2D texture2D, AppPath appPath, string subFolderName, string fileNameWithoutExtension, int quality = 90) { string directory = GetAppPath(appPath); if (!string.IsNullOrEmpty(subFolderName)) directory = Path.Combine(directory, subFolderName); return SaveTextureAsJPG(texture2D, directory, fileNameWithoutExtension, quality); } /// /// Saves Texture2D as JPG to Windows/Mac special folder. /// public string SaveTextureAsJPG(Texture2D texture2D, Environment.SpecialFolder specialFolder, string subFolderName, string fileNameWithoutExtension, int quality = 90) { string directory = Environment.GetFolderPath(specialFolder); if (!string.IsNullOrEmpty(subFolderName)) directory = Path.Combine(directory, subFolderName); return SaveTextureAsJPG(texture2D, directory, fileNameWithoutExtension, quality); } #endregion #region ----- Save Texture2D as PNG ----- /// /// Saves Texture2D as PNG to accessible path (e.g. Application data path). /// public string SaveTextureAsPNG(Texture2D texture2D, string directory, string fileNameWithoutExtension) { return SaveBytes(texture2D.EncodeToPNG(), directory, fileNameWithoutExtension + ".png"); } /// /// Saves Texture2D as PNG to Application data path. /// public string SaveTextureAsPNG(Texture2D texture2D, AppPath appPath, string subFolderName, string fileNameWithoutExtension) { string directory = GetAppPath(appPath); if (!string.IsNullOrEmpty(subFolderName)) directory = Path.Combine(directory, subFolderName); return SaveTextureAsPNG(texture2D, directory, fileNameWithoutExtension); } /// /// Saves Texture2D as PNG to Windows/Mac special folder. /// public string SaveTextureAsPNG(Texture2D texture2D, Environment.SpecialFolder specialFolder, string subFolderName, string fileNameWithoutExtension) { string directory = Environment.GetFolderPath(specialFolder); if (!string.IsNullOrEmpty(subFolderName)) directory = Path.Combine(directory, subFolderName); return SaveTextureAsPNG(texture2D, directory, fileNameWithoutExtension); } #endregion #region ----- Save byte array to File ----- /// /// Saves file byte array to accessible path (e.g. Application data path). /// public string SaveBytes(byte[] bytes, string directory, string fileNameWithExtension) { if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); string savePath = Path.Combine(directory, fileNameWithExtension); File.WriteAllBytes(savePath, bytes); return savePath; } /// /// Saves file byte array to Application data path. /// public string SaveBytes(byte[] bytes, AppPath appPath, string subFolderName, string fileNameWithExtension) { string directory = GetAppPath(appPath); return SaveBytes(bytes, directory, fileNameWithExtension); } /// /// Saves file byte array to Windows/Mac special folder. /// public string SaveBytes(byte[] bytes, Environment.SpecialFolder specialFolder, string subFolderName, string fileNameWithExtension) { string directory = Path.Combine(Environment.GetFolderPath(specialFolder), subFolderName); return SaveBytes(bytes, directory, fileNameWithExtension); } /// /// Saves file byte array to Windows/Mac special folder. /// public string SaveBytes(byte[] bytes, Environment.SpecialFolder specialFolder, string fileNameWithExtension) { string directory = Environment.GetFolderPath(specialFolder); return SaveBytes(bytes, directory, fileNameWithExtension); } #endregion } }