-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileBridge.cs
87 lines (70 loc) · 1.89 KB
/
FileBridge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
namespace DLavender.Windows
{
public static class FileBridge
{
#region Internal
private static class Imports
{
[DllImport("FileDragBridge.dll")]
public static extern void AddHook(DragEndCallback callback);
[DllImport("FileDragBridge.dll")]
public static extern void RemoveHook();
}
private delegate void DragEndCallback(int length, IntPtr arrayPointer);
[AOT.MonoPInvokeCallback(typeof(DragEndCallback))]
private static void onBegin(int length, IntPtr arrayPointer)
{
var paths = new List<string>(length);
var arrayResult = new IntPtr[length];
Marshal.Copy(arrayPointer, arrayResult, 0, length);
for (int i = 0; i < length; i++)
{
string res = Marshal.PtrToStringUni(arrayResult[i]);
paths.Add(res);
}
OnDragFiles?.Invoke(paths);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void OnDomainReload()
{
OnDragFiles = null;
}
#endregion
#region API
/// <summary>
/// Register delegates functions here.
/// </summary>
public static event OnDragEvent OnDragFiles;
/// <summary>
/// Calling convention of delegate.
/// </summary>
public delegate void OnDragEvent(List<string> path);
/// <summary>
/// Subscribe window messages from win api to grab drag event.
/// </summary>
public static void Enable()
{
#if !UNITY_EDITOR_WIN && UNITY_STANDALONE_WIN
Imports.AddHook(onBegin);
#elif UNITY_EDITOR_WIN
Debug.Log("FileBridge doesn't work on Editor Platform");
#endif
}
/// <summary>
/// Unsubscribe to disable drag feature.
/// </summary>
public static void Disable()
{
#if !UNITY_EDITOR_WIN && UNITY_STANDALONE_WIN
Imports.RemoveHook();
#elif UNITY_EDITOR_WIN
Debug.Log("FileBridge doesn't work on Editor Platform");
#endif
}
#endregion
}
}