■DragAndDrop用テキストフィールドを作成する関数
private string CreateDragAngDropTextField(string label,string text,bool isDirectory)
{
var rect = EditorGUILayout.GetControlRect();
var unityEvent = Event.current;
switch (unityEvent.type)
{
case EventType.DragUpdated:
if (rect.Contains(unityEvent.mousePosition))
{
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
Event.current.Use();
}
break;
case EventType.DragPerform:
if (rect.Contains(unityEvent.mousePosition))
{
string path = DragAndDrop.paths[0];
var attr = File.GetAttributes(path);
DragAndDrop.AcceptDrag();
if (isDirectory)
{
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
text = path;
else
text = Path.GetDirectoryName(text);
}
else
{
if ((attr & FileAttributes.Directory) != FileAttributes.Directory)
text = Path.GetFileName(path);
}
Event.current.Use();
}
break;
}
return EditorGUI.TextField(rect, label, text);
}
■利用例
public class HogeEditor : Editor {
private SerializedProperty m_directory;
private SerializedProperty m_fileName;
private void OnEnable()
{
m_directory= base.serializedObject.FindProperty("directory");
m_fileName= base.serializedObject.FindProperty("fileName");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
m_directory.stringValue = CreateDragAngDropTextField("Directory",m_assetBundleName.stringValue, true);
m_fileName.stringValue = CreateDragAngDropTextField("File", m_spriteName.stringValue, false);
serializedObject.ApplyModifiedProperties();
}
private string CreateDragAngDropTextField(string label,string text,bool isDirectory){
...
}
}