From fffd520cbebaa5151783bcd55daa8123743d3d65 Mon Sep 17 00:00:00 2001 From: Miguel Alcantara Date: Sun, 28 Oct 2018 20:59:45 -0400 Subject: [PATCH 1/3] Modified editor to include Unity label window. This allows for easier search for the fonts with the provided autocomplete logic. --- Editor/LabelWindow.cs | 353 ++++++++++++++++++++++++++++++++++++ Editor/LabelWindow.cs.meta | 11 ++ Editor/ModifyEditorStyle.cs | 54 ++++-- 3 files changed, 399 insertions(+), 19 deletions(-) create mode 100644 Editor/LabelWindow.cs create mode 100644 Editor/LabelWindow.cs.meta diff --git a/Editor/LabelWindow.cs b/Editor/LabelWindow.cs new file mode 100644 index 0000000..cef394c --- /dev/null +++ b/Editor/LabelWindow.cs @@ -0,0 +1,353 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace RotaryHeart.Lib +{ + public class LabelWindow + { + public delegate void SelectCallback(string label, bool added); + public SelectCallback OnSelect; + + object m_AssetLabels; + bool allowMultipleSelection = true; + bool allowCustom; + Dictionary allLabels = new Dictionary(); + + #region Reflection + + #region Fields + + Assembly m_assembly; + + Type m_inputDataType; + Type m_listElementType; + Type m_popupListType; + + FieldInfo m_CloseOnSelection; + FieldInfo m_AllowCustom; + FieldInfo m_OnSelectCallback; + FieldInfo m_MaxCount; + FieldInfo m_SortAlphabetically; + FieldInfo m_EnableAutoCompletion; + + PropertyInfo m_filterScore; + PropertyInfo m_selected; + PropertyInfo m_text; + + MethodInfo m_OnInternalSelectCallback; + MethodInfo m_NewOrMatchingElement; + + #endregion + + #region Properties + + Assembly EditorAssembly + { + get + { + if (m_assembly == null) + { + m_assembly = typeof(Editor).Assembly; + } + + return m_assembly; + } + } + + Type InputDataType + { + get + { + if (m_inputDataType == null) + { + m_inputDataType = EditorAssembly.GetType("UnityEditor.PopupList+InputData"); + } + + return m_inputDataType; + } + } + Type ListElementType + { + get + { + if (m_listElementType == null) + { + m_listElementType = EditorAssembly.GetType("UnityEditor.PopupList+ListElement"); + } + + return m_listElementType; + } + } + Type PopupList + { + get + { + if (m_popupListType == null) + { + m_popupListType = EditorAssembly.GetType("UnityEditor.PopupList"); + } + + return m_popupListType; + } + } + + FieldInfo CloseOnSelection + { + get + { + if (m_CloseOnSelection == null) + { + m_CloseOnSelection = InputDataType.GetField("m_CloseOnSelection"); + } + + return m_CloseOnSelection; + } + } + FieldInfo AllowCustom + { + get + { + if (m_AllowCustom == null) + { + m_AllowCustom = InputDataType.GetField("m_AllowCustom"); + } + + return m_AllowCustom; + } + } + FieldInfo OnSelectCallback + { + get + { + if (m_OnSelectCallback == null) + { + m_OnSelectCallback = InputDataType.GetField("m_OnSelectCallback"); + } + + return m_OnSelectCallback; + } + } + FieldInfo MaxCount + { + get + { + if (m_MaxCount == null) + { + m_MaxCount = InputDataType.GetField("m_MaxCount"); + } + + return m_MaxCount; + } + } + FieldInfo SortAlphabetically + { + get + { + if (m_SortAlphabetically == null) + { + m_SortAlphabetically = InputDataType.GetField("m_SortAlphabetically"); + } + + return m_SortAlphabetically; + } + } + FieldInfo EnableAutoCompletion + { + get + { + if (m_EnableAutoCompletion == null) + { + m_EnableAutoCompletion = InputDataType.GetField("m_EnableAutoCompletion"); + } + + return m_EnableAutoCompletion; + } + } + + PropertyInfo FilterScore + { + get + { + if (m_filterScore == null) + { + m_filterScore = ListElementType.GetProperty("filterScore"); + } + + return m_filterScore; + } + } + PropertyInfo Selected + { + get + { + if (m_selected == null) + { + m_selected = ListElementType.GetProperty("selected"); + } + + return m_selected; + } + } + PropertyInfo Text + { + get + { + if (m_text == null) + { + m_text = ListElementType.GetProperty("text"); + } + + return m_text; + } + } + + MethodInfo OnInternalSelectCallback + { + get + { + if (m_OnInternalSelectCallback == null) + { + m_OnInternalSelectCallback = typeof(LabelWindow).GetMethod("_OnInternalSelectCallback", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(ListElementType); + } + + return m_OnInternalSelectCallback; + } + } + MethodInfo NewOrMatchingElement + { + get + { + if (m_NewOrMatchingElement == null) + { + m_NewOrMatchingElement = InputDataType.GetMethod("NewOrMatchingElement"); + } + + return m_NewOrMatchingElement; + } + } + + #endregion + + #endregion + + /// + /// Opens the default label window + /// + /// The position where the window is going to be drawn + /// Label array used to populate the list + /// Selected labels + /// Should the default labels be included + /// Should the system allow more than 1 label selected + /// Should the window close on selection + /// Allow custom labels + /// Max label count + /// Should the labels be sorted + /// Should the window show auto completetion + public void OpenLabelWindow(Rect position, string[] labels, string[] selectedLabels, bool populateDefaultLabels = true, bool allowMultipleSelection = true, bool closeOnSelection = false, bool allowCustom = true, int maxCount = 15, bool sortAlphabetically = true, bool enableAutoCompletition = true) + { + if (labels == null) + labels = new string[] { }; + if (selectedLabels == null) + selectedLabels = new string[] { }; + + this.allowMultipleSelection = allowMultipleSelection; + this.allowCustom = allowCustom; + + //Cache the data required + CacheData(labels, selectedLabels, populateDefaultLabels, closeOnSelection, maxCount, sortAlphabetically, enableAutoCompletition); + + //Create a reference of the window + var popupListReference = Activator.CreateInstance(PopupList, new object[] { m_AssetLabels }); + + //Get the correct show method + var showMethod = typeof(PopupWindow).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(x => + x.Name.Equals("Show") && x.GetParameters().Length == 4).Single(); + + //Invoke the method with the correct arguments + showMethod.Invoke(null, new object[] { position, popupListReference, null, 6 }); + } + + void CacheData(string[] labels, string[] selectedLabels, bool populateDefaultLabels, bool closeOnSelection, int maxCount, bool sortAlphabetically, bool enableAutoCompletition) + { + //Create instance to the delegate + Delegate action = Delegate.CreateDelegate(OnSelectCallback.FieldType, this, OnInternalSelectCallback); + + //Create instance of data to send to the popup window + m_AssetLabels = Activator.CreateInstance(InputDataType); + + //Assign all the respective values, including the delegate callback + CloseOnSelection.SetValue(m_AssetLabels, closeOnSelection); + AllowCustom.SetValue(m_AssetLabels, true); + OnSelectCallback.SetValue(m_AssetLabels, action); + MaxCount.SetValue(m_AssetLabels, maxCount); + SortAlphabetically.SetValue(m_AssetLabels, sortAlphabetically); + EnableAutoCompletion.SetValue(m_AssetLabels, enableAutoCompletition); + + //Get all the labels available + if (populateDefaultLabels) + { + allLabels = (Dictionary)typeof(AssetDatabase).InvokeMember("GetAllLabels", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, null); + } + + //Include any custom one sent on the array + foreach (var tag in labels) + { + if (string.IsNullOrEmpty(tag)) + continue; + + if (!allLabels.ContainsKey(tag)) + allLabels.Add(tag, 0); + } + + //Asing all the selected values + foreach (var pair in allLabels) + { + var element = NewOrMatchingElement.Invoke(m_AssetLabels, new object[] { pair.Key }); + + if ((float)(FilterScore.GetValue(element, null)) < pair.Value) + { + FilterScore.SetValue(element, pair.Value, null); + } + Selected.SetValue(element, selectedLabels.Any(label => string.Equals(label, pair.Key, StringComparison.OrdinalIgnoreCase)), null); + } + } + + /// + /// Function called by Unity when an element is selected + /// + /// Element data + void _OnInternalSelectCallback(T data) + { + string selectedLabel = Text.GetValue(data, null).ToString(); + + if (!allowCustom && !allLabels.Keys.Any(x => x.ToLower().Equals(selectedLabel.ToLower()))) + return; + + if (!allowMultipleSelection) + { + foreach (var pair in allLabels) + { + if (pair.Key.ToLower().Equals(selectedLabel.ToLower())) + { + continue; + } + + var element = NewOrMatchingElement.Invoke(m_AssetLabels, new object[] { pair.Key }); + + Selected.SetValue(element, false, null); + } + } + + bool currentValue = (bool)(Selected.GetValue(data, null)); + + Selected.SetValue(data, !currentValue, null); + + if (OnSelect != null) + OnSelect.Invoke(selectedLabel, !currentValue); + } + } +} diff --git a/Editor/LabelWindow.cs.meta b/Editor/LabelWindow.cs.meta new file mode 100644 index 0000000..a306047 --- /dev/null +++ b/Editor/LabelWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0e6c6148bffd72544931589c0097ad4a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/ModifyEditorStyle.cs b/Editor/ModifyEditorStyle.cs index 2309848..47a7bb5 100644 --- a/Editor/ModifyEditorStyle.cs +++ b/Editor/ModifyEditorStyle.cs @@ -1,8 +1,7 @@ -using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; -using System; +using RotaryHeart.Lib; public class ModifyEditorStyle { @@ -15,30 +14,34 @@ public class ModifyEditorStyle private static int fontSize { - get{ - return EditorPrefs.GetInt("ModifyEditorStyle_FontSize",11); + get + { + return EditorPrefs.GetInt("ModifyEditorStyle_FontSize", 11); } - set{ - EditorPrefs.SetInt("ModifyEditorStyle_FontSize",value); + set + { + EditorPrefs.SetInt("ModifyEditorStyle_FontSize", value); } } - private static int selected + private static string selected { - get{ - string fontName = EditorPrefs.GetString("ModifyEditorStyle_Selected", defaultFont); - return Array.IndexOf(fonts, fontName); + get + { + return EditorPrefs.GetString("ModifyEditorStyle_Selected", defaultFont); } - set{ - EditorPrefs.SetString("ModifyEditorStyle_Selected", (value < fonts.Length && value >= 0) ? fonts[value] : defaultFont); + set + { + EditorPrefs.SetString("ModifyEditorStyle_Selected", value); } } private static string[] _fonts; private static string[] fonts { - get{ - if(_fonts == null) + get + { + if (_fonts == null) { _fonts = Font.GetOSInstalledFontNames(); } @@ -114,7 +117,7 @@ private static IEnumerable InternalStyles } } - + #if UNITY_2018_3_OR_NEWER private class ModifyEditorStyleProvider : SettingsProvider { @@ -140,7 +143,21 @@ static void ModifyEditorStylePreference() { EditorGUILayout.HelpBox("Changing the font size works but unfortunately the line height used in various drawers is baked as a const 16, we could not change it as a const was baked throughout the compiled Unity source code, the enlarged font will clip. The default seems to be 11, I found that going to 13 is still readable if that helps with your eye condition for the time being. (It clips characters with hanging part like 'g') Some part seems to not change immediately until you recompile something.", MessageType.Info); - selected = EditorGUILayout.Popup("Font", selected, fonts); + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.PrefixLabel("Font"); + if (EditorGUILayout.DropdownButton(new GUIContent(selected), FocusType.Keyboard)) + { + LabelWindow window = new LabelWindow(); + + window.OnSelect = (string val, bool enable) => + { + selected = val; + }; + + window.OpenLabelWindow(new Rect(Event.current.mousePosition, Vector2.one), fonts, null, false, false, true, false); + } + EditorGUILayout.EndHorizontal(); + fontSize = EditorGUILayout.IntField("Font Size", fontSize); if (GUILayout.Button("Modify")) { @@ -151,8 +168,7 @@ static void ModifyEditorStylePreference() static void Modify() { GUISkin skin = GUI.skin; - string font = selected >= 0 && selected < fonts.Length ? fonts[selected] : defaultFont; - Font changeToFont = Font.CreateDynamicFontFromOSFont(font, fontSize); + Font changeToFont = Font.CreateDynamicFontFromOSFont(selected, fontSize); foreach (var x in EditorStylesGUIStyles) { @@ -194,4 +210,4 @@ static Startup() EditorApplication.hierarchyWindowItemOnGUI += ModifyStartUp; } } -} +} \ No newline at end of file From 49c85447a344c57340ab634c34a5d4e2fcc86ddc Mon Sep 17 00:00:00 2001 From: Miguel Alcantara Date: Mon, 29 Oct 2018 06:19:48 -0400 Subject: [PATCH 2/3] Merge branch 'master' of https://github.com/5argon/ModifyEditorStyle into 5argon-master # Conflicts: # Editor/ModifyEditorStyle.cs --- Editor/ModifyEditorStyle.cs | 376 +++++++++++++++++++++++++++++------- 1 file changed, 309 insertions(+), 67 deletions(-) diff --git a/Editor/ModifyEditorStyle.cs b/Editor/ModifyEditorStyle.cs index 47a7bb5..14aea8c 100644 --- a/Editor/ModifyEditorStyle.cs +++ b/Editor/ModifyEditorStyle.cs @@ -1,7 +1,11 @@ +using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.SceneManagement; using UnityEditor; -using RotaryHeart.Lib; +using UnityEditor.SceneManagement; +using System; +using System.Reflection; public class ModifyEditorStyle { @@ -12,36 +16,93 @@ public class ModifyEditorStyle "Lucida Grande"; #endif + private static bool enable + { + get{ + return EditorPrefs.GetBool("ModifyEditorStyle_Enable",true); + } + set{ + EditorPrefs.SetBool("ModifyEditorStyle_Enable",value); + } + } + private static int fontSize { - get - { - return EditorPrefs.GetInt("ModifyEditorStyle_FontSize", 11); + get{ + return EditorPrefs.GetInt("ModifyEditorStyle_FontSize",11); } - set - { - EditorPrefs.SetInt("ModifyEditorStyle_FontSize", value); + set{ + EditorPrefs.SetInt("ModifyEditorStyle_FontSize",value); } } - private static string selected + private static int smallFontSize { - get - { - return EditorPrefs.GetString("ModifyEditorStyle_Selected", defaultFont); + get{ + return EditorPrefs.GetInt("ModifyEditorStyle_SmallFontSize",9); } - set - { - EditorPrefs.SetString("ModifyEditorStyle_Selected", value); + set{ + EditorPrefs.SetInt("ModifyEditorStyle_SmallFontSize",value); + } + } + + private static int bigFontSize + { + get{ + return EditorPrefs.GetInt("ModifyEditorStyle_BigFontSize",12); + } + set{ + EditorPrefs.SetInt("ModifyEditorStyle_BigFontSize",value); + } + } + + private static int paddingTop + { + get{ + return EditorPrefs.GetInt("ModifyEditorStyle_PaddingTop",1); + } + set{ + EditorPrefs.SetInt("ModifyEditorStyle_PaddingTop",value); + } + } + + private static int paddingBottom + { + get{ + return EditorPrefs.GetInt("ModifyEditorStyle_PaddingBottom",2); + } + set{ + EditorPrefs.SetInt("ModifyEditorStyle_PaddingBottom",value); + } + } + + private static int selected + { + get{ + string fontName = EditorPrefs.GetString("ModifyEditorStyle_Selected", defaultFont); + return Array.IndexOf(fonts, fontName); + } + set{ + EditorPrefs.SetString("ModifyEditorStyle_Selected", (value < fonts.Length && value >= 0) ? fonts[value] : defaultFont); + } + } + + private static int selectedBold + { + get{ + string fontName = EditorPrefs.GetString("ModifyEditorStyle_SelectedBold", defaultFont); + return Array.IndexOf(fonts, fontName); + } + set{ + EditorPrefs.SetString("ModifyEditorStyle_SelectedBold", (value < fonts.Length && value >= 0) ? fonts[value] : defaultFont); } } private static string[] _fonts; private static string[] fonts { - get - { - if (_fonts == null) + get{ + if(_fonts == null) { _fonts = Font.GetOSInstalledFontNames(); } @@ -49,20 +110,95 @@ private static string[] fonts } } + private static IEnumerable GUISkinStyles + { + get + { + GUISkin skin = GUI.skin; + yield return skin.label; + yield return skin.button; + yield return skin.textArea; + yield return skin.textField; + } + } + /// You can comment out what you don't wanna change private static IEnumerable EditorStylesGUIStyles { get { - yield return EditorStyles.boldLabel; - yield return EditorStyles.centeredGreyMiniLabel; yield return EditorStyles.colorField; yield return EditorStyles.foldout; yield return EditorStyles.foldoutPreDrop; - yield return EditorStyles.helpBox; yield return EditorStyles.label; + yield return EditorStyles.numberField; //textField + yield return EditorStyles.objectField; + yield return EditorStyles.objectFieldMiniThumb; + yield return EditorStyles.radioButton; + yield return EditorStyles.textArea; //textField + yield return EditorStyles.textField; //textField + yield return EditorStyles.toggle; + yield return EditorStyles.whiteLabel; + yield return EditorStyles.wordWrappedLabel; + } + } + + private static IEnumerable NeedPadding + { + get{ + GUISkin skin = GUI.skin; + yield return skin.label; + yield return skin.textArea; + yield return skin.textField; + yield return EditorStyles.foldout; + yield return EditorStyles.foldoutPreDrop; + yield return EditorStyles.label; + yield return EditorStyles.textArea; //textField + yield return EditorStyles.textField; //textField + yield return EditorStyles.numberField; //textField + + yield return GUI.skin.FindStyle("TV Line"); + yield return GUI.skin.FindStyle("TV Insertion"); + yield return GUI.skin.FindStyle("TV Ping"); + yield return GUI.skin.FindStyle("TV Selection"); + + //Styles in older version + yield return GUI.skin.FindStyle("IN Foldout"); + yield return GUI.skin.FindStyle("PR Insertion"); + yield return GUI.skin.FindStyle("PR Label"); + } + } + + private static IEnumerable EditorStylesBold + { + get + { + yield return EditorStyles.boldLabel; + yield return EditorStyles.toggleGroup; //BoldToggle + yield return EditorStyles.whiteBoldLabel; + + //Internal style + yield return GUI.skin.FindStyle("TV LineBold"); + } + + } + + private static IEnumerable EditorStylesBig + { + get + { yield return EditorStyles.largeLabel; - yield return EditorStyles.layerMaskField; + yield return EditorStyles.whiteLargeLabel; + } + } + + private static IEnumerable EditorStylesSmall + { + get + { + yield return EditorStyles.centeredGreyMiniLabel; //Same as miniLabel + yield return EditorStyles.helpBox; + yield return EditorStyles.layerMaskField; //MiniPopup yield return EditorStyles.miniBoldLabel; yield return EditorStyles.miniButton; yield return EditorStyles.miniButtonLeft; @@ -70,30 +206,23 @@ private static IEnumerable EditorStylesGUIStyles yield return EditorStyles.miniButtonRight; yield return EditorStyles.miniLabel; yield return EditorStyles.miniTextField; - yield return EditorStyles.numberField; - yield return EditorStyles.objectField; - yield return EditorStyles.objectFieldMiniThumb; yield return EditorStyles.objectFieldThumb; - yield return EditorStyles.popup; - yield return EditorStyles.radioButton; - yield return EditorStyles.textArea; - yield return EditorStyles.textField; - yield return EditorStyles.toggle; - yield return EditorStyles.toggleGroup; + yield return EditorStyles.popup; //MiniPopup yield return EditorStyles.toolbar; yield return EditorStyles.toolbarButton; yield return EditorStyles.toolbarDropDown; yield return EditorStyles.toolbarPopup; yield return EditorStyles.toolbarTextField; - yield return EditorStyles.whiteBoldLabel; - yield return EditorStyles.whiteLabel; - yield return EditorStyles.whiteLargeLabel; yield return EditorStyles.whiteMiniLabel; - yield return EditorStyles.wordWrappedLabel; yield return EditorStyles.wordWrappedMiniLabel; //Not available in 2017.1.5f1 but available in 2018.3, not sure when was it added. - //yield return EditorStyles.miniPullDown; +#if UNITY_2018_3_OR_NEWER + yield return EditorStyles.miniPullDown; +#endif + + //Internal styles + yield return GUI.skin.FindStyle("GV Gizmo DropDown"); } } @@ -105,9 +234,6 @@ private static IEnumerable InternalStyles yield return GUI.skin.FindStyle("TV Line"); yield return GUI.skin.FindStyle("TV Insertion"); yield return GUI.skin.FindStyle("TV Ping"); - yield return GUI.skin.FindStyle("ToolbarButton"); - yield return GUI.skin.FindStyle("TV Line"); - yield return GUI.skin.FindStyle("TV LineBold"); yield return GUI.skin.FindStyle("TV Selection"); //Styles in older version @@ -117,7 +243,7 @@ private static IEnumerable InternalStyles } } - + #if UNITY_2018_3_OR_NEWER private class ModifyEditorStyleProvider : SettingsProvider { @@ -141,57 +267,163 @@ static SettingsProvider ModifyEditorStyleSettingsProvider() #endif static void ModifyEditorStylePreference() { - EditorGUILayout.HelpBox("Changing the font size works but unfortunately the line height used in various drawers is baked as a const 16, we could not change it as a const was baked throughout the compiled Unity source code, the enlarged font will clip. The default seems to be 11, I found that going to 13 is still readable if that helps with your eye condition for the time being. (It clips characters with hanging part like 'g') Some part seems to not change immediately until you recompile something.", MessageType.Info); + EditorGUILayout.HelpBox("Changing the font size works but unfortunately the line height used in various drawers was baked as a const 16, we could not change it as a const was baked throughout the compiled Unity source code. (The enlarged characters with hanging part like 'g' will clip.)\n\nAlso, some parts seems to not change immediately until you recompile something.", MessageType.Info); - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.PrefixLabel("Font"); - if (EditorGUILayout.DropdownButton(new GUIContent(selected), FocusType.Keyboard)) - { - LabelWindow window = new LabelWindow(); - - window.OnSelect = (string val, bool enable) => - { - selected = val; - }; - - window.OpenLabelWindow(new Rect(Event.current.mousePosition, Vector2.one), fonts, null, false, false, true, false); - } - EditorGUILayout.EndHorizontal(); + enable = EditorGUILayout.BeginToggleGroup("Enable", enable); + selected = EditorGUILayout.Popup("Font", selected, fonts); + selectedBold = EditorGUILayout.Popup("Bold Font", selectedBold, fonts); + EditorGUILayout.Space(); fontSize = EditorGUILayout.IntField("Font Size", fontSize); + smallFontSize = EditorGUILayout.IntField("Small Font Size", smallFontSize); + bigFontSize = EditorGUILayout.IntField("Big Font Size", bigFontSize); + EditorGUILayout.Space(); + EditorGUILayout.HelpBox("Applies custom paddings to certain UI", MessageType.Info); + paddingTop = EditorGUILayout.IntField("Padding Top", paddingTop); + paddingBottom = EditorGUILayout.IntField("Padding Bottom", paddingBottom); + if (GUILayout.Button("Modify")) { Modify(); } + + EditorGUILayout.EndToggleGroup(); } + //These statics are cleared out so often including just on loading a new scene.. it makes the linked font disappear + private static Font normalFont; + private static Font bigFont; + private static Font smallFont; + private static Font boldFont; + private static Font smallBoldFont; + static void Modify() { + if(!enable) return; + + string fontName = selected >= 0 && selected < fonts.Length ? fonts[selected] : defaultFont; + string boldFontName = selectedBold >= 0 && selectedBold < fonts.Length ? fonts[selectedBold] : defaultFont; + + normalFont = Font.CreateDynamicFontFromOSFont(fontName, fontSize); + //bigFont = Font.CreateDynamicFontFromOSFont(fontName, bigFontSize); + smallFont = Font.CreateDynamicFontFromOSFont(fontName, smallFontSize); + boldFont = Font.CreateDynamicFontFromOSFont(boldFontName, fontSize); + smallBoldFont = Font.CreateDynamicFontFromOSFont(boldFontName, smallFontSize); + GUISkin skin = GUI.skin; - Font changeToFont = Font.CreateDynamicFontFromOSFont(selected, fontSize); + //Debug.Log($"- : {skin.font?.name} {skin.font?.fontSize}"); + skin.font = normalFont; + GUI.skin = skin; //SetDefaultFont activated on this setter + + //EditorStyles static was pulled from s_Current which was populated from `EditorGUIUtility.GetBuiltinSkin` which we cannot interfere. + //s_Current is internal and therefore we need to reflect to change the font. All other styles are accessible except the fonts. + var eType = typeof(EditorStyles); + var es = (EditorStyles)(eType.GetField("s_Current", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null)); + eType.GetField("m_StandardFont", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(es, normalFont); + eType.GetField("m_BoldFont", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(es, boldFont); + eType.GetField("m_MiniFont", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(es, smallFont); + eType.GetField("m_MiniBoldFont", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(es, smallBoldFont); + + //We should not override font where there's no font in the first place, because that will make the fallback switch + //to bold on override not working. - foreach (var x in EditorStylesGUIStyles) + // foreach (var z in GUISkinStyles) + // { + // Debug.Log($"{z.name} : {z.font?.name} {z.font?.fontSize} {z.fontSize} {z.padding}"); + // } + + // foreach (var x in EditorStylesGUIStyles) + // { + // if (x != null) + // { + // if(x.font != null) + // { + // Debug.Log($"{x.name} : {x.font.name} {x.font.fontSize} {x.fontSize} {x.padding}"); + // } + // else + // { + // Debug.Log($"{x.name} : NO FONT {x.fontSize} {x.padding}"); + // } + // } + // } + + foreach (var x in NeedPadding) + { + if(x != null) + { + //Debug.Log($"{x.name} -> {x.padding}"); + var p = x.padding; + p.top = paddingTop; + p.bottom = paddingBottom; + x.padding = p; + } + } + + foreach (var x in EditorStylesBig) { if (x != null) { - x.font = changeToFont; + // if(x.font != null) + // { + // Debug.Log($"{x.name} : {x.font.name} {x.font.fontSize} {x.fontSize} {x.padding}"); + // } + // else + // { + // Debug.Log($"{x.name} : NO FONT {x.fontSize} {x.padding}"); + // } + + x.fontSize = bigFontSize; } } - foreach (var x in InternalStyles) + + foreach (var x in EditorStylesSmall) { if (x != null) { - x.font = changeToFont; + // if(x.font != null) + // { + // Debug.Log($"SMALL {x.name} : {x.font.name} {x.font.fontSize} {x.fontSize} {x.padding}"); + // } + // else + // { + // Debug.Log($"SMALL {x.name} : NO FONT {x.fontSize} {x.padding}"); + // } + + x.fontSize = smallFontSize; } } - skin.font = changeToFont; - skin.label.font = changeToFont; - skin.button.font = changeToFont; - skin.textArea.font = changeToFont; - skin.textField.font = changeToFont; - GUI.skin = skin; - //Debug.Log($"Modified"); + // foreach (var x in EditorStylesBold) + // { + // if (x != null) + // { + // if(x.font != null) + // { + // Debug.Log($"{x.name} : {x.font.name} {x.font.fontSize} {x.fontSize} {x.padding}"); + // } + // else + // { + // Debug.Log($"{x.name} : NO FONT {x.fontSize} {x.padding}"); + // } + // } + // } + + // foreach (var x in InternalStyles) + // { + // if (x != null) + // { + // if(x.font != null) + // { + // Debug.Log($"{x.name} : {x.font.name} {x.font.fontSize} {x.fontSize} {x.padding}"); + // } + // else + // { + // Debug.Log($"{x.name} : NO FONT {x.fontSize} {x.padding}"); + // } + // } + // } + + // Debug.Log($"Modified"); } static void ModifyStartUp(int instanceID, Rect selectionRect) @@ -200,6 +432,12 @@ static void ModifyStartUp(int instanceID, Rect selectionRect) EditorApplication.hierarchyWindowItemOnGUI -= ModifyStartUp; } + static void ModifySceneChange(Scene scene, OpenSceneMode mode) + { + EditorApplication.hierarchyWindowItemOnGUI -= ModifyStartUp; + EditorApplication.hierarchyWindowItemOnGUI += ModifyStartUp; + } + [InitializeOnLoad] public class Startup { @@ -208,6 +446,10 @@ static Startup() //Debug.Log($"STARTUP!!!"); EditorApplication.hierarchyWindowItemOnGUI -= ModifyStartUp; EditorApplication.hierarchyWindowItemOnGUI += ModifyStartUp; + + //Somehow loading a new scene clears the static variable that we stored the font? + EditorSceneManager.sceneOpened -= ModifySceneChange; + EditorSceneManager.sceneOpened += ModifySceneChange; } } -} \ No newline at end of file +} From 00f94d5ee69a7e4b5edf10b91d5449bab76e3a72 Mon Sep 17 00:00:00 2001 From: Miguel Alcantara Date: Mon, 29 Oct 2018 06:30:33 -0400 Subject: [PATCH 3/3] Incorporated label window for both dropdown buttons. --- Editor/ModifyEditorStyle.cs | 154 +++++++++++++++++++++++------------- 1 file changed, 100 insertions(+), 54 deletions(-) diff --git a/Editor/ModifyEditorStyle.cs b/Editor/ModifyEditorStyle.cs index 14aea8c..7edeb25 100644 --- a/Editor/ModifyEditorStyle.cs +++ b/Editor/ModifyEditorStyle.cs @@ -6,6 +6,7 @@ using UnityEditor.SceneManagement; using System; using System.Reflection; +using RotaryHeart.Lib; public class ModifyEditorStyle { @@ -18,91 +19,106 @@ public class ModifyEditorStyle private static bool enable { - get{ - return EditorPrefs.GetBool("ModifyEditorStyle_Enable",true); + get + { + return EditorPrefs.GetBool("ModifyEditorStyle_Enable", true); } - set{ - EditorPrefs.SetBool("ModifyEditorStyle_Enable",value); + set + { + EditorPrefs.SetBool("ModifyEditorStyle_Enable", value); } } private static int fontSize { - get{ - return EditorPrefs.GetInt("ModifyEditorStyle_FontSize",11); + get + { + return EditorPrefs.GetInt("ModifyEditorStyle_FontSize", 11); } - set{ - EditorPrefs.SetInt("ModifyEditorStyle_FontSize",value); + set + { + EditorPrefs.SetInt("ModifyEditorStyle_FontSize", value); } } - private static int smallFontSize + private static int smallFontSize { - get{ - return EditorPrefs.GetInt("ModifyEditorStyle_SmallFontSize",9); + get + { + return EditorPrefs.GetInt("ModifyEditorStyle_SmallFontSize", 9); } - set{ - EditorPrefs.SetInt("ModifyEditorStyle_SmallFontSize",value); + set + { + EditorPrefs.SetInt("ModifyEditorStyle_SmallFontSize", value); } } - private static int bigFontSize + private static int bigFontSize { - get{ - return EditorPrefs.GetInt("ModifyEditorStyle_BigFontSize",12); + get + { + return EditorPrefs.GetInt("ModifyEditorStyle_BigFontSize", 12); } - set{ - EditorPrefs.SetInt("ModifyEditorStyle_BigFontSize",value); + set + { + EditorPrefs.SetInt("ModifyEditorStyle_BigFontSize", value); } } private static int paddingTop { - get{ - return EditorPrefs.GetInt("ModifyEditorStyle_PaddingTop",1); + get + { + return EditorPrefs.GetInt("ModifyEditorStyle_PaddingTop", 1); } - set{ - EditorPrefs.SetInt("ModifyEditorStyle_PaddingTop",value); + set + { + EditorPrefs.SetInt("ModifyEditorStyle_PaddingTop", value); } } private static int paddingBottom { - get{ - return EditorPrefs.GetInt("ModifyEditorStyle_PaddingBottom",2); + get + { + return EditorPrefs.GetInt("ModifyEditorStyle_PaddingBottom", 2); } - set{ - EditorPrefs.SetInt("ModifyEditorStyle_PaddingBottom",value); + set + { + EditorPrefs.SetInt("ModifyEditorStyle_PaddingBottom", value); } } - private static int selected + private static string selected { - get{ - string fontName = EditorPrefs.GetString("ModifyEditorStyle_Selected", defaultFont); - return Array.IndexOf(fonts, fontName); + get + { + return EditorPrefs.GetString("ModifyEditorStyle_Selected", defaultFont); } - set{ - EditorPrefs.SetString("ModifyEditorStyle_Selected", (value < fonts.Length && value >= 0) ? fonts[value] : defaultFont); + set + { + EditorPrefs.SetString("ModifyEditorStyle_Selected", string.IsNullOrEmpty(value) ? defaultFont : value); } } - private static int selectedBold + private static string selectedBold { - get{ - string fontName = EditorPrefs.GetString("ModifyEditorStyle_SelectedBold", defaultFont); - return Array.IndexOf(fonts, fontName); + get + { + return EditorPrefs.GetString("ModifyEditorStyle_SelectedBold", defaultFont); } - set{ - EditorPrefs.SetString("ModifyEditorStyle_SelectedBold", (value < fonts.Length && value >= 0) ? fonts[value] : defaultFont); + set + { + EditorPrefs.SetString("ModifyEditorStyle_SelectedBold", string.IsNullOrEmpty(value) ? defaultFont : value); } } private static string[] _fonts; private static string[] fonts { - get{ - if(_fonts == null) + get + { + if (_fonts == null) { _fonts = Font.GetOSInstalledFontNames(); } @@ -110,7 +126,7 @@ private static string[] fonts } } - private static IEnumerable GUISkinStyles + private static IEnumerable GUISkinStyles { get { @@ -145,7 +161,8 @@ private static IEnumerable EditorStylesGUIStyles private static IEnumerable NeedPadding { - get{ + get + { GUISkin skin = GUI.skin; yield return skin.label; yield return skin.textArea; @@ -243,7 +260,7 @@ private static IEnumerable InternalStyles } } - + #if UNITY_2018_3_OR_NEWER private class ModifyEditorStyleProvider : SettingsProvider { @@ -271,8 +288,39 @@ static void ModifyEditorStylePreference() enable = EditorGUILayout.BeginToggleGroup("Enable", enable); - selected = EditorGUILayout.Popup("Font", selected, fonts); - selectedBold = EditorGUILayout.Popup("Bold Font", selectedBold, fonts); + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.PrefixLabel("Font"); + if (EditorGUILayout.DropdownButton(new GUIContent(selected), FocusType.Keyboard)) + { + LabelWindow window = new LabelWindow(); + window.OnSelect = (string value, bool enabled) => + { + if (enabled) + selected = value; + else + selected = null; + }; + window.OpenLabelWindow(new Rect(Event.current.mousePosition, Vector2.one), fonts, new string[] { selected }, false, false, true, false); + } + EditorGUILayout.EndHorizontal(); + + + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.PrefixLabel("Bold Font"); + if (EditorGUILayout.DropdownButton(new GUIContent(selectedBold), FocusType.Keyboard)) + { + LabelWindow window = new LabelWindow(); + window.OnSelect = (string value, bool enabled) => + { + if (enabled) + selectedBold = value; + else + selectedBold = null; + }; + window.OpenLabelWindow(new Rect(Event.current.mousePosition, Vector2.one), fonts, new string[] { selectedBold }, false, false, true, false); + } + EditorGUILayout.EndHorizontal(); + EditorGUILayout.Space(); fontSize = EditorGUILayout.IntField("Font Size", fontSize); smallFontSize = EditorGUILayout.IntField("Small Font Size", smallFontSize); @@ -299,16 +347,14 @@ static void ModifyEditorStylePreference() static void Modify() { - if(!enable) return; + if (!enable) return; - string fontName = selected >= 0 && selected < fonts.Length ? fonts[selected] : defaultFont; - string boldFontName = selectedBold >= 0 && selectedBold < fonts.Length ? fonts[selectedBold] : defaultFont; - normalFont = Font.CreateDynamicFontFromOSFont(fontName, fontSize); - //bigFont = Font.CreateDynamicFontFromOSFont(fontName, bigFontSize); - smallFont = Font.CreateDynamicFontFromOSFont(fontName, smallFontSize); - boldFont = Font.CreateDynamicFontFromOSFont(boldFontName, fontSize); - smallBoldFont = Font.CreateDynamicFontFromOSFont(boldFontName, smallFontSize); + normalFont = Font.CreateDynamicFontFromOSFont(selected, fontSize); + //bigFont = Font.CreateDynamicFontFromOSFont(selected, bigFontSize); + smallFont = Font.CreateDynamicFontFromOSFont(selected, smallFontSize); + boldFont = Font.CreateDynamicFontFromOSFont(selectedBold, fontSize); + smallBoldFont = Font.CreateDynamicFontFromOSFont(selectedBold, smallFontSize); GUISkin skin = GUI.skin; //Debug.Log($"- : {skin.font?.name} {skin.font?.fontSize}"); @@ -349,7 +395,7 @@ static void Modify() foreach (var x in NeedPadding) { - if(x != null) + if (x != null) { //Debug.Log($"{x.name} -> {x.padding}"); var p = x.padding;