@@ -75,7 +75,7 @@ public override string ToString()
7575 /// ウィンドウ操作ができる状態ならtrueを返す
7676 /// </summary>
7777 /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
78- public bool IsActive { get { return hWnd != IntPtr . Zero ; } }
78+ public bool IsActive { get { return ( hWnd != IntPtr . Zero && WinApi . IsWindow ( hWnd ) ) ; } }
7979
8080 /// <summary>
8181 /// ウィンドウが最大化されていればtrue
@@ -664,6 +664,12 @@ private static IntPtr MessageCallback(int nCode, IntPtr wParam, ref WinApi.MSG l
664664 /// </summary>
665665 public void BeginFileDrop ( )
666666 {
667+ if ( ! IsActive )
668+ {
669+ EndFileDrop ( ) ;
670+ return ;
671+ }
672+
667673 if ( ! instances . Contains ( this ) ) {
668674 instances . Add ( this ) ;
669675 }
@@ -684,26 +690,31 @@ public void EndFileDrop()
684690 EndHook ( ) ;
685691 instances . Remove ( this ) ;
686692
693+ if ( ! IsActive ) return ;
694+
695+ // ドロップを受け付ける状態を元に戻す
687696 long exstyle = this . CurrentWindowExStyle ;
688- exstyle &= ~ WinApi . WS_EX_ACCEPTFILES ; // ドロップ受付をやめる
697+ exstyle &= ~ WinApi . WS_EX_ACCEPTFILES ;
689698 exstyle |= ( WinApi . WS_EX_ACCEPTFILES & this . OriginalWindowExStyle ) ; // 元からドロップ許可ならやはり受付
690699 this . CurrentWindowExStyle = exstyle ;
691700 WinApi . SetWindowLong ( hWnd , WinApi . GWL_EXSTYLE , this . CurrentWindowExStyle ) ;
692-
693701 }
694702
695703 /// <summary>
696704 /// メッセージフックを開始
697705 /// </summary>
698706 private void BeginHook ( )
699707 {
700- if ( IsHookSet ) return ;
708+ // ウィンドウが操作不可なら設定はできない
701709 if ( ! IsActive )
702710 {
703- IsHookSet = false ;
711+ EndHook ( ) ; // フック設定なしということにしておく
704712 return ;
705713 }
706714
715+ // フック設定済みなら二重には設定しない
716+ if ( IsHookSet ) return ;
717+
707718 // フックを設定
708719 uint threadId = WinApi . GetCurrentThreadId ( ) ;
709720 IntPtr module = WinApi . GetModuleHandle ( null ) ;
@@ -718,7 +729,6 @@ private void BeginHook()
718729 }
719730
720731 IsHookSet = true ;
721-
722732 //Debug.Log("BeginHook");
723733 }
724734
@@ -733,9 +743,6 @@ private void EndHook()
733743 myHook = IntPtr . Zero ;
734744 myHookCallback = null ;
735745
736- // Unityだと通常はドラッグ不可のはずなので戻しても良いのかも
737- WinApi . DragAcceptFiles ( hWnd , false ) ;
738-
739746 //Debug.Log("EndHook");
740747 }
741748 IsHookSet = false ;
@@ -746,7 +753,7 @@ private void EndHook()
746753 /// </summary>
747754 public void Dispose ( )
748755 {
749- if ( hWnd != IntPtr . Zero && WinApi . IsWindow ( hWnd ) )
756+ if ( IsActive )
750757 {
751758 EndFileDrop ( ) ;
752759#if UNITY_EDITOR
0 commit comments