18
18
19
19
namespace BrunoMikoski . ScriptableObjectCollections
20
20
{
21
+
21
22
[ CustomEditor ( typeof ( ScriptableObjectCollection ) , true ) ]
22
23
public class CollectionCustomEditor : Editor
23
24
{
@@ -290,9 +291,9 @@ private void BindCollectionItemListItem(VisualElement targetElement, int targetI
290
291
Editor editor = EditorCache . GetOrCreateEditorForObject ( targetItem ) ;
291
292
292
293
Label titleLabel = foldout . Q < Label > ( ) ;
293
- titleLabel . RegisterCallback < MouseDownEvent , int > ( RenameItemAtIndex , targetIndex ) ;
294
+ titleLabel . RegisterCallback < MouseDownEvent , int > ( RenameItemAtIndex , targetIndex , TrickleDown . TrickleDown ) ;
294
295
295
- targetElement . RegisterCallback < MouseUpEvent , int > ( ShowOptionsForIndex , targetIndex ) ;
296
+ targetElement . parent . parent . RegisterCallback < MouseUpEvent , int > ( ShowOptionsForIndex , targetIndex ) ;
296
297
297
298
imguiContainer . onGUIHandler = ( ) =>
298
299
{
@@ -435,15 +436,35 @@ private void OnClickGenerateStaticFile(MouseUpEvent evt)
435
436
436
437
private void OnClickRemoveSelectedItems ( MouseUpEvent evt )
437
438
{
439
+
440
+
438
441
if ( ! collectionItemListView . selectedIndices . Any ( ) )
439
442
{
443
+ if ( ! EditorUtility . DisplayDialog ( $ "Delete Item",
444
+ $ "Are you sure you want to delete { filteredItems [ ^ 1 ] . name } ?", "Yes" , "No" ) )
445
+ {
446
+ return ;
447
+ }
448
+
440
449
DeleteItemAtIndex ( filteredItems . Count - 1 ) ;
441
450
}
442
451
else
443
452
{
453
+ if ( ! EditorUtility . DisplayDialog ( $ "Delete { collectionItemListView . selectedIndices . Count ( ) } Items",
454
+ $ "Are you sure you want to delete all { collectionItemListView . selectedIndices . Count ( ) } items?", "Yes" , "No" ) )
455
+ {
456
+ return ;
457
+ }
458
+
459
+ List < ScriptableObject > itemsToBeDuplicated = new List < ScriptableObject > ( ) ;
444
460
foreach ( int selectedIndex in collectionItemListView . selectedIndices )
445
461
{
446
- DeleteItemAtIndex ( selectedIndex ) ;
462
+ itemsToBeDuplicated . Add ( filteredItems [ selectedIndex ] ) ;
463
+ }
464
+
465
+ foreach ( ScriptableObject item in itemsToBeDuplicated )
466
+ {
467
+ DeleteItemAtIndex ( collection . IndexOf ( item ) ) ;
447
468
}
448
469
}
449
470
@@ -657,12 +678,6 @@ protected void DeleteItemAtIndex(int selectedIndex)
657
678
return ;
658
679
}
659
680
660
- if ( ! EditorUtility . DisplayDialog ( $ "Delete { scriptableObject . name } ",
661
- $ "Are you sure you want to delete { scriptableObject . name } ?", "Yes" , "No" ) )
662
- {
663
- return ;
664
- }
665
-
666
681
Undo . RecordObject ( collection , "Remove Item" ) ;
667
682
668
683
filteredItems . Remove ( scriptableObject ) ;
@@ -696,22 +711,47 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
696
711
{
697
712
if ( evt . button != 1 )
698
713
return ;
699
-
714
+
715
+ int selectedItemsCount = collectionItemListView . selectedIndices . Count ( ) ;
716
+ bool singleItemSelected = selectedItemsCount == 1 ;
717
+
700
718
ScriptableObject scriptableObject = filteredItems [ targetIndex ] ;
701
719
702
720
GenericMenu menu = new GenericMenu ( ) ;
703
721
704
- menu . AddItem (
705
- new GUIContent ( "Copy Values" ) ,
706
- false ,
707
- ( ) => { CopyCollectionItemUtility . SetSource ( scriptableObject ) ; }
708
- ) ;
722
+ if ( singleItemSelected )
723
+ {
724
+ menu . AddItem (
725
+ new GUIContent ( "Copy Values" ) ,
726
+ false ,
727
+ ( ) => { CopyCollectionItemUtility . SetSource ( scriptableObject ) ; }
728
+ ) ;
729
+ }
730
+ else
731
+ {
732
+ menu . AddDisabledItem (
733
+ new GUIContent ( "Copy Values" ) ,
734
+ false ) ;
735
+ }
709
736
if ( CopyCollectionItemUtility . CanPasteToTarget ( scriptableObject ) )
710
737
{
711
738
menu . AddItem (
712
739
new GUIContent ( "Paste Values" ) ,
713
740
false ,
714
- ( ) => { CopyCollectionItemUtility . ApplySourceToTarget ( scriptableObject ) ; }
741
+ ( ) =>
742
+ {
743
+ if ( selectedItemsCount > 0 )
744
+ {
745
+ foreach ( int selectedIndex in collectionItemListView . selectedIndices )
746
+ {
747
+ CopyCollectionItemUtility . ApplySourceToTarget ( filteredItems [ selectedIndex ] ) ;
748
+ }
749
+ }
750
+ else
751
+ {
752
+ CopyCollectionItemUtility . ApplySourceToTarget ( scriptableObject ) ;
753
+ }
754
+ }
715
755
) ;
716
756
}
717
757
else
@@ -724,39 +764,118 @@ private void ShowOptionsForIndex(MouseUpEvent evt, int targetIndex)
724
764
menu . AddItem (
725
765
new GUIContent ( "Duplicate Item" ) ,
726
766
false ,
727
- ( ) => { DuplicateItem ( targetIndex ) ; }
767
+ ( ) =>
768
+ {
769
+ if ( selectedItemsCount > 0 )
770
+ {
771
+ List < ScriptableObject > itemsToBeDuplicated = new List < ScriptableObject > ( ) ;
772
+ foreach ( int selectedIndex in collectionItemListView . selectedIndices )
773
+ {
774
+ itemsToBeDuplicated . Add ( filteredItems [ selectedIndex ] ) ;
775
+ }
776
+
777
+ foreach ( ScriptableObject item in itemsToBeDuplicated )
778
+ {
779
+ DuplicateItem ( item , false ) ;
780
+ }
781
+ }
782
+ else
783
+ {
784
+ DuplicateItem ( targetIndex ) ;
785
+ }
786
+ }
728
787
) ;
729
788
730
789
menu . AddItem (
731
790
new GUIContent ( "Delete Item" ) ,
732
791
false ,
733
- ( ) => { DeleteItemAtIndex ( targetIndex ) ; }
792
+ ( ) =>
793
+ {
794
+ if ( selectedItemsCount > 0 )
795
+ {
796
+ if ( ! EditorUtility . DisplayDialog ( $ "Delete { collectionItemListView . selectedIndices . Count ( ) } Items",
797
+ $ "Are you sure you want to delete all { collectionItemListView . selectedIndices . Count ( ) } items?", "Yes" , "No" ) )
798
+ {
799
+ return ;
800
+ }
801
+
802
+ List < ScriptableObject > itemsToBeDuplicated = new List < ScriptableObject > ( ) ;
803
+ foreach ( int selectedIndex in collectionItemListView . selectedIndices )
804
+ {
805
+ itemsToBeDuplicated . Add ( filteredItems [ selectedIndex ] ) ;
806
+ }
807
+
808
+ foreach ( ScriptableObject item in itemsToBeDuplicated )
809
+ {
810
+ DeleteItemAtIndex ( collection . IndexOf ( item ) ) ;
811
+ }
812
+ }
813
+ else
814
+ {
815
+ if ( ! EditorUtility . DisplayDialog ( $ "Delete Item",
816
+ $ "Are you sure you want to delete { filteredItems [ ^ 1 ] . name } ?", "Yes" , "No" ) )
817
+ {
818
+ return ;
819
+ }
820
+
821
+ DeleteItemAtIndex ( targetIndex ) ;
822
+ }
823
+ }
734
824
) ;
735
825
736
826
menu . AddSeparator ( "" ) ;
737
827
menu . AddItem (
738
828
new GUIContent ( "Select Asset" ) ,
739
829
false ,
740
- ( ) => { SelectItemAtIndex ( targetIndex ) ; }
830
+ ( ) =>
831
+ {
832
+ if ( selectedItemsCount > 0 )
833
+ {
834
+ SelectItemAtIndex ( collectionItemListView . selectedIndices . ToArray ( ) ) ;
835
+ }
836
+ else
837
+ {
838
+ SelectItemAtIndex ( targetIndex ) ;
839
+ }
840
+ }
741
841
) ;
742
842
743
843
menu . ShowAsContext ( ) ;
744
844
}
745
845
746
- private void SelectItemAtIndex ( int index )
846
+ private void SelectItemAtIndex ( params int [ ] index )
747
847
{
748
- ScriptableObject collectionItem = filteredItems [ index ] ;
749
- Selection . objects = new Object [ ] { collectionItem } ;
848
+ Object [ ] selectedObjects = new Object [ index . Length ] ;
849
+ for ( int i = 0 ; i < index . Length ; i ++ )
850
+ {
851
+ selectedObjects [ i ] = filteredItems [ index [ i ] ] ;
852
+ }
853
+ Selection . objects = selectedObjects ;
750
854
}
751
855
752
- private void DuplicateItem ( int index )
856
+ private void DuplicateItem ( int index , bool showRenameAfter = true )
753
857
{
754
858
ScriptableObject source = filteredItems [ index ] ;
859
+ DuplicateItem ( source , showRenameAfter ) ;
860
+ }
861
+
862
+ private void DuplicateItem ( ScriptableObject source , bool showRenameAfter )
863
+ {
755
864
CopyCollectionItemUtility . SetSource ( source ) ;
756
865
ScriptableObject newItem = AddNewItemOfType ( source . GetType ( ) , false ) ;
757
866
CopyCollectionItemUtility . ApplySourceToTarget ( newItem ) ;
758
- int targetIndex = filteredItems . IndexOf ( newItem ) ;
759
- RenameItemAtIndex ( targetIndex ) ;
867
+
868
+ if ( showRenameAfter )
869
+ {
870
+ int targetIndex = filteredItems . IndexOf ( newItem ) ;
871
+ RenameItemAtIndex ( targetIndex ) ;
872
+ }
873
+ else
874
+ {
875
+ AssetDatabaseUtils . RenameAsset ( newItem , $ "{ source . name } (Copy)") ;
876
+ AssetDatabase . SaveAssetIfDirty ( newItem ) ;
877
+ ReloadFilteredItems ( ) ;
878
+ }
760
879
}
761
880
762
881
private void RenameItemAtIndex ( MouseDownEvent evt , int targetIndex )
0 commit comments