using UnityEditor; using UnityEngine; namespace NugetForUnity.Ui { /// /// Collection of GUIStyle. /// internal static class Styles { private static GUIStyle cachedSearchFieldStyle; private static GUIStyle cachedHeaderStyle; private static GUIStyle cachedToolbarStyle; private static GUIStyle cachedBackgroundStyle; private static GUIStyle cachedBoldLabelStyle; private static GUIStyle cachedAuthorsLabelStyle; private static GUIStyle cachedDescriptionLabelStyle; private static GUIStyle cachedLinkLabelStyle; private static GUIStyle cachedPackageNameLabelStyle; /// /// Gets the color used for lines used as seperators. /// public static Color LineColor { get { if (EditorGUIUtility.isProSkin) { return new Color(0.05f, 0.05f, 0.05f); } return new Color(0.6f, 0.6f, 0.6f); } } /// /// Gets the color used for package authors. /// public static Color AuthorsTextColor { get { if (EditorGUIUtility.isProSkin) { return new Color(0.55f, 0.55f, 0.55f); } return new Color(0.45f, 0.45f, 0.45f); } } /// /// Gets the color used for the foldout header. /// public static Color FoldoutHeaderColor { get { if (EditorGUIUtility.isProSkin) { return new Color(0.2f, 0.2f, 0.2f); } return new Color(0.85f, 0.85f, 0.85f); } } /// /// Gets a GUI style with a background color the same as the editor's current background color. /// public static GUIStyle BackgroundStyle { get { if (cachedBackgroundStyle != null) { return cachedBackgroundStyle; } cachedBackgroundStyle = new GUIStyle(); var backgroundColor = EditorGUIUtility.isProSkin ? new Color32(56, 56, 56, 255) : new Color32(194, 194, 194, 255); cachedBackgroundStyle.normal.background = CreateSingleColorTexture(backgroundColor); return cachedBackgroundStyle; } } /// /// Gets the GUI style used for the header of the NuGet window. /// public static GUIStyle HeaderStyle { get { if (cachedHeaderStyle != null) { return cachedHeaderStyle; } cachedHeaderStyle = new GUIStyle(); var backgroundColor = EditorGUIUtility.isProSkin ? new Color(0.1f, 0.1f, 0.1f) : new Color(0.4f, 0.4f, 0.4f); cachedHeaderStyle.alignment = TextAnchor.MiddleLeft; cachedHeaderStyle.normal.background = CreateSingleColorTexture(backgroundColor); cachedHeaderStyle.normal.textColor = Color.white; return cachedHeaderStyle; } } /// /// Gets the GUI style used for the toolbar of the NuGet window. /// public static GUIStyle ToolbarStyle { get { if (cachedToolbarStyle != null) { return cachedToolbarStyle; } cachedToolbarStyle = new GUIStyle(EditorStyles.toolbar) { fixedHeight = 25f }; return cachedToolbarStyle; } } /// /// Gets the GUI style used for the package search field. /// public static GUIStyle SearchFieldStyle { get { if (cachedSearchFieldStyle != null) { return cachedSearchFieldStyle; } var original = GetStyle("ToolbarSearchTextField"); cachedSearchFieldStyle = new GUIStyle(original) { fontSize = 12, fixedHeight = 20f }; return cachedSearchFieldStyle; } } /// /// Gets the GUI style used for bold labels. /// public static GUIStyle BoldLabelStyle { get { if (cachedBoldLabelStyle != null) { return cachedBoldLabelStyle; } cachedBoldLabelStyle = new GUIStyle(EditorStyles.label) { fontSize = 12, fontStyle = FontStyle.Bold }; return cachedBoldLabelStyle; } } /// /// Gets the GUI style used for authors labels. /// public static GUIStyle AuthorsLabelStyle { get { if (cachedAuthorsLabelStyle != null) { return cachedAuthorsLabelStyle; } cachedAuthorsLabelStyle = new GUIStyle(EditorStyles.label) { fontSize = 10, fontStyle = FontStyle.Normal }; cachedAuthorsLabelStyle.normal.textColor = AuthorsTextColor; cachedAuthorsLabelStyle.focused.textColor = AuthorsTextColor; cachedAuthorsLabelStyle.hover.textColor = AuthorsTextColor; return cachedAuthorsLabelStyle; } } /// /// Gets the GUI style used for description labels. /// public static GUIStyle DescriptionLabelStyle { get { if (cachedDescriptionLabelStyle != null) { return cachedDescriptionLabelStyle; } cachedDescriptionLabelStyle = new GUIStyle(EditorStyles.label) { wordWrap = true, fontStyle = FontStyle.Normal, alignment = TextAnchor.UpperLeft, }; return cachedDescriptionLabelStyle; } } /// /// Gets the GUI style used for package name labels. /// public static GUIStyle PackageNameLabelStyle { get { if (cachedPackageNameLabelStyle != null) { return cachedPackageNameLabelStyle; } cachedPackageNameLabelStyle = new GUIStyle(EditorStyles.label) { fontSize = 15, fontStyle = FontStyle.Bold }; return cachedPackageNameLabelStyle; } } /// /// Gets the GUI style used for labels representing a URL / clickable LINK. /// public static GUIStyle LinkLabelStyle { get { if (cachedLinkLabelStyle != null) { return cachedLinkLabelStyle; } cachedLinkLabelStyle = GetStyle("LinkLabel"); return cachedLinkLabelStyle; } } /// /// From here: http://forum.unity3d.com/threads/changing-the-background-color-for-beginhorizontal.66015/. /// /// The color to fill the texture with. /// The generated texture. private static Texture2D CreateSingleColorTexture(Color color) { const int width = 16; const int height = 16; var pix = new Color32[width * height]; Color32 color32 = color; for (var index = 0; index < pix.Length; index++) { pix[index] = color32; } var result = new Texture2D(width, height); result.SetPixels32(pix); result.Apply(); return result; } private static GUIStyle GetStyle(string styleName) { var style = GUI.skin.FindStyle(styleName); if (style == null) { style = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle(styleName); } if (style == null) { Debug.LogError("Missing built-in guistyle " + styleName); style = new GUIStyle(); } return style; } } }