using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(FilePathAttribute))]
public class FilePathDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
FilePathAttribute fi = (FilePathAttribute)attribute;
if (property.propertyType == SerializedPropertyType.String)
{
EditorGUI.BeginProperty(position, label, property);
Rect textFieldPosition = new Rect(position.x, position.y, position.width - 35, position.height);
property.stringValue = EditorGUI.TextField(textFieldPosition, label, property.stringValue);
Rect buttonPosition = new Rect(position.x + position.width - 30, position.y, 30, position.height);
if (GUI.Button(buttonPosition, "Browse"))
{
string path = EditorUtility.OpenFilePanel("Select File", "", fi.extension);
if (!string.IsNullOrEmpty(path))
{
property.stringValue = path;
}
}
EditorGUI.EndProperty();
}
else
{
EditorGUI.LabelField(position, label.text, "Use FilePath with string.");
}
}
}
Теперь, когда вы посмотрите на класс Example в инспекторе Unity, вы увидите текстовое поле с кнопкой "Browse", которая позволит выбрать файл и установить путь в текстовое поле.
Таким образом, вы визуально указываете, что это поле является путем до файла и облегчаете выбор файла.