- Home /
Can not you change the default GUI style of EditorGUI in OnInspectorGUI?
Information about the GUI skins on the Internet usually tells you to open a separate editor window and work there. However, I would like to use only some of the fonts, etc. in the basic editor component window.
GUISkin skin;
public override void OnInspectorGUI()
{
StyleInit(); //Edit the internal GUI styles of the GUI skin.
GUI.skin = skin;
base.OnInspectorGUI();
script.test = GUILayout.TextField(script.test);
}
void StyleInit()
{
skin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector);
skin.font = font;
labelStyle = new GUIStyle(EditorStyles.label);
labelStyle.font = font;
labelStyle.fontSize = defaultFontSize;
skin.label = labelStyle;
popupStyle = new GUIStyle(EditorStyles.popup);
popupStyle.font = font;
popupStyle.fontSize = defaultFontSize;
popupStyle.fixedHeight = defaultFontSize;
textFieldStyle = new GUIStyle(EditorStyles.textField);
textFieldStyle.font = font;
textFieldStyle.fontSize = defaultFontSize;
textFieldStyle.fixedHeight = defaultFontSize + 5;
skin.textField = textFieldStyle;
buttonStyle = new GUIStyle(EditorStyles.miniButton);
buttonStyle.font = font;
buttonStyle.fontSize = defaultFontSize;
buttonStyle.fixedHeight = defaultFontSize + 10;
skin.button = buttonStyle;
checkStyle = new GUIStyle(EditorStyles.toggle);
checkStyle.font = font;
checkStyle.fontSize = defaultFontSize;
checkStyle.fixedHeight = defaultFontSize + 10;
skin.toggle = checkStyle;
boxStyle = new GUIStyle(GUI.skin.box);
boxStyle.font = font;
boxStyle.alignment = TextAnchor.UpperLeft;
boxStyle.fontSize = defaultFontSize;
skin.box = boxStyle;
submitStyle = new GUIStyle(EditorStyles.miniButton);
submitStyle.font = font;
submitStyle.fontSize = (int)(defaultFontSize * 1.5f);
submitStyle.fixedHeight = defaultFontSize * 2;
objectStyle = new GUIStyle(EditorStyles.objectField);
objectStyle.font = font;
objectStyle.fontSize = defaultFontSize;
objectStyle.fixedHeight = defaultFontSize + 5;
//boxStyle.fixedHeight = defaultFontSize + 10;
}
Here is the Edtior script for the component that wants to change the GUI skin. I created a GUI skin file in the Editor Default Resource folder as shown in this link, loaded it into EditorGUIUtility, and applied the GUI.skin attribute to my GUI skin before running the base function in OnInspectorGUI.
However, no matter what method you use, you can not control the style of the inspector that is drawn in Unity by default. (The above function called base's OnInspectorGUI, but it did not call DrawDefaultInspector)
As you can see from the last TextField, when I manually set the TextField through the GUILayout call in the editor script, my intention was to come up without adding a separate GUIStyle. However, it is very troublesome to specify a field in the editor whenever you want to express more variables. Because of this, I want to follow the basic inspecting method of basic expression and change only some GUI style such as font.
Is this impossible? If you want to output TextFields and so on in modified fonts, is there only a way to make TextFields correspond to each other in the editor script?
Answer by Heukhyeon · Jan 09, 2018 at 02:36 AM
good. This was a much simpler problem than I thought. The reason I did not change the existing EditorStyle directly was that the output properties of existing components such as Transform changed when the property was directly changed.
But after these steps, I could only change the EditorStyle of my intended component.
GUIStyle label;
GUIStyle textField;
Font font;
public override void OnInspectorGUI()
{
BackUP();
base.OnInspectorGUI();
Revert();
}
void BackUP()
{
label = new GUIStyle(EditorStyles.label);
textField = new GUIStyle(EditorStyles.textField);
EditorStyles.label.font = font;
EditorStyles.textField.font = font;
}
void Revert()
{
RevertStyle(EditorStyles.label, label);
RevertStyle(EditorStyles.textField, textField);
}
void RevertStyle(GUIStyle target,GUIStyle backup)
{
target.font = backup.font;
target.fixedHeight = backup.fixedHeight;
}
However, at this time, when FixedHeight is modified, TextField or Label may appear to invade another property area. (That is, the height of the current property is not calculated properly.) This is true even if wrapped in BeginVertical ~ EndVertical of EditorGUILayout.
Your answer
Follow this Question
Related Questions
How to assign a rigidbody in custom inspector? 1 Answer
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Questions Regarding Images in Custom Inspector/Editor 1 Answer
Make a custom inspector that shows a group of variables in form of list 2 Answers
How to Hide/Show List or Array in the inspector based on a variable? 0 Answers