/* * - m_Name: * m_Color: {r: 0.18616219, g: 0.05374134, b: 0.52205884, a: 1} * - m_Name: * m_Color: {r: 0.31163496, g: 0.45094258, b: 0.9632353, a: 1} */ public static void ExportToColorPresetLibrary(Swatch swatch) { var swatchProjectPath = AssetDatabase.GetAssetPath(swatch.GetInstanceID()); var swatchDirectory = Path.GetDirectoryName(swatchProjectPath); var libraryDirectory = swatchDirectory + "/Editor"; if (!AssetDatabase.IsValidFolder(libraryDirectory)) { AssetDatabase.CreateFolder(swatchDirectory, "Editor"); } var libraryProjectpath = libraryDirectory + "/" + swatch.name + ".colors"; var fullFileName = libraryProjectpath.Replace("Assets", Application.dataPath); string fileText = COLORS_TEMPLATE; for (int i = 0; i < swatch.colors.Length; i++) { fileText += GetYAMLForColor(swatch.colors[i]); } Debug.Log("[SwatchPresetExporter] writing to " + fullFileName); File.WriteAllText(fullFileName, fileText); AssetDatabase.ImportAsset(libraryProjectpath); }
public override void OnInspectorGUI() { Swatch swatch = (Swatch)target; EditorGUI.BeginChangeCheck(); base.OnInspectorGUI(); if (EditorGUI.EndChangeCheck()) { swatch.SignalChange(); SwatchEditorGUI.GameViewRepaint(); } if (GUILayout.Button("Add .ASE")) { var path = EditorUtility.OpenFilePanel("Swatchy Import", "", "ase"); if (path != null && path != string.Empty) { Debug.Log("[SwatchEditorGUI] path " + path); SwatchASEFile aseFile = new SwatchASEFile(path); swatch.AddColorsFromASEFile(aseFile); } } if (GUILayout.Button("Add .ASE Folder")) { var path = EditorUtility.OpenFolderPanel("Swatchy Folder Import", "", ""); //var path = EditorUtility.OpenFilePanel("Import", "", "ase"); if (path != null && path != string.Empty) { var files = Directory.GetFiles(path); for (int i = 0; i < files.Length; i++) { string file = files[i]; if (file.EndsWith(".ase")) { SwatchASEFile aseFile = new SwatchASEFile(file); swatch.AddColorsFromASEFile(aseFile); } } } } if (GUILayout.Button(replace ? "Cancel Replace" : "Replace With Another Swatch")) { replace = !replace; if (replace) { LoadOtherSwatches(); } } if (replace) { if (otherSwatchGUIDs != null && otherSwatchFilenames != null) { var lastRect = GUILayoutUtility.GetLastRect(); var indent = 50; lastRect.x += indent; lastRect.width -= indent; var spacing = 3; GUILayout.Space((EditorGUIUtility.singleLineHeight + spacing) * otherSwatchGUIDs.Length); for (int i = 0; i < otherSwatchGUIDs.Length; i++) { var swatchName = otherSwatchFilenames[i]; var buttonRect = new Rect(lastRect.x, lastRect.y + EditorGUIUtility.singleLineHeight + spacing, lastRect.width, EditorGUIUtility.singleLineHeight); lastRect = buttonRect; if (GUI.Button(buttonRect, swatchName)) { var otherSwatchAssetPath = AssetDatabase.GUIDToAssetPath(otherSwatchGUIDs[i]); var otherSwatch = AssetDatabase.LoadAssetAtPath <Swatch>(otherSwatchAssetPath); if (otherSwatch != null) { swatch.ReplaceSelfWithOtherSwatch(otherSwatch); SwatchEditorGUI.GameViewRepaint(); } else { Debug.LogError("[SwatchEditorGUI] couldnt load asset at path: " + otherSwatchAssetPath); } break; } } } } if (GUILayout.Button(merge ? "Cancel Merge" : "Merge With Another Swatch")) { mergeObject = null; merge = !merge; } if (merge) { mergeObject = (Swatch)EditorGUILayout.ObjectField(mergeObject, typeof(Swatch), false); if (mergeObject != null) { if (GUILayout.Button("Merge")) { swatch.AddColorsFromOtherSwatch(mergeObject); mergeObject = null; merge = false; } } } if (GUILayout.Button("Export To Color Picker Presets")) { SwatchPresetExporter.ExportToColorPresetLibrary(swatch); } }
public static void CreateSwatch() { Swatch asset = ScriptableObject.CreateInstance <Swatch>(); ProjectWindowUtil.CreateAsset(asset, "New Swatch.asset"); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { SwatchyColor swatchyColor = (SwatchyColor)fieldInfo.GetValue(property.serializedObject.targetObject); Swatch swatch = swatchyColor.swatch; Color color = swatchyColor.color; if (swatchTexture == null) { swatchTexture = textureWithColor(color); } var swatchProperty = property.FindPropertyRelative("_swatch"); var colorIndexProperty = property.FindPropertyRelative("_colorIndex"); position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); var swatchSize = EditorGUIUtility.singleLineHeight; var keySize = EditorGUIUtility.singleLineHeight * 1.25f; var spacing = EditorGUIUtility.singleLineHeight * 0.5f; //var toggleSize = EditorGUIUtility.singleLineHeight; var toggleSize = 0; var swatchObjectPositionX = swatch == null ? position.x : position.x + swatchSize + keySize + toggleSize + spacing * 2; //var swatchObjectWidth = swatch == null ? position.width : position.width - swatchSize - keySize - spacing * 2; var swatchObjectWidth = position.width - swatchObjectPositionX + position.x; var swatchObjectRect = new Rect(swatchObjectPositionX, position.y, swatchObjectWidth, EditorGUIUtility.singleLineHeight); var swatchRect = new Rect(position.x, position.y, swatchSize, EditorGUIUtility.singleLineHeight); var colorIndexRect = new Rect(swatchRect.position.x + swatchRect.width + spacing, position.y, keySize, EditorGUIUtility.singleLineHeight); EditorGUI.BeginProperty(position, label, property); var indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; // Draw Swatch object EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(swatchObjectRect, swatchProperty, GUIContent.none); if (EditorGUI.EndChangeCheck()) { property.serializedObject.ApplyModifiedProperties(); swatchyColor.swatch = swatchyColor._swatch; // hack which calls observer pattern UpdateActiveSwatch(swatchyColor.color); } if (swatch != null) { // Draw Color Field if (DrawTextureButton(swatchTexture, swatchRect)) { paletteOpen = !paletteOpen && swatch != null && swatch.colors != null && swatch.colors.Length > 0; } DrawBlackGrid(swatchRect.x, swatchRect.y, 1, 1, (int)EditorGUIUtility.singleLineHeight); // Draw Color index text field EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(colorIndexRect, colorIndexProperty, GUIContent.none); if (EditorGUI.EndChangeCheck()) { property.serializedObject.ApplyModifiedProperties(); swatchyColor.colorIndex = colorIndexProperty.intValue; // hack which calls observer pattern UpdateActiveSwatch(swatchyColor.color); } // Draw Toggle //EditorGUI.PropertyField(usingSwatchGroupToggleR, usingSwatchGroupProperty, GUIContent.none); //usingSwatchGroupProperty.boolValue = EditorGUI.Toggle(usingSwatchGroupToggleR, usingSwatchGroupProperty.boolValue); if (paletteOpen) { if (palleteTexture == null) { palleteTexture = textureWithColors(swatch.colors); } var textureRect = new Rect(swatchRect.x, swatchRect.y + EditorGUIUtility.singleLineHeight + 3, palleteTexture.width * EditorGUIUtility.singleLineHeight, palleteTexture.height * EditorGUIUtility.singleLineHeight); DrawTexture(palleteTexture, textureRect); DrawBlackGrid(textureRect.x, textureRect.y, palleteTexture.width, palleteTexture.height, (int)EditorGUIUtility.singleLineHeight); // listen to click Event e = Event.current; if (IsClickInRect(textureRect)) { Vector2 rectClickPosition = e.mousePosition - textureRect.position; int cellXIndex = (int)(rectClickPosition.x / EditorGUIUtility.singleLineHeight); int cellYIndex = (int)(rectClickPosition.y / EditorGUIUtility.singleLineHeight); int colorIndex = cellYIndex * palleteTexture.width + cellXIndex; colorIndexProperty.intValue = colorIndex; property.serializedObject.ApplyModifiedProperties(); swatchyColor.colorIndex = colorIndex; // calls observer pattern UpdateActiveSwatch(swatchyColor.color); } else if (IsClick()) { paletteOpen = false; EditorUtility.SetDirty(property.serializedObject.targetObject); // Repaint } } } // Set indent back to what it was EditorGUI.indentLevel = indent; EditorGUI.EndProperty(); }