Unity ObjectField 代码选择文件过滤路径
在 Unity 的 ObjectField 中使用代码选择文件时,可以使用 EditorUtility.OpenFilePanelWithFilters 方法过滤路径。例如,只允许选择 png 和 jpg 文件:
string path = EditorUtility.OpenFilePanelWithFilters('Select Image', '', new string[] { 'Image files', 'png,jpg' });
if (!string.IsNullOrEmpty(path))
{
// do something with the selected file
}
在 ObjectField 中使用这个方法:
Texture2D texture = (Texture2D)EditorGUILayout.ObjectField('Texture', texture, typeof(Texture2D), false);
if (GUILayout.Button('Select Texture'))
{
string path = EditorUtility.OpenFilePanelWithFilters('Select Image', '', new string[] { 'Image files', 'png,jpg' });
if (!string.IsNullOrEmpty(path))
{
texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
}
}
这将在 ObjectField 下面显示一个按钮,点击它将打开一个文件对话框,允许选择 png 和 jpg 文件。选择的文件将被加载为 Texture2D 并在 ObjectField 中显示。
原文地址: http://www.cveoy.top/t/topic/lMsU 著作权归作者所有。请勿转载和采集!