效果说明:

>根据提示填写相关参数,先“选择保存路径”,再“生成渐变图”即可生成相应尺寸的渐变图。

界面展示:

image.png

代码实现:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. public class DrawRamp : EditorWindow
  7. {
  8. private Vector2 windowSize = new Vector2(370,500);//窗口大小
  9. [Header("渐变")]
  10. private Gradient gradient = new Gradient();//渐变
  11. [Header("输出图像尺寸")]
  12. private Vector2 resolution = new Vector2(256, 8);//分辨率大小
  13. Color[] rampColor;//
  14. [Header("Alpha通道?")]
  15. bool isAlpha;//是否需要A通道
  16. [Header("输出名称")]
  17. private string texName = "rampTex";//名称
  18. private int serial = 1;
  19. private float[] gaodus;
  20. [Header("保存路径")]
  21. private string path = "请先选择好保存路径再生成哦~";//保存路径
  22. [MenuItem("Tools/渐变图制作")]
  23. static void RampCreateWindow()
  24. {
  25. DrawRamp window = EditorWindow.GetWindow<DrawRamp>("渐变图制作");
  26. window.minSize = window.windowSize;
  27. window.maxSize = window.windowSize;
  28. window.Show();
  29. }
  30. private void OnGUI()
  31. {
  32. EditorGUILayout.Space();
  33. EditorGUILayout.LabelField("在Unity中创建并保存一个自定义渐变贴图√ 冲!");
  34. gradient = EditorGUILayout.GradientField("Gradient(渐变)", gradient, GUILayout.Width(350));
  35. EditorGUILayout.BeginHorizontal();
  36. EditorGUILayout.PrefixLabel("设置输出分辨率");
  37. resolution = EditorGUILayout.Vector2Field(GUIContent.none, resolution);
  38. EditorGUILayout.EndHorizontal();
  39. EditorGUILayout.BeginHorizontal();
  40. if (GUILayout.Button("分辨率 128*8", GUILayout.Width(100)))
  41. resolution = new Vector2(128, 8);
  42. if (GUILayout.Button("分辨率 256*8", GUILayout.Width(100)))
  43. resolution = new Vector2(256, 8);
  44. if (GUILayout.Button("分辨率 512*8",GUILayout.Width(100)))
  45. resolution = new Vector2(512, 8);
  46. EditorGUILayout.EndHorizontal();
  47. EditorGUILayout.Space();
  48. isAlpha = EditorGUILayout.ToggleLeft("输出图 是否包含Alpha通道", isAlpha);
  49. texName = EditorGUILayout.TextField("输出渐变贴图命名:", texName);
  50. EditorGUILayout.Space();
  51. EditorGUILayout.LabelField("当前保存路径为:");
  52. if(string.IsNullOrEmpty(path.Trim()))
  53. {
  54. path = "路径不能为空哦~请再选择一次√";
  55. }
  56. EditorGUILayout.LabelField(path);
  57. EditorGUILayout.BeginHorizontal();
  58. bool ispath = GUILayout.Button(">-选择保存路径-<", GUILayout.Width(110));
  59. if (ispath)
  60. {
  61. SetPath();
  62. Debug.Log("你的保存路径为:" + path);
  63. }
  64. bool isCreated = GUILayout.Button(">-生成渐变图-<", GUILayout.Width(110));
  65. if (isCreated)
  66. {
  67. OutputRampTex();
  68. }
  69. EditorGUILayout.EndHorizontal();
  70. }
  71. void SetPath()//选择保存路径
  72. {
  73. path = EditorUtility.OpenFolderPanel("", "", "");
  74. }
  75. void OutputRampTex()//输出渐变贴图(关键算法)
  76. {
  77. rampColor = new Color[(int)(resolution.x * resolution.y)];
  78. if (isAlpha == false){
  79. gaodus = new float[(int)resolution.y];
  80. gaodus[0] = 0;
  81. float gao = 0;
  82. for (int g = 0; g < resolution.y; g++)
  83. {
  84. if (g == 0){}
  85. else{
  86. gao += resolution.x;
  87. gaodus[g] = gao;
  88. }
  89. }
  90. for (int a = 0; a < resolution.y; a++)
  91. {
  92. for (int c = 0; c < resolution.x; c++)
  93. {
  94. float temp = c / resolution.x;
  95. rampColor[(int)gaodus[a] + c] = gradient.Evaluate(temp);
  96. }
  97. }
  98. }
  99. else{
  100. gaodus = new float[(int)resolution.y];
  101. gaodus[0] = 0;
  102. float gao = 0;
  103. for (int g = 0; g < resolution.y; g++)
  104. {
  105. if (g == 0){}
  106. else{
  107. gao += resolution.x;
  108. gaodus[g] = gao;
  109. }
  110. }
  111. for (int a = 0; a < resolution.y; a++)
  112. {
  113. for (int c = 0; c < resolution.x; c++)
  114. {
  115. float temp = c / resolution.x;
  116. rampColor[(int)gaodus[a] + c] = gradient.Evaluate(temp);
  117. rampColor[(int)gaodus[a] + c].a = gradient.Evaluate(temp).a;
  118. }
  119. }
  120. }
  121. Save(rampColor);
  122. Debug.Log("Ramp图已生成," + "名称:" + texName + ",保存路径:" + path);
  123. }
  124. void Save(Color[] colors)//保存渐变图
  125. {
  126. TextureFormat _texFormat;
  127. if(isAlpha){
  128. _texFormat = TextureFormat.ARGB32;
  129. }else{
  130. _texFormat = TextureFormat.RGB24;
  131. }
  132. Texture2D tex = new Texture2D((int)resolution.x, (int)resolution.y, _texFormat, false);
  133. tex.SetPixels(colors);
  134. tex.Apply();//应用更改
  135. byte[] bytes = tex.EncodeToPNG();
  136. string sname = texName + "_" + serial;
  137. serial += 1;
  138. File.WriteAllBytes(path + "/" + sname + ".png", bytes);
  139. AssetDatabase.Refresh();
  140. }
  141. }