Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closeobj | The object to be selected by default. |
allowSceneObjects | Is selection of Scene objects allowed, or should it only show assets. |
searchFilter | The default search filter to apply. This is the initial search query text that applies when the object picker is opened. |
controlID | The id of the control to set. This is useful if you are showing more than one of these. You can get the value at a later time. |
Show the object picker from code.
<T>: The type of objects that are available to pick in the object picker.
Once the user interacts with the object picker, it responds by sending ExecuteCommand
events back to the OnGUI
that called this function. The messages are:
ObjectSelectorUpdated
: The selected object was changed. Call GetObjectPickerObject to read the selected object.ObjectSelectorClosed
: The user closed the object picker.ObjectSelectorCanceled
: The user canceled the picking operation and the object picked closed.Here is an example of how it could be called for any control during an OnGUI call:
static void CallShowPicker<T>(UnityEngine.Object currentObjectValue, int currentControlId) where T : UnityEngine.Object { // Show an object picker with the `currentObjectValue` selected. We pass true to allow // scene objects. We set the initial search query string to an empty string. The `currentControlId` // is passed to us during the OnGUI call, for any object field or other control that needs to show the object picker. EditorGUIUtility.ShowObjectPicker<T>(currentObjectValue, true, "", currentControlId); }
Here is how an object field that uses the previous method could be setup during an OnGUI call:
public static void DoObjectField(Rect position, SerializedProperty property, Type objType, GUIContent label) { label = EditorGUI.BeginProperty(position, label, property); // Generate a controlId for this object field. Use a unique hint integer for correct matching of controls. var controlId = GUIUtility.GetControlID("Example_EditorGUIUtility_ShowObjectPicker".GetHashCode(), FocusType.Keyboard, position); position = EditorGUI.PrefixLabel(position, controlId, label); DoObjectField(position, position, controlId, objType, property); EditorGUI.EndProperty(); }
Finally, here is how you could handle the ExecuteCommand events during an OnGUI call:
static bool HandleCommands(SerializedProperty property, ref UnityEngine.Object currentObjectValue, UnityEngine.Object originalObjectValue, Type objectType, int currentControlId) { var evt = Event.current; switch (evt.type) { case EventType.ExecuteCommand: string commandName = evt.commandName; if (commandName == "ObjectSelectorUpdated" && EditorGUIUtility.GetObjectPickerControlID() == currentControlId) { currentObjectValue = AssignSelectedObject(EditorGUIUtility.GetObjectPickerObject(), property, objectType, evt); return true; } if (commandName == "ObjectSelectorClosed" && EditorGUIUtility.GetObjectPickerControlID() == currentControlId) { return true; } if (commandName == "ObjectSelectorCanceled" && EditorGUIUtility.GetObjectPickerControlID() == currentControlId) { if (property != null) property.objectReferenceValue = originalObjectValue; else currentObjectValue = originalObjectValue; return true; } break; default: return false; } return false; }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.