public static string ParsePath(DestinationFolder destinationFolder, string customPath) { string path = ""; #if !UNITY_EDITOR && UNITY_ANDROID // path = AndroidUtils.GetExternalPictureDirectory() ; // path = AndroidUtils.GetExternalFilesDir() ; path = AndroidUtils.GetFirstAvailableMediaStorage() + "/" + customPath; #elif !UNITY_EDITOR && UNITY_IOS path = Application.persistentDataPath + "/" + customPath; #else if (destinationFolder == DestinationFolder.CUSTOM_FOLDER) { path = customPath; } else if (destinationFolder == DestinationFolder.PERSISTENT_DATA_PATH) { // #if UNITY_EDITOR || UNITY_STANDALONE // path = Application.persistentDataPath + "/" + customPath; // #elif UNITY_ANDROID // path = AndroidUtils.GetFirstAvailableMediaStorage() + "/" + customPath; // #else path = Application.persistentDataPath + "/" + customPath; // #endif } else if (destinationFolder == DestinationFolder.DATA_PATH) { path = Application.dataPath + "/" + customPath; } else if (destinationFolder == DestinationFolder.PICTURES_FOLDER) { #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_WSA_10_0 path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures) + "/" + customPath; // #elif UNITY_ANDROID // path = AndroidUtils.GetExternalPictureDirectory() + "/" + customPath; // #elif UNITY_IOS // path = Application.persistentDataPath + "/" +customPath; #else path = Application.persistentDataPath + "/" + customPath; #endif } #endif // Add a slash if not already at the end of the folder name if (path.Length > 0) { path = path.Replace("//", "/"); if (path[path.Length - 1] != '/' && path[path.Length - 1] != '\\') { path += "/"; } } return(path); }
public static string GetExternalPictureDirectory() { if (IsPrimaryStorageAvailable()) { return(GetPrimaryStorage() + "/" + AndroidUtils.GetPictureFolder()); } else { return(GetFirstAvailableMediaStorage()); } }
public string GetPath() { string path = ""; if (m_Config.m_DestinationFolder == ScreenshotConfig.DestinationFolder.CUSTOM_FOLDER) { path = m_Config.m_CustomRootedPath; } else if (m_Config.m_DestinationFolder == ScreenshotConfig.DestinationFolder.PERSISTENT_DATA_PATH) { #if UNITY_EDITOR || UNITY_STANDALONE path = Application.persistentDataPath + "/" + m_Config.m_CustomRelativePath; #elif UNITY_ANDROID path = AndroidUtils.GetFirstAvailableMediaStorage() + "/" + m_Config.m_CustomRelativePath; #else path = Application.persistentDataPath + "/" + m_Config.m_CustomRelativePath; #endif } else if (m_Config.m_DestinationFolder == ScreenshotConfig.DestinationFolder.DATA_PATH) { path = Application.dataPath + "/" + m_Config.m_CustomRelativePath; } else if (m_Config.m_DestinationFolder == ScreenshotConfig.DestinationFolder.PICTURES_FOLDER) { #if UNITY_EDITOR || UNITY_STANDALONE path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures) + "/" + m_Config.m_CustomRelativePath; #elif UNITY_ANDROID path = AndroidUtils.GetExternalPictureDirectory() + "/" + m_Config.m_CustomRelativePath; #elif UNITY_IOS path = Application.persistentDataPath + "/" + m_Config.m_CustomRelativePath; #else path = Application.persistentDataPath + "/" + m_Config.m_CustomRelativePath; #endif } // Add a slash if not already at the end of the folder name if (path.Length > 0) { path = path.Replace("//", "/"); if (path [path.Length - 1] != '/' && path [path.Length - 1] != '\\') { path += "/"; } } return(path); }
/// <summary> /// Exports to file. /// </summary> /// <returns><c>true</c>, if to file was exported, <c>false</c> otherwise.</returns> /// <param name="texture">Texture.</param> The texture to export. /// <param name="fullpath">Filename.</param> The filename must be a valid full path. Use the ScreenshotNameParser to get a valid path. /// <param name="imageFormat">Image format.</param> /// <param name="JPGQuality">JPG quality.</param> public static bool ExportToFile(Texture2D texture, string fullpath, ImageFileFormat imageFormat, int JPGQuality = 70, bool addToGallery = true) { #if UNITY_ANDROID && !UNITY_EDITOR && IGNORE_ANDROID_SCREENSHOT return(false); #endif #if UNITY_IOS && !UNITY_EDITOR && IGNORE_IOS_SCREENSHOT return(false); #endif if (texture == null) { Debug.LogError("Can not export the texture to file " + fullpath + ", texture is empty."); return(false); } #if UNITY_WEBPLAYER Debug.Log("WebPlayer is not supported."); return(false); #else // Convert texture to bytes byte[] bytes = null; if (imageFormat == ImageFileFormat.JPG) { bytes = texture.EncodeToJPG(JPGQuality); } else { bytes = texture.EncodeToPNG(); } #endif #if !UNITY_EDITOR && UNITY_WEBGL // Create a downloadable image for the web browser try { string shortFileName = fullpath; int index = fullpath.LastIndexOf('/'); if (index >= 0) { shortFileName = fullpath.Substring(index + 1); } string format = (imageFormat == ImageFileFormat.JPG) ? "jpeg" : "png"; WebGLUtils.ExportImage(bytes, shortFileName, format); } catch { Debug.LogError("Failed to create downloadable image."); return(false); } // #if !UNITY_EDITOR && UNITY_WEBGL // AndroidJavaObject classMedia = new AndroidJavaObject ("java.io.File", fullpath); #elif !UNITY_WEBPLAYER // Create the directory if (!PathUtils.CreateExportDirectory(fullpath)) { return(false); } // Export the image try { System.IO.File.WriteAllBytes(fullpath, bytes); } catch { Debug.LogError("Failed to create the file : " + fullpath); return(false); } #endif if (addToGallery) { #if UNITY_ANDROID && !UNITY_EDITOR // Update android gallery try { AndroidUtils.AddImageToGallery(fullpath); } catch { Debug.LogError("Failed to update Android Gallery"); return(false); } #elif UNITY_IOS && !UNITY_EDITOR // Update ios gallery try { iOsUtils.AddImageToGallery(fullpath); } catch { Debug.LogError("Failed to update iOS Gallery"); return(false); } #endif } #if !UNITY_WEBPLAYER return(true); #endif }
public static bool ExportToFile(Texture2D texture, string filename, ImageFormat imageFormat, int JPGQuality = 70) { if (texture == null) { Debug.LogError("Texture is null."); return(false); } #if UNITY_WEBPLAYER Debug.Log("WebPlayer is not supported."); return(false); #else // Convert texture to bytes byte[] bytes = null; if (imageFormat == ImageFormat.JPG) { bytes = texture.EncodeToJPG(JPGQuality); } else { bytes = texture.EncodeToPNG(); } #endif #if UNITY_WEBGL && !UNITY_EDITOR // Create a downloadable image for the web browser try { string shortFileName = filename; int index = filename.LastIndexOf('/'); if (index >= 0) { shortFileName = filename.Substring(index + 1); } string format = (imageFormat == ImageFormat.JPG) ? "jpeg" : "png"; WebGLUtils.ExportImage(bytes, shortFileName, format); } catch { Debug.LogError("Failed to create downloadable image."); return(false); } #elif !UNITY_WEBPLAYER // Create the directory if (!CreateExportDirectory(filename)) { return(false); } // Export the image try { System.IO.File.WriteAllBytes(filename, bytes); } catch { Debug.LogError("Failed to create the file : " + filename); return(false); } #endif #if UNITY_ANDROID && !UNITY_EDITOR // Update android gallery try { AndroidUtils.AddImageToGallery(filename); } catch { Debug.LogError("Failed to update Android Gallery"); return(false); } #elif UNITY_IOS && !UNITY_EDITOR // Update ios gallery try { iOsUtils.AddImageToGallery(filename); } catch { Debug.LogError("Failed to update iOS Gallery"); return(false); } #endif #if !UNITY_WEBPLAYER return(true); #endif }