@@ -14,9 +14,6 @@ namespace Depra.Ecs.Hybrid.Editor
1414 [ CustomEditor ( typeof ( EntityRecipe ) ) ]
1515 internal sealed class EntityRecipeEditor : UnityEditor . Editor
1616 {
17- private static void DrawBundlesHeader ( Rect rect ) => EditorGUI . LabelField ( rect , "Component Bundles" ) ;
18- private static void DrawSourcesHeader ( Rect rect ) => EditorGUI . LabelField ( rect , "Component Sources" ) ;
19-
2017 private ReorderableList _componentBundles ;
2118 private SerializedProperty _componentBundlesProperty ;
2219 private ReorderableList _componentSources ;
@@ -38,15 +35,14 @@ private void OnEnable()
3835 _componentSources = new ReorderableList ( serializedObject , _componentSourcesProperty , true , true , true , true )
3936 {
4037 drawHeaderCallback = DrawSourcesHeader ,
41- drawElementCallback = DrawSourceElement
38+ drawElementCallback = DrawSourceElement ,
4239 } ;
4340 }
4441
4542 public override void OnInspectorGUI ( )
4643 {
4744 serializedObject . Update ( ) ;
48- var recipe = ( EntityRecipe ) target ;
49- DrawControlButtons ( recipe ) ;
45+ DrawControlButtons ( ( EntityRecipe ) target ) ;
5046 EditorGUILayout . Space ( 5 ) ;
5147 _componentBundles . DoLayoutList ( ) ;
5248 _componentSources . DoLayoutList ( ) ;
@@ -142,13 +138,27 @@ private void DrawBundleElement(Rect rect, int index, bool isActive, bool isFocus
142138 if ( componentBundle )
143139 {
144140 var actionRect = new Rect ( rect . x + rect . width - 45 , rect . y , 45 , rect . height ) ;
141+ var pendingOriginalPath = isNested ? GetPendingDeletionPath ( componentBundle ) : null ;
142+ var hasPendingOriginal = ! string . IsNullOrEmpty ( pendingOriginalPath ) ;
143+
145144 if ( ! isNested )
146145 {
147146 if ( GUI . Button ( actionRect , "Nest" ) )
148147 {
149148 MakeNested ( recipe , index ) ;
150149 }
151150 }
151+ else if ( hasPendingOriginal )
152+ {
153+ var originalColor = GUI . backgroundColor ;
154+ GUI . backgroundColor = new Color ( 1f , 0.3f , 0.3f ) ;
155+ if ( GUI . Button ( actionRect , "Del" ) )
156+ {
157+ DeleteOriginal ( componentBundle , pendingOriginalPath ) ;
158+ }
159+
160+ GUI . backgroundColor = originalColor ;
161+ }
152162 else
153163 {
154164 if ( GUI . Button ( actionRect , "Out" ) )
@@ -172,15 +182,9 @@ private void OnBundleRemoved(ReorderableList list)
172182 var componentBundle = element . objectReferenceValue as ComponentDatabase ;
173183 var recipe = ( EntityRecipe ) target ;
174184
175- Undo . RecordObject ( recipe , "Remove Set" ) ;
176-
177- if ( componentBundle )
185+ if ( componentBundle && IsNestedInAsset ( componentBundle , recipe ) )
178186 {
179- recipe . Remove ( componentBundle ) ;
180- if ( IsNestedInAsset ( componentBundle , recipe ) )
181- {
182- Undo . DestroyObjectImmediate ( componentBundle ) ;
183- }
187+ Undo . DestroyObjectImmediate ( componentBundle ) ;
184188 }
185189
186190 ReorderableList . defaultBehaviours . DoRemoveButton ( list ) ;
@@ -229,6 +233,7 @@ private void MakeAllNested(EntityRecipe recipe, List<ComponentDatabase> bundles)
229233
230234 var newNested = CreateNestedCopy ( recipe , componentBundle ) ;
231235 _componentBundlesProperty . GetArrayElementAtIndex ( index ) . objectReferenceValue = newNested ;
236+ SavePendingDeletion ( newNested , componentBundle ) ;
232237 convertedCount ++ ;
233238 }
234239
@@ -249,12 +254,45 @@ private void MakeNested(EntityRecipe recipe, int index)
249254 Undo . RecordObject ( recipe , $ "Make Nested: { original . name } ") ;
250255 var newNested = CreateNestedCopy ( recipe , original ) ;
251256 _componentBundlesProperty . GetArrayElementAtIndex ( index ) . objectReferenceValue = newNested ;
252-
257+ SavePendingDeletion ( newNested , original ) ;
253258 serializedObject . ApplyModifiedProperties ( ) ;
254259 EditorUtility . SetDirty ( recipe ) ;
255260 AssetDatabase . SaveAssets ( ) ;
256261 }
257262
263+ private static void DeleteOriginal ( ComponentDatabase nested , string originalPath )
264+ {
265+ if ( nested == null || string . IsNullOrEmpty ( originalPath ) )
266+ {
267+ return ;
268+ }
269+
270+ if ( ! File . Exists ( originalPath ) )
271+ {
272+ ClearPendingDeletion ( nested ) ;
273+ Debug . LogWarning ( $ "Original file no longer exists: { originalPath } ") ;
274+ return ;
275+ }
276+
277+ if ( EditorUtility . DisplayDialog (
278+ "Delete Original File" ,
279+ $ "Delete original file?\n \n { originalPath } \n \n This cannot be undone!",
280+ "Delete" ,
281+ "Cancel" ) )
282+ {
283+ if ( AssetDatabase . DeleteAsset ( originalPath ) )
284+ {
285+ ClearPendingDeletion ( nested ) ;
286+ AssetDatabase . SaveAssets ( ) ;
287+ Debug . Log ( $ "Deleted original file: { originalPath } ") ;
288+ }
289+ else
290+ {
291+ Debug . LogError ( $ "Failed to delete: { originalPath } ") ;
292+ }
293+ }
294+ }
295+
258296 private static ComponentDatabase CreateNestedCopy ( EntityRecipe recipe , ComponentDatabase original )
259297 {
260298 var copy = Instantiate ( original ) ;
@@ -320,5 +358,47 @@ private static ComponentDatabase ExtractToNewAsset(ComponentDatabase nested, str
320358
321359 return copy ;
322360 }
361+
362+ private static void DrawBundlesHeader ( Rect rect ) => EditorGUI . LabelField ( rect , "Component Bundles" ) ;
363+ private static void DrawSourcesHeader ( Rect rect ) => EditorGUI . LabelField ( rect , "Component Sources" ) ;
364+
365+ private static string GetSessionStateKey ( string nestedGuid ) => $ "EntityRecipe_PendingDeletion_{ nestedGuid } ";
366+
367+ private static void SavePendingDeletion ( ComponentDatabase nested , ComponentDatabase original )
368+ {
369+ if ( ! nested || ! original )
370+ {
371+ return ;
372+ }
373+
374+ var nestedPath = AssetDatabase . GetAssetPath ( nested ) ;
375+ var nestedGuid = AssetDatabase . AssetPathToGUID ( nestedPath ) ;
376+ var originalPath = AssetDatabase . GetAssetPath ( original ) ;
377+ SessionState . SetString ( GetSessionStateKey ( nestedGuid ) , originalPath ) ;
378+ }
379+
380+ private static string GetPendingDeletionPath ( ComponentDatabase nested )
381+ {
382+ if ( nested == null )
383+ {
384+ return null ;
385+ }
386+
387+ var nestedPath = AssetDatabase . GetAssetPath ( nested ) ;
388+ var nestedGuid = AssetDatabase . AssetPathToGUID ( nestedPath ) ;
389+ return SessionState . GetString ( GetSessionStateKey ( nestedGuid ) , null ) ;
390+ }
391+
392+ private static void ClearPendingDeletion ( ComponentDatabase nested )
393+ {
394+ if ( nested == null )
395+ {
396+ return ;
397+ }
398+
399+ var nestedPath = AssetDatabase . GetAssetPath ( nested ) ;
400+ var nestedGuid = AssetDatabase . AssetPathToGUID ( nestedPath ) ;
401+ SessionState . EraseString ( GetSessionStateKey ( nestedGuid ) ) ;
402+ }
323403 }
324404}
0 commit comments