-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateColorMaterialWindow.cs
139 lines (131 loc) · 3.8 KB
/
CreateColorMaterialWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using npScripts;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEditor;
using UnityEngine;
public class CreateColorMaterialWindow : EditorWindow
{
struct NameIndex
{
public string name;
public int index;
public NameIndex(string n, int i)
{
name = n;
index = i;
}
}
#region Fields
static List<string> colors = new List<string>();
static Dictionary<ColorNameGroup, List<NameIndex>> colorDict = new Dictionary<ColorNameGroup, List<NameIndex>>();
public string colorFilePath;
public List<ColorData> colorDatas = new List<ColorData>();
public Color materialColor = Color.red;
public string materialName;
public bool usePremade = false;
public enum ColorNameGroup { All, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z }
public int premadeIndex;
public Shader shader;
Vector2 scrollPoint;
ColorNameGroup colorGroup;
public ColorData.ColorDataType colorType = ColorData.ColorDataType.RGB;
bool [] foldOuts = new bool [26];
#endregion
[MenuItem("nonPareil/Color Material Creator")]
internal static void Init()
{
// Get existing open window or if none, make a new one:
CreateColorMaterialWindow window = (CreateColorMaterialWindow)EditorWindow.GetWindow(typeof(CreateColorMaterialWindow));
window.Show();
}
public bool DrawColor(int index)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField(colorDatas [index].name);
EditorGUILayout.ColorField(colorDatas [index].CreateColor());
if (GUILayout.Button("Use"))
{
colorDatas [index].colorType = colorType;
materialColor = colorDatas [index].CreateColor();
EditorGUILayout.EndHorizontal();
return true;
}
EditorGUILayout.EndHorizontal();
return false;
}
void LoadColors()
{
string path = EditorUtility.OpenFilePanel("Save new spline profile", "Assets", "xml");
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
//Read the file.
colorDatas.Clear();
using (XmlReader reader = XmlReader.Create(path, settings))
{
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.Name == "Color")
{
ColorData cd = new ColorData();
cd.ReadFromXML(reader);
cd.colorType = ColorData.ColorDataType.RGB;
colorDatas.Add(cd);
}
}
}
}
}
private void OnGUI()
{
colorType = (ColorData.ColorDataType)EditorGUILayout.EnumPopup("Color Format", colorType);
materialColor = EditorGUILayout.ColorField("Color", materialColor);
shader = (Shader)EditorGUILayout.ObjectField("Shader", shader, typeof(Shader), false);
usePremade = EditorGUILayout.Toggle("Use Premade Color", usePremade);
if (GUILayout.Button("Load Color File"))
{
LoadColors();
}
if (GUILayout.Button("Save"))
{
string path = EditorUtility.SaveFilePanelInProject("Save material", "NEW mat.mat", "mat", "Please enter a file name to save the material");
if (!string.IsNullOrEmpty(path))
{
Material mat = new Material(shader);
mat.color = materialColor;
AssetDatabase.CreateAsset(mat, path);
}
}
if (usePremade)
{
scrollPoint = EditorGUILayout.BeginScrollView(scrollPoint);
char currentC = ' ';
int currentFoldIndex = -1;
for (int i = 0; i < colorDatas.Count; i++)
{
if (colorDatas [i].name.Length != 0)
{
if (colorDatas [i].name.ToLower() [0] != currentC)
{
currentC = colorDatas [i].name.ToLower() [0];
currentFoldIndex++;
foldOuts [currentFoldIndex] = EditorGUILayout.Foldout(foldOuts [currentFoldIndex], (currentC + "").ToUpper());
}
if (foldOuts [currentFoldIndex])
{
EditorGUI.indentLevel++;
if (DrawColor(i))
{
materialColor = colorDatas [i].CreateColor();//TODO fix bug
}
EditorGUI.indentLevel--;
}
}
}
EditorGUILayout.EndScrollView();
}
}
}