How to style EditorGUILayout.ObjectField() ?

Hey dear people,

I note that the EditorGUILayout.ObjectField() function does not accept a GUIStyle parameter despite having 10 overloads and that all other EditorGUILayout.*Field() functions do provide an a GUIStyle parameter in their overloads.

I’m just wondering if anyone has a known solution to styling ObjectFields ??

Many thanks! :slight_smile:

Thanks @RobAnthem and @Bunny83 :slight_smile:

I’ve ended up creating this function to accept a style for object field display… It works pretty well.

public static void DrawProObjectField<T>(
			GUIContent label, 
			SerializedProperty value, 
			Type objType, 
			GUIStyle style, 
			bool allowSceneObjects, 
			Texture objIcon = null,
			params GUILayoutOption[] options) where T: UnityEngine.Object
		{

			T tObj =  value.objectReferenceValue as T;

			if (objIcon == null) {
				objIcon = EditorGUIUtility.FindTexture("PrefabNormal Icon");
			} 
			style.imagePosition = ImagePosition.ImageLeft;

			int pickerID = 455454425;

			if (tObj != null) {
				EditorGUILayout.LabelField(label, 
					new GUIContent(tObj.name, objIcon), style, options);
			}

			if (GUILayout.Button("Select")) {
				EditorGUIUtility.ShowObjectPicker<T>(
					tObj, allowSceneObjects,"", pickerID);

			}
			if (Event.current.commandName == "ObjectSelectorUpdated")
			{
				if (EditorGUIUtility.GetObjectPickerControlID() == pickerID)
				{
					tObj = EditorGUIUtility.GetObjectPickerObject() as T;
					value.objectReferenceValue = tObj;
				}
			}

		}

Hello,
I have found a simpler solution. Say, for example, Il want to have the text of the ObjectField in red. Then, the in my custom Drawer I have

public override void OnGUI( Rect position, SerializedProperty property,
                                GUIContent label )
    {
        Type objType = ... // get type of desired object (see comment below)
         Color savedColor = EditorStyles.objectField.normal.textColor;
        EditorStyles.objectField.normal.textColor = Color.red;
        EditorGUI.ObjectField( fieldRect, "Custom", objRef, objType, true );
        EditorStyles.objectField.normal.textColor = savedColor;
}

To get the type from the property, see A smarter Way to get the Type of SerializedProperty - Questions & Answers - Unity Discussions