/// <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
        }
예제 #2
0
        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
        }