public static bool ExportImage(UPAImage img, TextureType type, TextureExtension extension) { string path = EditorUtility.SaveFilePanel( "Export image as " + extension.ToString(), "Assets/", img.name + "." + extension.ToString().ToLower(), extension.ToString().ToLower()); if (path.Length == 0) return false; byte[] bytes; if (extension == TextureExtension.PNG) { // Encode texture into PNG bytes = img.GetFinalImage(true).EncodeToPNG(); } else { // Encode texture into JPG #if UNITY_4_2 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_3 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_5 bytes = img.GetFinalImage(true).EncodeToJPG(); #else bytes = img.GetFinalImage(true).EncodeToJPG(); #endif } path = FileUtil.GetProjectRelativePath(path); //Write to a file in the project folder File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (type == TextureType.texture) texImp.textureType = TextureImporterType.Image; else if (type == TextureType.sprite) { texImp.textureType = TextureImporterType.Sprite; #if UNITY_4_2 texImp.spritePixelsToUnits = 10; #elif UNITY_4_3 texImp.spritePixelsToUnits = 10; #elif UNITY_4_5 texImp.spritePixelsToUnits = 10; #else texImp.spritePixelsPerUnit = 10; #endif } texImp.filterMode = FilterMode.Point; texImp.textureFormat = TextureImporterFormat.AutomaticTruecolor; AssetDatabase.ImportAsset(path); return true; }
public static bool ExportImage(UPAImage img, TextureType type, TextureExtension extension) { string path = EditorUtility.SaveFilePanel( "Export image as " + extension.ToString(), "Assets/", img.name + "." + extension.ToString().ToLower(), extension.ToString().ToLower()); if (path.Length == 0) { return(false); } byte[] bytes; if (extension == TextureExtension.PNG) { // Encode texture into PNG bytes = img.GetFinalImage(true).EncodeToPNG(); } else { // Encode texture into JPG #if UNITY_4_2 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_3 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_5 bytes = img.GetFinalImage(true).EncodeToJPG(); #else bytes = img.GetFinalImage(true).EncodeToJPG(); #endif } path = FileUtil.GetProjectRelativePath(path); //Write to a file in the project folder File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (type == TextureType.texture) { texImp.textureType = TextureImporterType.Default; } else if (type == TextureType.sprite) { texImp.textureType = TextureImporterType.Sprite; #if UNITY_4_2 texImp.spritePixelsToUnits = 10; #elif UNITY_4_3 texImp.spritePixelsToUnits = 10; #elif UNITY_4_5 texImp.spritePixelsToUnits = 10; #else texImp.spritePixelsPerUnit = 10; #endif } texImp.filterMode = FilterMode.Point; texImp.textureFormat = TextureImporterFormat.AutomaticTruecolor; AssetDatabase.ImportAsset(path); return(true); }
public override void OnInspectorGUI() { UPAImage img = (UPAImage)target; GUILayout.BeginArea(new Rect(5, 53, Screen.width - 10, Screen.height)); if (GUILayout.Button("Open", GUILayout.Height(40))) { UPAEditorWindow.CurrentImg = UPASession.OpenImageByAsset(img); if (UPAEditorWindow.window != null) { UPAEditorWindow.window.Repaint(); } } if (GUILayout.Button("Export", GUILayout.Height(40))) { UPAExportWindow.Init(img); } GUILayout.EndArea(); //Make sure the textures are loaded img.LoadAllTexsFromMaps(); float ratio = (float)img.width / (float)img.height; EditorGUI.DrawTextureTransparent(new Rect(5, 150, Screen.width - 10, (Screen.width - 10) * ratio), img.GetFinalImage(true), ScaleMode.ScaleToFit, 0); }
public static bool ExportImage(UPAImage img, TextureType type, TextureExtension extension) { var folder = "Assets/"; var fileName = img.name + "." + extension.ToString().ToLower(); if (PlayerPrefs.HasKey("pixelfile")) { folder = PlayerPrefs.GetString("pixelfolder"); fileName = PlayerPrefs.GetString("pixelfile"); } string path = EditorUtility.SaveFilePanel( "Export image as " + extension.ToString(), folder, fileName, extension.ToString().ToLower()); if (path.Length == 0) { return(false); } byte[] bytes; if (extension == TextureExtension.PNG) { // Encode texture into PNG bytes = img.GetFinalImage(true).EncodeToPNG(); } else { // Encode texture into JPG #if UNITY_4_2 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_3 bytes = img.GetFinalImage(true).EncodeToPNG(); #elif UNITY_4_5 bytes = img.GetFinalImage(true).EncodeToJPG(); #else bytes = img.GetFinalImage(true).EncodeToJPG(); #endif } path = FileUtil.GetProjectRelativePath(path); PlayerPrefs.SetString("pixeldir", Path.GetDirectoryName(path)); PlayerPrefs.SetString("pixelfile", Path.GetFileName(path)); PlayerPrefs.Save(); //Write to a file in the project folder File.WriteAllBytes(path, bytes); AssetDatabase.Refresh(); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (type == TextureType.texture) { texImp.textureType = TextureImporterType.Default; } else if (type == TextureType.sprite) { texImp.textureType = TextureImporterType.Sprite; #if UNITY_4_2 texImp.spritePixelsToUnits = 10; #elif UNITY_4_3 texImp.spritePixelsToUnits = 10; #elif UNITY_4_5 texImp.spritePixelsToUnits = 10; #else texImp.spritePixelsPerUnit = 32; #endif } texImp.filterMode = FilterMode.Point; texImp.textureCompression = TextureImporterCompression.Uncompressed; AssetDatabase.ImportAsset(path); return(true); }