@@ -49,7 +49,16 @@ protected class PrefabLightmapDataItem
4949 /// <summary>
5050 /// Internal value for determining if the bake was started as a result of this tool
5151 /// </summary>
52- protected bool StartedBaking ;
52+ protected bool BakingStarted ;
53+
54+ /// <summary>
55+ /// Index into the baking queue that is currently being processed
56+ /// </summary>
57+ protected int BakingIndex ;
58+ /// <summary>
59+ /// The queue of objects to be baked in the current run
60+ /// </summary>
61+ protected List < PrefabLightmapData > BakingItems ;
5362
5463 /// <summary>
5564 /// Export path text field
@@ -88,6 +97,8 @@ protected class PrefabLightmapDataItem
8897 /// </summary>
8998 protected Button ButtonBake ;
9099
100+ protected Button ButtonCancel ;
101+
91102 /// <summary>
92103 /// Static function for registering with the Editor interface
93104 /// </summary>
@@ -141,6 +152,7 @@ public void CreateGUI()
141152 this . ToggleAll = this . rootVisualElement . Q < Toggle > ( name = "ToggleAll" ) ;
142153 this . ButtonClear = this . rootVisualElement . Q < Button > ( name = "ButtonClear" ) ;
143154 this . ButtonBake = this . rootVisualElement . Q < Button > ( name = "ButtonBake" ) ;
155+ this . ButtonCancel = this . rootVisualElement . Q < Button > ( name = "ButtonCancel" ) ;
144156
145157 this . TextFieldPath . SetEnabled ( false ) ;
146158 this . TextFieldName . SetEnabled ( false ) ;
@@ -156,6 +168,7 @@ public void CreateGUI()
156168 this . ToggleAll . RegisterValueChangedCallback ( this . ToggleAllChangedEventHandler ) ;
157169 this . ButtonClear . clicked += this . ButtonClearClicked ;
158170 this . ButtonBake . clicked += this . ButtonBakeClicked ;
171+ this . ButtonCancel . clicked += this . ButtonCancelClicked ;
159172
160173 this . TextFieldPath . value = this . LastTextPath ;
161174 this . ToggleDefault . value = this . LastDefault ;
@@ -230,10 +243,41 @@ protected void ButtonBakeClicked()
230243 return ;
231244 }
232245
233- this . StartedBaking = true ;
246+ this . BakingStarted = true ;
247+ this . BakingItems = new List < PrefabLightmapData > ( ) ;
248+
249+ foreach ( PrefabLightmapDataItem item in this . ListViewBehaviours . itemsSource )
250+ if ( item . Selected == true )
251+ this . BakingItems . Add ( item . PrefabLightmap ) ;
234252
235253 Lightmapping . bakeCompleted += this . UpdatePrefabs ;
236- Lightmapping . BakeAsync ( ) ;
254+
255+ this . ListViewBehaviours . SetEnabled ( false ) ;
256+ this . ButtonBake . style . display = DisplayStyle . None ;
257+ this . ButtonCancel . style . display = DisplayStyle . Flex ;
258+
259+ this . BakingIndex = - 1 ;
260+ this . ProcessNextPrefab ( ) ;
261+ }
262+ /// <summary>
263+ /// Cacnel the previously started bake of the selected items
264+ /// </summary>
265+ protected void ButtonCancelClicked ( )
266+ {
267+ Lightmapping . bakeCompleted -= this . UpdatePrefabs ;
268+ Lightmapping . Cancel ( ) ;
269+ Lightmapping . ClearLightingDataAsset ( ) ;
270+
271+ this . BakingStarted = false ;
272+ this . BakingIndex = - 1 ;
273+ this . BakingItems . Clear ( ) ;
274+
275+ this . ButtonCancel . style . display = DisplayStyle . None ;
276+ this . ButtonBake . style . display = DisplayStyle . Flex ;
277+ this . ListViewBehaviours . SetEnabled ( true ) ;
278+
279+ foreach ( PrefabLightmapDataItem item in this . ListViewBehaviours . itemsSource )
280+ item . PrefabLightmap . gameObject . SetActive ( true ) ;
237281 }
238282
239283 /// <summary>
@@ -319,7 +363,7 @@ protected void ToggleAllChangedEventHandler(ChangeEvent<bool> changeEvent)
319363 /// </summary>
320364 protected void UpdateListView ( )
321365 {
322- if ( this . ListViewBehaviours == null )
366+ if ( this . ListViewBehaviours == null || this . ListViewBehaviours . enabledSelf == false )
323367 return ;
324368
325369 List < PrefabLightmapDataItem > items = this . FindPrefabLightmapItems ( ) ;
@@ -380,27 +424,42 @@ protected void UpdateListView()
380424
381425 this . ListViewBehaviours . Rebuild ( ) ;
382426 }
427+
383428 /// <summary>
384- /// UPdate the prefab with the lighting data and save the prefab to the file system
429+ /// Check for and process the next PrefabLightmapData item
385430 /// </summary>
386- protected void UpdatePrefabs ( )
431+ protected void ProcessNextPrefab ( )
387432 {
388- if ( this . StartedBaking == false )
389- return ;
433+ this . BakingIndex ++ ;
390434
391- int counter = 0 ;
392-
393- foreach ( PrefabLightmapDataItem item in this . ListViewBehaviours . itemsSource )
435+ if ( this . BakingItems . Count > this . BakingIndex )
394436 {
395- counter ++ ;
437+ foreach ( PrefabLightmapDataItem item in this . ListViewBehaviours . itemsSource )
438+ item . PrefabLightmap . gameObject . SetActive ( false ) ;
396439
397- string itemName = item . PrefabLightmap . gameObject . name + "-" + counter ;
440+ this . BakingItems [ this . BakingIndex ] . gameObject . SetActive ( true ) ;
398441
399- if ( item . Selected == false ) continue ;
442+ Lightmapping . ClearLightingDataAsset ( ) ;
443+ Lightmapping . BakeAsync ( ) ;
444+ }
445+ else this . ButtonCancelClicked ( ) ;
446+ }
447+ /// <summary>
448+ /// Update the prefab with the lighting data and save the prefab to the file system
449+ /// </summary>
450+ protected void UpdatePrefabs ( )
451+ {
452+ if ( this . BakingStarted == false )
453+ return ;
400454
401- PrefabLightmapInfo data = PrefabLightmapTool . GatherLightmapData ( item . PrefabLightmap . gameObject , this . TextFieldPath . value , itemName , this . TextFieldName . value ) ;
455+ if ( this . BakingIndex < 0 || this . BakingIndex >= this . BakingItems . Count )
456+ return ;
402457
403- PrefabLightmapData prefabLightmap = item . PrefabLightmap as PrefabLightmapData ;
458+ PrefabLightmapData prefabLightmap = this . BakingItems [ this . BakingIndex ] ;
459+
460+ if ( prefabLightmap . gameObject . activeInHierarchy == true )
461+ {
462+ PrefabLightmapInfo data = PrefabLightmapTool . GatherLightmapData ( prefabLightmap . gameObject , this . TextFieldPath . value , prefabLightmap . gameObject . name , this . TextFieldName . value ) ;
404463
405464 if ( prefabLightmap != null )
406465 {
@@ -430,23 +489,23 @@ protected void UpdatePrefabs()
430489 Data = data
431490 } ;
432491
433- GameObject targetPrefab = PrefabUtility . GetCorrespondingObjectFromOriginalSource ( item . PrefabLightmap . gameObject ) as GameObject ;
492+ GameObject targetPrefab = PrefabUtility . GetCorrespondingObjectFromOriginalSource ( prefabLightmap . gameObject ) as GameObject ;
434493
435494 if ( targetPrefab != null )
436495 {
437- GameObject root = PrefabUtility . GetOutermostPrefabInstanceRoot ( item . PrefabLightmap . gameObject ) ;
496+ GameObject root = PrefabUtility . GetOutermostPrefabInstanceRoot ( prefabLightmap . gameObject ) ;
438497
439498 if ( root != null )
440499 {
441- GameObject rootPrefab = PrefabUtility . GetCorrespondingObjectFromSource ( item . PrefabLightmap . gameObject ) ;
500+ GameObject rootPrefab = PrefabUtility . GetCorrespondingObjectFromSource ( prefabLightmap . gameObject ) ;
442501
443502 string rootPath = AssetDatabase . GetAssetPath ( rootPrefab ) ;
444503
445504 PrefabUtility . UnpackPrefabInstanceAndReturnNewOutermostRoots ( root , PrefabUnpackMode . OutermostRoot ) ;
446505
447506 try
448507 {
449- PrefabUtility . ApplyPrefabInstance ( item . PrefabLightmap . gameObject , InteractionMode . AutomatedAction ) ;
508+ PrefabUtility . ApplyPrefabInstance ( prefabLightmap . gameObject , InteractionMode . AutomatedAction ) ;
450509 }
451510 catch
452511 {
@@ -458,13 +517,13 @@ protected void UpdatePrefabs()
458517 }
459518 else
460519 {
461- PrefabUtility . ApplyPrefabInstance ( item . PrefabLightmap . gameObject , InteractionMode . AutomatedAction ) ;
520+ PrefabUtility . ApplyPrefabInstance ( prefabLightmap . gameObject , InteractionMode . AutomatedAction ) ;
462521 }
463522 }
464523 }
465524 }
466525
467- this . StartedBaking = false ;
526+ this . ProcessNextPrefab ( ) ;
468527 }
469528
470529 /// <summary>
0 commit comments