Search Unity

How to recreate the object loader circle button?

Discussion in 'Immediate Mode GUI (IMGUI)' started by DanVioletSagmiller, Jan 10, 2018.

  1. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    In the inspector, when there is an object field, to the right of it, is a little circular button. When you click the button it loads a window to find objects matching the type of that field.

    What is that button called, and can I recreate it? Either that, or I could add my own button, but I still need to know how to load object selection window, filtered to an asset type, and get the selected value.

    Background:
    I have a special property type that I can save a value as a float directly, or as an asset that is storing a float. If it is the float directly, that object is the only one with that value. if I set the reference to the asset, then multiple objects can share the same value.

    I already have a property drawer for it, which keeps that down to one field. If I drag an object over it, it switches the property layout to show an object field. If the value is set to none, it goes back to the local data value.

    The problem is that when it is showing the float value directly, there is no round object button. The only way to set it, is through dragging an existing asset on to it. I want that button to be there normally.

    - Thanks.
     
  2. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    Found step 1: It seems to be called "Object Picker Button" - which lead to "EditorGUIUtility.ShowObjectPicker" - This allows me to create a custom access method for it. even if there isn't an easy way to just add the Object Picker button.
     
  3. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    825
    The style for the object picker with the field:
    Code (CSharp):
    1. (GUIStyle)"ObjectField"
    Just the icon:
    Code (CSharp):
    1. EditorGUIUtility.FindTexture( "IN ObjectField" )
    Those all all icons I found:
    upload_2018-1-11_9-28-9.png

    Does this help you orare you already happy with your last solution? :)
     
  4. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    Yes! this is helpful. Now I just need to figure out how to pop up the little finder window for particular asset types and I'm golden. :D
     
  5. NAjhar

    NAjhar

    Joined:
    Feb 9, 2018
    Posts:
    1
    I think you can just get away with using this to pop it open from a call.

    EditorGUIUtility.ShowObjectPicker

    link
     
  6. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    @Johannski I've got everything working, except the icon part. I could not get the texture from that.

    Code (CSharp):
    1. EditorGUIUtility.FindTexture("ObjectField"); // also "IN ObjectField"
    always returns null. Is there something I'm missing?

    (side note, I downloaded the script to see all the internal textures, and was able to pull up the same results, it just seems like I need some additional line, like setting a style. )
     
    Last edited: Feb 26, 2018
  7. DanVioletSagmiller

    DanVioletSagmiller

    Joined:
    Aug 26, 2010
    Posts:
    204
    Got it, its not a texture I grab, but the style itself applied to a button:

    Code (CSharp):
    1. GUI.Button(dragRect, "", GUI.skin.GetStyle("IN ObjectField"));
     
    khaled24 likes this.
  8. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    825
    Ah very nice! I actually wouln't have guessed that. Good job :)
     
  9. CaseyHofland

    CaseyHofland

    Joined:
    Mar 18, 2016
    Posts:
    615
    I needed this as well and after a good hour of scratching my head, I found a way easier method!

    Code (CSharp):
    1. Rect myRect = new Rect(position);
    2. myRect.x += myRect.width - 20f;
    3. myRect.width = 20f;
    4.  
    5. if(GUI.Button(myRect, "", GUIStyle.none))
    6. {
    7.     EditorGUIUtility.ShowObjectPicker();
    8. }
    9. EditorGUI.ObjectField(position, property, fieldInfo.FieldType);
    By drawing the button before drawing the object field, the object picker button is blocked and won't be called. Combined with GUIStyle.none, you have a button that is in front of the object picker button, but 'shows' the object picker button. From there it's just adding your own code to your button and you're golden.