Search Unity

show file field instead of image box for texture2d in editor

Discussion in 'Scripting' started by AntFitch, Aug 6, 2013.

  1. AntFitch

    AntFitch

    Joined:
    Jan 31, 2012
    Posts:
    243
    I have a custom inspector which requires the user to drop script and texture files into it. In the default inspector, the texture2D fields look just like the script fields (a field with the name of the file in it), but when I add the texture2D field to the custom inspector, it only displays an image box. Is there a way to change this? I checked out the GUILayout options, but didn't see anything.

    Here is the line of code:

    Texture2D icon = EditorGUILayout.ObjectField(icon, typeof(Texture2D), true) as Texture2D;
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Hey there :)

    $texture2d-field.png

    You can change this easily:
    Code (csharp):
    1.  
    2. // (A)
    3. icon = EditorGUILayout.ObjectField(icon, typeof(Texture2D)) as Texture2D;
    4.  
    5. // (B)
    6. EditorGUIUtility.LookLikeInspector();
    7. icon = EditorGUILayout.ObjectField(icon, typeof(Texture2D)) as Texture2D;
    8. EditorGUIUtility.LookLikeControls(); // back to normal
    9.  
    If using Unity 4 you can also take advantage of the new EditorGUIUtility.ShowObjectPicker and EditorGUIUtility.GetObjectPickerObject functions to display the object selection window. The benefit with this approach is that you can customize the object field to look exactly as you want. This is similar to the brush selection interface in Rotorz Tile System.

    I hope this helps!
     
    Last edited: Aug 8, 2013
  3. AntFitch

    AntFitch

    Joined:
    Jan 31, 2012
    Posts:
    243
    Thank you! :D