Example #1
0
        private void Export2D()
        {
            Texture2D texture = null;

            try
            {
                string path = EditorUtility.SaveFilePanel("Export Noise To Texture2D",
                                                          Application.dataPath,
                                                          "New Noise Texture2D.png",
                                                          "png");

                if (!path.StartsWith(Application.dataPath))
                {
                    Debug.LogError("You must specificy a path in your project's Assets folder to export a Noise Texture");
                }

                if (!string.IsNullOrEmpty(path))
                {
                    EditorUtility.DisplayProgressBar("Exporting Noise to Texture2D", "Making some noise...", 0.1f);

                    texture = NoiseUtils.BakeToTexture2D(m_noise, dims2D.x, dims2D.y, m_format, TextureCreationFlags.None);

                    byte[] bytes = ImageConversion.EncodeToPNG(texture);

                    System.IO.File.WriteAllBytes(path, bytes);

                    Texture2D.DestroyImmediate(texture);
                    texture = null;

                    string assetPath = path.Remove(0, Application.dataPath.Length - "Assets".Length);

                    AssetDatabase.Refresh();

                    EditorUtility.ClearProgressBar();

                    texture = AssetDatabase.LoadAssetAtPath <Texture2D>(assetPath);
                    EditorGUIUtility.PingObject(texture);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);

                if (texture != null)
                {
                    Texture2D.DestroyImmediate(texture);
                }

                Debug.Log("Exception caught");

                EditorUtility.ClearProgressBar();
            }
        }