- Home /
How do I use EditorGUIUtility.ShowObjectPicker()? C#
http://docs.unity3d.com/Documentation/ScriptReference/EditorGUIUtility.ShowObjectPicker.html
No code example, documentation is vague, and google yields nothing. Anyone know what they're doing with this?
Also why couldn't it be: Object ShowObjectPicker( ObjectClass class, bool showSceneObjects )
As it is a unity editor function, I assume that's the most streamlined method. (Most everything else in editor is thread pausing, why not the object picker?)
you can call it from OnGUI() for editor scripts and it will show up.
EditorGUIUtility.ShowObjectPicker<UnityEngine.GameObject>(new UnityEngine.Object(), false, " ", 2);
as for applying it for practical use, the docs still have no information. I have discovered that you can filter objects by providing the type in the , t talks about messages, but no messages get receive that corresponded to the docs, also it talks about an ExecuteCommand event that i should receive, i found something similar to that (EditorGUIUtility.CommandEvent) which returns an event i tried to apply the
`EditorGUIUtility.GetObjectPickerObject();`
in OnGUI but no avail (Null Reference, UnityEngine has no idea what ShowObjectPicker I'm talking about). I'm interested in finding out on how to use it.
Also with regards to the question about why the object picker can't be a blocking call- most GUI stuff is in fact not blocking (nor should it be). Though I do agree that the way they chose to implement getting the return value from the picker isn't the greatest.
Answer by hklown · Nov 26, 2013 at 07:26 PM
To act on the "events" provided by the object picker, try this somewhere in the body of your OnGUI
method:
if (Event.current.commandName == "ObjectSelectorUpdated")
{
// do a thing relating to the object picker
}
Take a look at the Event class for some more info about consuming the events posted by the Object picker.
Answer by ecesis_llc · Feb 22, 2016 at 12:20 AM
I was still not sure how you use the control ID for a picker window. Here is what I did to get multiple object fields working with the picker.
Use the following code to open up a picker window with a specific control ID.
//in your EditorWindow class
int currentPickerWindow;
void ShowPicker() {
//create a window picker control ID
currentPickerWindow = EditorGUIUtility.GetControlID(FocusType.Passive) + 100;
//use the ID you just created
EditorGUIUtility.ShowObjectPicker<GameObject>(null,false,"ee",currentPickerWindow);
}
Then, in your method that draws the object field check for the event commandname "ObjectSelectorUpdated" and check if the open picker has your custom controlID. EditorGUIUtility.GetObjectPickerObject() should be the picker object you want.
Object effectGO = null;
if( Event.current.commandName == "ObjectSelectorUpdated" && EditorGUIUtility.GetObjectPickerControlID() == currentPickerWindow )
{
effectGO = EditorGUIUtility.GetObjectPickerObject();
currentPickerWindow = -1;
//name of selected object from picker
Debug.Log(effectGO.name);
}
The usage of control IDs makes this the correct answer I$$anonymous$$O. Not safe to use the picker window otherwise.
Yes, the usage of the control ID is very good. Also added the following lines, just for safety reasons.
if (Event.current.commandName == "ObjectSelectorClosed") {
currentPickerWindow = -1;
}
Just for curious, can I call GetObjectPikerObject() when commandName is "ObjectSelectorClosed"? Like when I double-click to choose an object in the selector and it's automatically closed. Is this when commandName is "ObjectSelectorClosed"?
Answer by Probe-Games · Dec 18, 2013 at 08:35 PM
This really pissed me off. That goddamn showobjectpicker.
Anyways, I found a script from some Japanese place that works. He basically made it looked at normal objects. I only changed it to Texture2D.
So if you check here and just change the Object texts to Texture2D. It'll work.