You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity工具拖拽文件至资源管理器异常问题求助

Unity拖拽文件至资源管理器的问题与需求

需求背景

我正在开发一款Unity游戏工具,核心需求是:支持从资源管理器拖拽文件到工具内处理,处理完成后再将文件拖拽回资源管理器、记事本、画图等第三方应用。

问题现象

  • 无法将文件拖拽到资源管理器的文件/文件夹区域
  • 可以将文件“复制”到资源管理器左侧侧边栏的空白处或滚动条位置

当前实现代码

拖拽启动方法

private void StartDrag()
{ 
    var data = new DataObject();
    var ls = new System.Collections.Specialized.StringCollection();
    ls.Add("C:/ExeMask.log");
    data.SetFileDropList(ls);

    OleInitialize(null);

    int[] array = new int[1];
    DoDragDrop(data, new DropSource(), (int)DragDropEffects.Copy, array);

    OleUninitialize();
}

[DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int OleInitialize(Action lpVoid);

[DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void OleUninitialize();

[DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int DoDragDrop(System.Runtime.InteropServices.ComTypes.IDataObject dataObject, IOleDropSource dropSource, int allowedEffects, int[] finalEffect);

DropSource实现

[ComImport]
[Guid("00000121-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleDropSource
{
    [PreserveSig]
    int OleQueryContinueDrag(int fEscapePressed, [In][MarshalAs(UnmanagedType.U4)] int grfKeyState);

    [PreserveSig]
    int OleGiveFeedback([In][MarshalAs(UnmanagedType.U4)] int dwEffect);
}

public class DropSource : IOleDropSource
{
    public DropSource()
    {

    }

    public int OleQueryContinueDrag(int fEscapePressed, [In, MarshalAs(UnmanagedType.U4)] int grfKeyState)
    {
        QueryContinueDragEventArgs qcdevent = null;
        bool escapePressed = fEscapePressed != 0;
        DragAction cancel = DragAction.Continue;
        if (escapePressed)
        {
            cancel = DragAction.Cancel;
        }
        else if ((((grfKeyState & 1) == 0) && ((grfKeyState & 2) == 0)) && ((grfKeyState & 0x10) == 0))
        {
            cancel = DragAction.Drop;
        }
        qcdevent = new QueryContinueDragEventArgs(grfKeyState, escapePressed, cancel);
        //this.peer.OnQueryContinueDrag(qcdevent);
        switch (qcdevent.Action)
        {
            case DragAction.Drop:
                return 0x40100;

            case DragAction.Cancel:
                return 0x40101;
        }
        return 0;
    }

    public int OleGiveFeedback([In, MarshalAs(UnmanagedType.U4)] int dwEffect)
    {

        GiveFeedbackEventArgs gfbevent = new GiveFeedbackEventArgs((DragDropEffects)dwEffect, true);
        //this.peer.OnGiveFeedback(gfbevent);
        if (gfbevent.UseDefaultCursors)
        {
            return 0x40102;
        }
        return 0;
    }
}

已尝试的解决方案

  • 调用/不调用OleInitialize和OleUninitialize
  • 尝试导入WinForms的System.Windows.Forms.dll到Unity,但因无CIL导入失败
  • 使用new Control().DoDragDrop实现,结果与当前代码一致,但未使用完全匹配的dll
  • 以管理员身份运行应用

疑问

是否存在无需依赖GUI组件即可发起拖拽操作的库?只需要调用方法就能启动拖拽功能即可。我了解过C的实现方案,但对C不太熟悉。

内容的提问来源于stack exchange,提问作者REDIZIT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 17:50:33