Unity编辑器之导入导出获取路径对话框
选中一个图片,点击 “Save Texture to file”按钮
在Editor文件夹下创建脚本
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class TestEditor : EditorWindow
{
[MenuItem("Examples/Save Texture to file")]
static void Apply()
{
Texture2D texture = Selection.activeObject as Texture2D; //选中一个图片
if (texture == null)
{ //如果没选图片,显示提示对话框
EditorUtility.DisplayDialog(
"Select Texture",
"You Must Select a Texture first!",
"Ok");
return;
}
//获取路径
string path = EditorUtility.SaveFilePanel(
"Save texture as PNG",
"",
texture.name + ".png",
"png");
if (path.Length != 0)
{
// Convert the texture to a format compatible with EncodeToPNG
if (texture.format != TextureFormat.ARGB32 && texture.format != TextureFormat.RGB24)
{
Texture2D newTexture = new Texture2D(texture.width, texture.height);
newTexture.SetPixels(texture.GetPixels(0), 0);
texture = newTexture;
}
var pngData = texture.EncodeToPNG();
if (pngData != null)
File.WriteAllBytes(path, pngData);
}
}
}