AFX_RampCreate.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. public class AFX_RampCreate : EditorWindow
  6. {
  7. public string tex2dName = "AFX_Ramp";
  8. public Texture tex;
  9. public Gradient gradient = new();
  10. public ParticleSystem particle;
  11. public TrailRenderer trail;
  12. public LineRenderer line;
  13. public string path = ">>还未选择保存路径";
  14. public string texname = "AFX_Ramp";
  15. public int serial = 1;
  16. public Vector2 resolution = new(256, 8);
  17. public float[] gaodus;
  18. private Color[] colorall;
  19. private bool isAlpha;
  20. private int valueRampsource = 4;
  21. private void OnGUI()
  22. {
  23. EditorGUILayout.Space();
  24. EditorGUILayout.LabelField("搞一个新的渐变Gradient");
  25. gradient = EditorGUILayout.GradientField("Gradient", gradient);
  26. EditorGUILayout.BeginHorizontal();
  27. EditorGUILayout.PrefixLabel("随后设置输出分辨率");
  28. resolution = EditorGUILayout.Vector2Field(GUIContent.none, resolution);
  29. EditorGUILayout.EndHorizontal();
  30. if (GUILayout.Button("设为 256*8"))
  31. resolution = new Vector2(256, 8);
  32. if (GUILayout.Button("设为 512*8"))
  33. resolution = new Vector2(512, 8);
  34. EditorGUILayout.Space();
  35. isAlpha = EditorGUILayout.ToggleLeft("<<勾选 以导出包含Alpha通道的图", isAlpha);
  36. var style = new GUIStyle("textfield");
  37. tex2dName = EditorGUILayout.TextField("输出文件命名:", tex2dName, style);
  38. EditorGUILayout.Space();
  39. EditorGUILayout.LabelField("当前保存路径为:");
  40. EditorGUILayout.LabelField(path);
  41. var ispath = GUILayout.Button("--选择保存路径--");
  42. if (ispath)
  43. {
  44. SetPath1();
  45. Debug.Log("你的保存路径为:" + path);
  46. }
  47. var shengcheng = GUILayout.Button("--生成渐变图--");
  48. if (shengcheng) OutRampTex();
  49. EditorGUILayout.Space(20);
  50. EditorGUILayout.LabelField("---------------------------------------------------------");
  51. if (GUILayout.Button("打开导出文件夹")) Application.OpenURL("file://" + path);
  52. EditorGUILayout.Space(20);
  53. if (GUILayout.Button("查看使用教程(视频)"))
  54. Application.OpenURL("https://space.bilibili.com/7234711/channel/detail?cid=112022");
  55. }
  56. [MenuItem("ADYFX/渐变图生成工具")]
  57. private static void RampCreateWindow()
  58. {
  59. var window = GetWindow<AFX_RampCreate>();
  60. window.minSize = new Vector2(350, 600);
  61. window.titleContent = new GUIContent("渐变图生成工具");
  62. window.Show();
  63. }
  64. private void SetPath1()
  65. {
  66. path = EditorUtility.OpenFolderPanel("", "", "");
  67. }
  68. private void OutRampTex()
  69. {
  70. colorall = new Color[(int)(resolution.x * resolution.y)];
  71. if (isAlpha == false)
  72. {
  73. gaodus = new float[(int)resolution.y];
  74. gaodus[0] = 0;
  75. float gao = 0;
  76. for (var g = 0; g < resolution.y; g++)
  77. if (g == 0)
  78. {
  79. }
  80. else
  81. {
  82. gao += resolution.x;
  83. gaodus[g] = gao;
  84. }
  85. for (var a = 0; a < resolution.y; a++)
  86. for (var c = 0; c < resolution.x; c++)
  87. {
  88. var temp = c / resolution.x;
  89. colorall[(int)gaodus[a] + c] = gradient.Evaluate(temp);
  90. }
  91. }
  92. else
  93. {
  94. gaodus = new float[(int)resolution.y];
  95. gaodus[0] = 0;
  96. float gao = 0;
  97. for (var g = 0; g < resolution.y; g++)
  98. if (g == 0)
  99. {
  100. }
  101. else
  102. {
  103. gao += resolution.x;
  104. gaodus[g] = gao;
  105. }
  106. for (var a = 0; a < resolution.y; a++)
  107. for (var c = 0; c < resolution.x; c++)
  108. {
  109. var temp = c / resolution.x;
  110. colorall[(int)gaodus[a] + c] = gradient.Evaluate(temp);
  111. colorall[(int)gaodus[a] + c].a = gradient.Evaluate(temp).a;
  112. }
  113. }
  114. Save(colorall);
  115. Debug.Log("Ramp图已生成," + "名称:" + tex2dName + ",保存路径:" + path);
  116. }
  117. private void Save(Color[] colors)
  118. {
  119. TextureFormat _texFormat;
  120. if (isAlpha)
  121. _texFormat = TextureFormat.ARGB32;
  122. else
  123. _texFormat = TextureFormat.RGB24;
  124. var tex = new Texture2D((int)resolution.x, (int)resolution.y, _texFormat, false);
  125. tex.SetPixels(colors);
  126. tex.Apply();
  127. byte[] bytes;
  128. bytes = tex.EncodeToPNG();
  129. var sname = tex2dName + "_" + serial;
  130. serial += 1;
  131. File.WriteAllBytes(path + "/" + sname + ".png", bytes);
  132. AssetDatabase.Refresh();
  133. }
  134. }
  135. #endif