回答編集履歴
1
解決した回答の記入
test
CHANGED
@@ -1,5 +1,253 @@
|
|
1
|
+
自分にて試行錯誤したところ、windowメッセージを取得することで欲しい機能を実装することができました。
|
2
|
+
|
3
|
+
|
4
|
+
|
1
|
-
|
5
|
+
下記に実装したコードを記しておきます。
|
6
|
+
|
2
|
-
|
7
|
+
```C#
|
8
|
+
|
3
|
-
|
9
|
+
class test : EditorWindow
|
10
|
+
|
4
|
-
|
11
|
+
{
|
12
|
+
|
13
|
+
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
|
14
|
+
|
15
|
+
private static extern int SetWindowLong32(HandleRef hWnd, int nIndex, int dwNewLong);
|
16
|
+
|
17
|
+
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
|
18
|
+
|
19
|
+
private static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, IntPtr dwNewLong);
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
[DllImport("user32.dll", EntryPoint = "DefWindowProcA")]
|
28
|
+
|
29
|
+
private static extern IntPtr DefWindowProc(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
[DllImport("user32.dll")]
|
34
|
+
|
35
|
+
private static extern System.IntPtr GetActiveWindow();
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
private HandleRef hMainWindow;
|
40
|
+
|
41
|
+
private IntPtr oldWndProcPtr;
|
42
|
+
|
43
|
+
private IntPtr newWndProcPtr;
|
44
|
+
|
45
|
+
private WndProcDelegate newWndProc;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
// P/Invokeの定義 (pinvoke.net参照)
|
50
|
+
|
51
|
+
public static IntPtr SetWindowLongPtr(HandleRef hWnd, int nIndex, IntPtr dwNewLong)
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
if (IntPtr.Size == 8)
|
56
|
+
|
57
|
+
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
|
58
|
+
|
59
|
+
else
|
60
|
+
|
61
|
+
{
|
62
|
+
|
63
|
+
return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
const int WM_DEVICECHANGE = 0x0219;
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
[StructLayout(LayoutKind.Sequential)]
|
76
|
+
|
77
|
+
struct DEV_BROADCAST_VOLUME
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
public uint dbch_Size;
|
82
|
+
|
83
|
+
public uint dbch_Devicetype;
|
84
|
+
|
85
|
+
public uint dbch_Reserved;
|
86
|
+
|
87
|
+
public uint dbch_Unitmask;
|
88
|
+
|
89
|
+
public ushort dbch_Flags;
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
private IntPtr wndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
|
96
|
+
|
97
|
+
{
|
98
|
+
|
99
|
+
if (msg == WM_DEVICECHANGE)
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
switch ((int)wParam)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
case 0x8000:
|
108
|
+
|
109
|
+
Debug.Log("デバイスが挿入され利用可能になりました。");
|
110
|
+
|
111
|
+
DEV_BROADCAST_VOLUME device = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_VOLUME));
|
112
|
+
|
113
|
+
uint deviceNum = device.dbch_Unitmask;
|
114
|
+
|
115
|
+
char deviceLet = (char)0;
|
116
|
+
|
117
|
+
for (int i = 0;i < 26; i++)
|
118
|
+
|
119
|
+
{
|
120
|
+
|
121
|
+
if (deviceNum == 0x01) deviceLet = (char)(i + 0x41);
|
122
|
+
|
123
|
+
deviceNum >>= 1;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
Debug.Log(deviceLet.ToString());
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
break;
|
132
|
+
|
133
|
+
case 0x0007:
|
134
|
+
|
135
|
+
//Debug.Log("デバイスが追加または削除されました。");
|
136
|
+
|
137
|
+
break;
|
138
|
+
|
139
|
+
case 0x8004:
|
140
|
+
|
141
|
+
//Debug.Log("デバイスが削除されました。");
|
142
|
+
|
143
|
+
break;
|
144
|
+
|
145
|
+
default:
|
146
|
+
|
147
|
+
//Debug.Log("wndProc msg:0x" + msg.ToString("x4") + " wParam:0x" + wParam.ToString("x4") + " lParam:0x" + lParam.ToString("x4"));
|
148
|
+
|
149
|
+
break;
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
return DefWindowProc(hWnd, msg, wParam, lParam);
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
/// 終了
|
164
|
+
|
165
|
+
~test()
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
Term();
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
/// 初期化(EditorWindowをShowした後にコール)
|
176
|
+
|
177
|
+
public void Init()
|
178
|
+
|
179
|
+
{
|
180
|
+
|
181
|
+
// ウインドウプロシージャをフックする
|
182
|
+
|
183
|
+
hMainWindow = new HandleRef(null, GetActiveWindow());
|
184
|
+
|
185
|
+
newWndProc = new WndProcDelegate(wndProc);
|
186
|
+
|
187
|
+
newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);
|
188
|
+
|
189
|
+
oldWndProcPtr = SetWindowLongPtr(hMainWindow, -4, newWndProcPtr);
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
public void Term()
|
196
|
+
|
197
|
+
{
|
198
|
+
|
199
|
+
SetWindowLongPtr(hMainWindow, -4, oldWndProcPtr);
|
200
|
+
|
201
|
+
hMainWindow = new HandleRef(null, IntPtr.Zero);
|
202
|
+
|
203
|
+
oldWndProcPtr = IntPtr.Zero;
|
204
|
+
|
205
|
+
newWndProcPtr = IntPtr.Zero;
|
206
|
+
|
207
|
+
newWndProc = null;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
void OnGUI()
|
214
|
+
|
215
|
+
{
|
216
|
+
|
217
|
+
// ウィンドウハンドルが切り替わったので初期化
|
218
|
+
|
219
|
+
if (hMainWindow.Handle == IntPtr.Zero)
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
Init();
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
}
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
上記のようにクラスを宣言し、Start関数内で下記のように呼ぶとウィンドウメッセージを取得できました。
|
236
|
+
|
5
|
-
お騒がせしました。
|
237
|
+
私の力不足でお騒がせしました。
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
```c#
|
242
|
+
|
243
|
+
void Start()
|
244
|
+
|
245
|
+
{
|
246
|
+
|
247
|
+
parent = ScriptableObject.CreateInstance<test>();
|
248
|
+
|
249
|
+
parent.Init();
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
```
|