I couldn’t find an Editor GUI object that would allow the selection of images restricted to a particular file path(s), so I made my own object that does what I need. I’ll put the code here because I’m a nice guy and am sure that someone could use this someday. Good luck and happy coding.
TexturePicker.cs
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public delegate void ImageSelectedHandler(Texture2D imageName);
public class TexturePickerEditor : EditorWindow
{
public List<Texture2D> images = new List<Texture2D>();
/// <summary>
/// A flag to indicate if the editor window has been setup or not.
/// </summary>
private bool isSetup = false;
private ImageSelectedHandler handler;
#region Setup
/// <summary>
/// Attempts to setup the editor by reading in textures from specified path.
/// </summary>
/// <param name='path'>
/// Path to load images from.
/// </param>
public void Setup(string path, ImageSelectedHandler functionHandler)
{
string[] paths = new string[] { path };
Setup(paths, functionHandler);
} // eo Setup
/// <summary>
/// Attempts to setup the editor by reading in all textures specified
/// by the various paths. Supports multiple paths of textures.
/// </summary>
/// <param name='paths'>
/// Paths of textures to read in.
/// </param>
public void Setup(string[] paths, ImageSelectedHandler functionHandler)
{
isSetup = true;
ReadInAllTextures(paths);
handler = functionHandler;
} // eo Setup
#endregion Setup
#region GUI
Vector2 scrollPosition = Vector2.zero;
void OnGUI()
{
if(!isSetup)
return;
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
// create a button for each image loaded in, 4 buttons in width
// calls the handler when a new image is selected.
int counter = 0;
foreach(Texture2D img in images)
{
if(counter % 4 == 0 || counter == 0)
EditorGUILayout.BeginHorizontal();
++counter;
if(GUILayout.Button(img, GUILayout.Height(100), GUILayout.Width(100)))
{
// tell handler about new image, close selection window
handler(img);
EditorWindow.focusedWindow.Close();
}
if(counter % 4 == 0)
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
} // eo OnGUI
#endregion GUI
#region Utility
/// <summary>
/// Reads the in all textures from the paths.
/// </summary>
/// <param name='paths'>
/// The paths to read images from.
/// </param>
void ReadInAllTextures(string[] paths)
{
foreach(string path in paths)
{
string[] allFilesInPath = Directory.GetFiles(path);
foreach(string filePath in allFilesInPath)
{
Texture2D obj = (Texture2D)AssetDatabase.LoadAssetAtPath(filePath, typeof(Texture2D));
if(obj is Texture2D)
{
images.Add(obj as Texture2D);
}
}
}
} // eo ReadInAllTextures
#endregion Utility
} // End TexturePickerEditor
EditorUtilities.cs
/// <summary>
/// Shows a texture with a label and a button to select a new image
/// from a list of images loaded from the path specified. This allows
/// a selection of an image from a subset of images, unlike the UnityEditor.ObjectField
/// that pulls all images from /Assets/
/// </summary>
/// <param name='label'>
/// Label to display.
/// </param>
/// <param name='selectedImage'>
/// Selected image that shows in the interface.
/// </param>
/// <param name='yPosition'>
/// How far down in the interface to show this tool.
/// </param>
/// <param name='textureFilePath'>
/// Texture file path containing the images to load.
/// </param>
/// <param name='functionHandler'>
/// The function to handle the selection of a new texture.
/// </param>
public static void TexturePreviewWithSelection(string label, Texture selectedImage, float yPosition,
string textureFilePaths, ImageSelectedHandler functionHandler)
{
TexturePreviewWithSelection(label, selectedImage, yPosition, new string[] { textureFilePaths }, functionHandler);
} // eo TexturePreviewWithSelection
/// <summary>
/// Shows a texture with a label and a button to select a new image
/// from a list of images loaded from the paths specified. This allows
/// a selection of an image from a subset of images, unlike the UnityEditor.ObjectField
/// that pulls all images from /Assets/
/// </summary>
/// <param name='label'>
/// Label to display.
/// </param>
/// <param name='selectedImage'>
/// Selected image that shows in the interface.
/// </param>
/// <param name='yPosition'>
/// How far down in the interface to show this tool.
/// </param>
/// <param name='textureFilePaths'>
/// Texture file paths containing the images to load.
/// </param>
/// <param name='functionHandler'>
/// The function to handle the selection of a new texture.
/// </param>
public static void TexturePreviewWithSelection(string label, Texture selectedImage, float yPosition,
string[] textureFilePaths, ImageSelectedHandler functionHandler)
{
EditorGUILayout.BeginVertical(GUILayout.Height(125));
{
EditorGUILayout.LabelField(label);
EditorGUI.DrawPreviewTexture(new Rect(50,yPosition,100,100), selectedImage);
// used to center the select texture button
EditorGUILayout.BeginVertical();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.Space();
if(GUILayout.Button("Select Texture", GUILayout.MaxWidth(100)))
{
EditorUtilities.TexturePicker(textureFilePaths, functionHandler);
}
EditorGUILayout.Space();
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndVertical();
}
EditorGUILayout.EndVertical();
} // eo TexturePreviewWithSelection
public static void TexturePicker(string path, ImageSelectedHandler functionHandler)
{
EditorUtilities.TexturePicker(new string[] { path }, functionHandler);
} // eo TexturePicker
/// <summary>
/// Creates a window with buttons to select a new image.
/// </summary>
/// <param name='paths'>
/// Paths to load images from.
/// </param>
/// <param name='functionHandler'>
/// How to handle the new image selection.
/// </param>
public static void TexturePicker(string[] paths, ImageSelectedHandler functionHandler)
{
TexturePickerEditor picker = (TexturePickerEditor)EditorWindow.GetWindow(typeof(TexturePickerEditor), true, "Texture Picker");
picker.Setup(paths, functionHandler);
} // eo TexturePicker