using System;
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
//typeof(编辑器类名),继承EditorWindow
[CustomEditor(typeof(EditorTest))]
public class EditorTest : EditorWindow
{
//通过MenuItem按钮来创建这样的一个对话框
[UnityEditor.MenuItem("Tool/colorToGradient")]
public static void ConfigDialog()
{
//GetWindow创建
EditorWindow.GetWindow(typeof(EditorTest));
}
private string _savePath;
private Texture2D tex;
private Texture2D texture1;
//对话框中的各种内容通过OnGUI函数来设置
void OnGUI()
{
//Button
if (GUILayout.Button("select TerrainColor"))
{
// // OpenFileDialog dialog = new OpenFileDialog();
// // dialog.Multiselect = false;//只能选择一个文件
// // dialog.Title = "请选择图片";
// // dialog.Filter = "图像文件(*bmp;*.jpg;*.jpeg;*.png;*.tga)|*bmp;*.jpg;*.jpeg;*.png;*.tga";
// TerrainColorPath= EditorUtility.OpenFilePanel("选择一个图片", "", "tga");
//
// if (TerrainColorPath!=null)
// {
// WWW www = new WWW(TerrainColorPath);
// texture = www.texture;
// Debug.Log("打开图片存为texture");
// }
}
if (GUILayout.Button("generate and save"))
{
// tex.LoadImage(File.ReadAllBytes("Assets/textures/q1.png"));
tex = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/textures/globalmap.png");
texture1=AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/textures/grassglobalmap.png");
_savePath = "";
_savePath=EditorUtility.SaveFilePanel("保存贴图", Application.dataPath, "gradient", "png");
if (_savePath!="")
{
SaveGradient();
}
}
}
void SaveGradient()
{
RenderTexture rt = RenderImage(tex);
Texture2D png = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false);
png.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
png.Apply();
File.WriteAllBytes( _savePath,png.EncodeToPNG());
AssetDatabase.Refresh();
}
private RenderTexture RenderImage(Texture2D source)
{
RenderTexture destination=new RenderTexture(source.width, source.height, 0);
//Shader sd = AssetDatabase.LoadAssetAtPath<Shader>("Assets/shaders/ColorToGradient.shader");
Shader sd = Shader.Find("Unlit/ColorToGradient");
Material mt = new Material(sd);
mt.SetTexture("_MainTex",tex);
mt.SetTexture("_MainTex1",texture1);
Graphics.Blit(source, destination, mt);
return destination;
}
}
unity 编辑器扩展 使用shader处理并保存图片
最新推荐文章于 2024-08-12 10:39:43 发布