@@ -151,6 +151,86 @@ public void OpenPrefabStage_PrefabPathTakesPrecedenceOverPath()
151151 }
152152 }
153153
154+ [ Test ]
155+ public void SavePrefabStage_PersistsPrefabContentChanges ( )
156+ {
157+ string prefabPath = CreateTestPrefab ( "SaveStageRoot" ) ;
158+
159+ try
160+ {
161+ AssertOpenPrefabStage ( prefabPath ) ;
162+
163+ var stage = PrefabStageUtility . GetCurrentPrefabStage ( ) ;
164+ Assert . IsNotNull ( stage , "Expected prefab stage to be open." ) ;
165+
166+ var child = new GameObject ( "SavedChild" ) ;
167+ child . transform . SetParent ( stage . prefabContentsRoot . transform , false ) ;
168+
169+ var saveResult = ToJObject ( ManagePrefabs . HandleCommand ( new JObject
170+ {
171+ [ "action" ] = "save_prefab_stage"
172+ } ) ) ;
173+
174+ Assert . IsTrue ( saveResult . Value < bool > ( "success" ) , $ "Expected save to succeed but got: { saveResult } ") ;
175+ Assert . AreEqual ( prefabPath , saveResult [ "data" ] . Value < string > ( "prefabPath" ) ) ;
176+
177+ StageUtility . GoToMainStage ( ) ;
178+
179+ GameObject reloaded = AssetDatabase . LoadAssetAtPath < GameObject > ( prefabPath ) ;
180+ Assert . IsNotNull ( reloaded . transform . Find ( "SavedChild" ) , "Saved prefab should contain the new child after save_prefab_stage." ) ;
181+ }
182+ finally
183+ {
184+ StageUtility . GoToMainStage ( ) ;
185+ SafeDeleteAsset ( prefabPath ) ;
186+ }
187+ }
188+
189+ [ Test ]
190+ public void ClosePrefabStage_SaveBeforeClose_PersistsChanges ( )
191+ {
192+ string prefabPath = CreateTestPrefab ( "CloseSaveRoot" ) ;
193+
194+ try
195+ {
196+ AssertOpenPrefabStage ( prefabPath ) ;
197+
198+ var stage = PrefabStageUtility . GetCurrentPrefabStage ( ) ;
199+ Assert . IsNotNull ( stage , "Expected prefab stage to be open." ) ;
200+
201+ var child = new GameObject ( "CloseSavedChild" ) ;
202+ child . transform . SetParent ( stage . prefabContentsRoot . transform , false ) ;
203+
204+ var closeResult = ToJObject ( ManagePrefabs . HandleCommand ( new JObject
205+ {
206+ [ "action" ] = "close_prefab_stage" ,
207+ [ "saveBeforeClose" ] = true
208+ } ) ) ;
209+
210+ Assert . IsTrue ( closeResult . Value < bool > ( "success" ) , $ "Expected close with save to succeed but got: { closeResult } ") ;
211+ Assert . IsNull ( PrefabStageUtility . GetCurrentPrefabStage ( ) , "Prefab stage should be closed after close_prefab_stage." ) ;
212+
213+ GameObject reloaded = AssetDatabase . LoadAssetAtPath < GameObject > ( prefabPath ) ;
214+ Assert . IsNotNull ( reloaded . transform . Find ( "CloseSavedChild" ) , "Saved prefab should contain the new child after close_prefab_stage(saveBeforeClose: true)." ) ;
215+ }
216+ finally
217+ {
218+ StageUtility . GoToMainStage ( ) ;
219+ SafeDeleteAsset ( prefabPath ) ;
220+ }
221+ }
222+
223+ private static void AssertOpenPrefabStage ( string prefabPath )
224+ {
225+ var openResult = ToJObject ( ManagePrefabs . HandleCommand ( new JObject
226+ {
227+ [ "action" ] = "open_prefab_stage" ,
228+ [ "prefabPath" ] = prefabPath
229+ } ) ) ;
230+
231+ Assert . IsTrue ( openResult . Value < bool > ( "success" ) , $ "Expected open to succeed but got: { openResult } ") ;
232+ }
233+
154234 private static string CreateTestPrefab ( string rootName )
155235 {
156236 string prefabPath = Path . Combine ( TempDirectory , $ "{ rootName } .prefab") . Replace ( '\\ ' , '/' ) ;
0 commit comments