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)
{
//如果没有选择图片,则显示对话框
EditorUtility.DisplayDialog("Select Texture",
"You Must Select a Texture first!",
"Ok");
return;
}
//显示保存文件面板,path为得到的选择位置的本地路径
var path = EditorUtility.SaveFilePanelInProject("Save texture as PNG",
texture.name + ".png",
"png",
"Please enter a file name to save the texture to");
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);
// As we are saving to the asset folder, tell Unity to scan for modified or new assets
AssetDatabase.Refresh();
}
}
}
}