效果说明:
>根据提示填写相关参数,先“选择保存路径”,再“生成渐变图”即可生成相应尺寸的渐变图。
界面展示:
代码实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class DrawRamp : EditorWindow
{
private Vector2 windowSize = new Vector2(370,500);//窗口大小
[Header("渐变")]
private Gradient gradient = new Gradient();//渐变
[Header("输出图像尺寸")]
private Vector2 resolution = new Vector2(256, 8);//分辨率大小
Color[] rampColor;//
[Header("Alpha通道?")]
bool isAlpha;//是否需要A通道
[Header("输出名称")]
private string texName = "rampTex";//名称
private int serial = 1;
private float[] gaodus;
[Header("保存路径")]
private string path = "请先选择好保存路径再生成哦~";//保存路径
[MenuItem("Tools/渐变图制作")]
static void RampCreateWindow()
{
DrawRamp window = EditorWindow.GetWindow<DrawRamp>("渐变图制作");
window.minSize = window.windowSize;
window.maxSize = window.windowSize;
window.Show();
}
private void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField("在Unity中创建并保存一个自定义渐变贴图√ 冲!");
gradient = EditorGUILayout.GradientField("Gradient(渐变)", gradient, GUILayout.Width(350));
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PrefixLabel("设置输出分辨率");
resolution = EditorGUILayout.Vector2Field(GUIContent.none, resolution);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("分辨率 128*8", GUILayout.Width(100)))
resolution = new Vector2(128, 8);
if (GUILayout.Button("分辨率 256*8", GUILayout.Width(100)))
resolution = new Vector2(256, 8);
if (GUILayout.Button("分辨率 512*8",GUILayout.Width(100)))
resolution = new Vector2(512, 8);
EditorGUILayout.EndHorizontal();
EditorGUILayout.Space();
isAlpha = EditorGUILayout.ToggleLeft("输出图 是否包含Alpha通道", isAlpha);
texName = EditorGUILayout.TextField("输出渐变贴图命名:", texName);
EditorGUILayout.Space();
EditorGUILayout.LabelField("当前保存路径为:");
if(string.IsNullOrEmpty(path.Trim()))
{
path = "路径不能为空哦~请再选择一次√";
}
EditorGUILayout.LabelField(path);
EditorGUILayout.BeginHorizontal();
bool ispath = GUILayout.Button(">-选择保存路径-<", GUILayout.Width(110));
if (ispath)
{
SetPath();
Debug.Log("你的保存路径为:" + path);
}
bool isCreated = GUILayout.Button(">-生成渐变图-<", GUILayout.Width(110));
if (isCreated)
{
OutputRampTex();
}
EditorGUILayout.EndHorizontal();
}
void SetPath()//选择保存路径
{
path = EditorUtility.OpenFolderPanel("", "", "");
}
void OutputRampTex()//输出渐变贴图(关键算法)
{
rampColor = new Color[(int)(resolution.x * resolution.y)];
if (isAlpha == false){
gaodus = new float[(int)resolution.y];
gaodus[0] = 0;
float gao = 0;
for (int g = 0; g < resolution.y; g++)
{
if (g == 0){}
else{
gao += resolution.x;
gaodus[g] = gao;
}
}
for (int a = 0; a < resolution.y; a++)
{
for (int c = 0; c < resolution.x; c++)
{
float temp = c / resolution.x;
rampColor[(int)gaodus[a] + c] = gradient.Evaluate(temp);
}
}
}
else{
gaodus = new float[(int)resolution.y];
gaodus[0] = 0;
float gao = 0;
for (int g = 0; g < resolution.y; g++)
{
if (g == 0){}
else{
gao += resolution.x;
gaodus[g] = gao;
}
}
for (int a = 0; a < resolution.y; a++)
{
for (int c = 0; c < resolution.x; c++)
{
float temp = c / resolution.x;
rampColor[(int)gaodus[a] + c] = gradient.Evaluate(temp);
rampColor[(int)gaodus[a] + c].a = gradient.Evaluate(temp).a;
}
}
}
Save(rampColor);
Debug.Log("Ramp图已生成," + "名称:" + texName + ",保存路径:" + path);
}
void Save(Color[] colors)//保存渐变图
{
TextureFormat _texFormat;
if(isAlpha){
_texFormat = TextureFormat.ARGB32;
}else{
_texFormat = TextureFormat.RGB24;
}
Texture2D tex = new Texture2D((int)resolution.x, (int)resolution.y, _texFormat, false);
tex.SetPixels(colors);
tex.Apply();//应用更改
byte[] bytes = tex.EncodeToPNG();
string sname = texName + "_" + serial;
serial += 1;
File.WriteAllBytes(path + "/" + sname + ".png", bytes);
AssetDatabase.Refresh();
}
}