@@ -71,11 +71,45 @@ internal static void CloseAllWindows()
7171 }
7272 }
7373
74- public static void ShowWindow ( )
75- {
76- var window = GetWindow < MCPForUnityEditorWindow > ( "MCP For Unity" ) ;
77- window . minSize = new Vector2 ( 500 , 340 ) ;
78- }
74+ public static void ShowWindow ( )
75+ {
76+ var existingWindows = UnityEngine . Resources . FindObjectsOfTypeAll < MCPForUnityEditorWindow > ( ) ;
77+ MCPForUnityEditorWindow window = null ;
78+
79+ if ( existingWindows . Length > 0 )
80+ {
81+ window = existingWindows [ 0 ] ;
82+
83+ // If multiple instances exist, keep one and close the extras to avoid stale hidden tabs.
84+ for ( int i = 1 ; i < existingWindows . Length ; i ++ )
85+ {
86+ try
87+ {
88+ existingWindows [ i ] . Close ( ) ;
89+ }
90+ catch ( Exception ex )
91+ {
92+ McpLog . Warn ( $ "Error closing duplicate MCP window: { ex . Message } ") ;
93+ }
94+ }
95+ }
96+ else
97+ {
98+ window = GetWindow < MCPForUnityEditorWindow > ( "MCP For Unity" ) ;
99+ }
100+
101+ window . titleContent = new GUIContent ( "MCP For Unity" ) ;
102+ window . minSize = new Vector2 ( 500 , 340 ) ;
103+
104+ if ( window . position . width < 100 || window . position . height < 100 )
105+ {
106+ window . position = new Rect ( 120 , 120 , 900 , 700 ) ;
107+ }
108+
109+ window . Show ( ) ;
110+ window . ShowTab ( ) ;
111+ window . Focus ( ) ;
112+ }
79113
80114 // Helper to check and manage open windows from other classes
81115 public static bool HasAnyOpenWindow ( )
@@ -125,7 +159,8 @@ public void CreateGUI()
125159 return ;
126160 }
127161
128- visualTree . CloneTree ( rootVisualElement ) ;
162+ rootVisualElement . Clear ( ) ;
163+ visualTree . CloneTree ( rootVisualElement ) ;
129164
130165 // Load main window USS
131166 var mainStyleSheet = AssetDatabase . LoadAssetAtPath < StyleSheet > (
0 commit comments