Object field used with the advanced search picker.
[UxmlElement]public class ObjectField : BaseField<Object>, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, ICustomStyle, IBindable, IMixedValueSupport, INotifyValueChanged<Object>
Open the Search's advanced picker with a customized view. Here is an example that shows how to use this object field in a custom window:
using UnityEditor;using UnityEditor.Search;using UnityEngine;using UnityEngine.Search;using UnityEngine.UIElements;class MyCustomWindowWithObjectField : EditorWindow{ TextField m_TextField; [MenuItem("Tests/My Custom Window/Open")] static void OpenWindow() { var window = CreateWindow<MyCustomWindowWithObjectField>(); window.Show(); } void CreateGUI() { // Create a SearchContext for our object selector. We want to search for materials. var searchContext = SearchService.CreateContext("t:material"); // Create the SearchViewFlags for our object selector. We want it to show as a borderless window, in grid view and without the ability to show the saved search queries. var searchViewFlags = SearchViewFlags.Borderless | SearchViewFlags.GridView | SearchViewFlags.DisableSavedSearchQuery; // Create the SearchViewState of our object selector. var searchViewState = new SearchViewState(searchContext, searchViewFlags); // Hide the group bar in this object selector, and set the group we want to show searchViewState.hideTabs = true; searchViewState.group = "all"; // Group that shows all results plus the "None" item. This is the default. // Configure the object field by setting the SearchViewState, SearchViewFlags and SearchContext. // Not all members are needed at the same time. If you provide a SearchViewState, you don't need // to provide the SearchContext and the SearchViewFlags since they are already contained in it. // If you provide none of these values, default ones will be used. var objectField = new UnityEditor.Search.ObjectField("My Material") { searchContext = searchContext, searchViewFlags = searchViewFlags, searchViewState = searchViewState }; objectField.RegisterCallback<ChangeEvent<UnityEngine.Object>>(OnObjectFieldChanged); rootVisualElement.Add(objectField); m_TextField = new TextField("My Material Name") { value = "Null" }; m_TextField.SetEnabled(false); rootVisualElement.Add(m_TextField); } void OnObjectFieldChanged(ChangeEvent<Object> evt) { if (m_TextField == null) return; m_TextField.value = evt.newValue?.name ?? "Null"; }}