using System;
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
[CustomEditor(typeof(EditorTest))]
public class EditorTest : EditorWindow
{
[UnityEditor.MenuItem("Tool/colorToGradient")]
public static void ConfigDialog()
{
EditorWindow.GetWindow(typeof(EditorTest));
}
private string _savePath;
private Texture2D tex;
private Texture2D texture1;
void OnGUI()
{
if (GUILayout.Button("select TerrainColor"))
{
}
if (GUILayout.Button("generate and save"))
{
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 = Shader.Find("Unlit/ColorToGradient");
Material mt = new Material(sd);
mt.SetTexture("_MainTex",tex);
mt.SetTexture("_MainTex1",texture1);
Graphics.Blit(source, destination, mt);
return destination;
}
}