public void OnGUI() { var removeAtIndex = -1; for (var i = 0; i < _colorPalettes.Count; i++) { GUILayout.BeginHorizontal(); { GUILayout.Label(_colorPalettes[i].paletteName, GUILayout.Width(60)); if (GUILayout.Button("Delete")) { removeAtIndex = i; } if (GUILayout.Button("Edit")) { _currentPaletteIndex = i; } if (GUILayout.Button("Load")) { PaletteChooserWindow.setColors(_colorPalettes[i]._colors); } } GUILayout.EndHorizontal(); } if (removeAtIndex >= 0) { Undo.RecordObject(this, "Removing ColorPalette"); var currentlySelectedPalette = _currentPaletteIndex >= 0 && _currentPaletteIndex != removeAtIndex ? _colorPalettes[_currentPaletteIndex] : null; UnityEngine.Object.DestroyImmediate(_colorPalettes[removeAtIndex], true); AssetDatabase.SaveAssets(); _colorPalettes.RemoveAt(removeAtIndex); removeAtIndex = -1; if (currentlySelectedPalette != null) { _currentPaletteIndex = _colorPalettes.IndexOf(currentlySelectedPalette); } } if (_currentPaletteIndex >= 0) { GUILayout.Space(30); // ensure we stay in bounds in case of undo if (_colorPalettes.Count < _currentPaletteIndex + 1) { _currentPaletteIndex = -1; } else { _colorPalettes[_currentPaletteIndex].OnGUI(); } } GUILayout.Space(30); if (GUILayout.Button("Add New Palette")) { Undo.RecordObject(this, "Adding ColorPalette"); var p = CreateInstance <ColorPalette>(); p.paletteName = "New Palette"; _colorPalettes.Add(p); _currentPaletteIndex = _colorPalettes.Count - 1; AssetDatabase.AddObjectToAsset(p, this); } if (GUILayout.Button("New Palette from Swatch (ase or aco)")) { var path = EditorUtility.OpenFilePanel("Choose PS Swatch File to Import (ase or aco file types)", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), ""); if (path != null && path != string.Empty) { var extension = System.IO.Path.GetExtension(path).ToLower(); if (extension != ".aco" && extension != ".ase") { EditorUtility.DisplayDialog("Palette Import Error", "Only .aco and .ase files are supported", "OK"); return; } var colors = PSSwatchImporter.readPhotoShopSwatchFile(path); if (colors.Count > 0) { Undo.RecordObject(this, "Adding ColorPalette"); var p = CreateInstance <ColorPalette>(); p._colors.Clear(); p.paletteName = System.IO.Path.GetFileNameWithoutExtension(path); _colorPalettes.Add(p); // add in our colors for (var i = 0; i < colors.Count; i++) { p._colors.Add(colors[i]); } p.recalculateHexCodes(); _currentPaletteIndex = _colorPalettes.Count - 1; AssetDatabase.AddObjectToAsset(p, this); } } } if (GUILayout.Button("Save Changes")) { AssetDatabase.SaveAssets(); } GUILayout.Space(10); }
public void OnGUI() { paletteName = GUILayout.TextField(paletteName); var requiresColorPaletteUpdate = false; var removeColorAtIndex = -1; for (var i = 0; i < colors.Count; i++) { GUILayout.BeginHorizontal(); { EditorGUI.BeginChangeCheck(); _hexCodes[i] = GUILayout.TextField(_hexCodes[i], GUILayout.Width(60)); if (EditorGUI.EndChangeCheck()) { if (_hexCodes[i].Length == 7 && _hexCodes[i][0] == '#') { _hexCodes[i] = _hexCodes[i].Substring(1); } if (_hexCodes[i].Length == 6) { colors[i] = hexToColor(_hexCodes[i]); requiresColorPaletteUpdate = true; } } EditorGUI.BeginChangeCheck(); colors[i] = EditorGUILayout.ColorField(colors[i], GUILayout.ExpandWidth(true)); if (GUILayout.Button("x")) { removeColorAtIndex = i; requiresColorPaletteUpdate = true; } if (EditorGUI.EndChangeCheck()) { _hexCodes[i] = colorToHex(colors[i]); requiresColorPaletteUpdate = true; } } GUILayout.EndHorizontal(); } if (GUILayout.Button("Add Color to Palette")) { colors.Add(Color.white); _hexCodes.Add("FFFFFF"); requiresColorPaletteUpdate = true; } if (removeColorAtIndex >= 0) { colors.RemoveAt(removeColorAtIndex); _hexCodes.RemoveAt(removeColorAtIndex); } if (requiresColorPaletteUpdate) { PaletteChooserWindow.setColors(colors); } if (GUI.changed) { Undo.RecordObject(this, "ColorPalette Modified"); } }