public static bool CanShare() { #if UNITY_EDITOR return(true); #elif (UNITY_IOS || UNITY_ANDROID) return(true); #elif UNITY_WEBGL return(WebGLUtils.CanShare()); #else return(false); #endif }
public static void ShareImage(Texture2D texture, string screenshotName, string title = "", string subject = "", string description = "", TextureExporter.ImageFileFormat imageFormat = TextureExporter.ImageFileFormat.PNG, int JPGQuality = 70) { #if !UNITY_EDITOR && (UNITY_IOS || UNITY_ANDROID) string format = (imageFormat == TextureExporter.ImageFileFormat.JPG) ? "jpeg" : "png"; string filename = screenshotName + "." + format; // On iOS and Android share using Native Share var nativeShare = new NativeShare(); nativeShare.AddFile(texture, filename); nativeShare.SetTitle(title); nativeShare.SetSubject(subject); nativeShare.SetText(description); nativeShare.Share(); #elif !UNITY_EDITOR && UNITY_WEBGL // On WebGL share using custom share plugin // Convert texture to bytes byte[] bytes = null; if (imageFormat == TextureExporter.ImageFileFormat.JPG) { bytes = texture.EncodeToJPG(JPGQuality); } else { bytes = texture.EncodeToPNG(); } // Try sharing try { string format = (imageFormat == TextureExporter.ImageFileFormat.JPG) ? "jpeg" : "png"; WebGLUtils.ShareImage(bytes, screenshotName, format); } catch { Debug.LogError("Failed to share image."); } #else Debug.LogError("Share not supported on this platform."); #endif }
/// <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 }