diff --git a/Assets/MeshUtility/Editor/TextureSaver.cs b/Assets/MeshUtility/Editor/TextureSaver.cs new file mode 100644 index 000000000..cffbd39f0 --- /dev/null +++ b/Assets/MeshUtility/Editor/TextureSaver.cs @@ -0,0 +1,51 @@ +using System.IO; +using UnityEditor; +using UnityEngine; + +namespace MeshUtility +{ + public static class EditorChangeTextureType + { + [MenuItem("Assets/SaveAsPng", true)] + static bool IsTextureAsset() + { + return Selection.activeObject is Texture2D; + } + + [MenuItem("Assets/SaveAsPng")] + static void SaveAsPng() + { + var texture = Selection.activeObject as Texture2D; + var path = SaveDialog(texture.name); + if (string.IsNullOrEmpty(path)) + { + return; + } + + File.WriteAllBytes(path, texture.EncodeToPNG()); + Debug.Log($"save: ${path}"); + } + + private static string m_lastExportDir; + static string SaveDialog(string name) + { + string directory; + if (string.IsNullOrEmpty(m_lastExportDir)) + directory = Directory.GetParent(Application.dataPath).ToString(); + else + directory = m_lastExportDir; + + // save dialog + var path = EditorUtility.SaveFilePanel( + "Save png", + directory, + $"{name}.png", + "png"); + if (!string.IsNullOrEmpty(path)) + { + m_lastExportDir = Path.GetDirectoryName(path).Replace("\\", "/"); + } + return path; + } + } +} diff --git a/Assets/MeshUtility/Editor/TextureSaver.cs.meta b/Assets/MeshUtility/Editor/TextureSaver.cs.meta new file mode 100644 index 000000000..733e8bdc1 --- /dev/null +++ b/Assets/MeshUtility/Editor/TextureSaver.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 01c8541cb3fd27f4882e3d32c37a45aa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: