1+ using System . Collections . Generic ;
2+ using UnityEditor ;
3+ using UnityEngine ;
4+
5+ namespace BrunoMikoski . ScriptableObjectCollections
6+ {
7+ public static class SOCollectionsProjectContextMenus
8+ {
9+ // ================================
10+ // ISOCItem (ScriptableObjectCollectionItem) commands
11+ // ================================
12+
13+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Move to Different Collection" , true ) ]
14+ private static bool ValidateMoveToDifferentCollection ( )
15+ {
16+ Object [ ] selectedObjects = Selection . objects ;
17+ if ( selectedObjects == null || selectedObjects . Length == 0 )
18+ return false ;
19+ // Check that every selected object implements ISOCItem.
20+ foreach ( Object obj in selectedObjects )
21+ {
22+ ISOCItem socItem = obj as ISOCItem ;
23+ if ( socItem == null )
24+ return false ;
25+ }
26+ return true ;
27+ }
28+
29+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Move to Different Collection" ) ]
30+ private static void MoveToDifferentCollection ( )
31+ {
32+ Object [ ] selectedObjects = Selection . objects ;
33+ List < ISOCItem > items = new List < ISOCItem > ( ) ;
34+ foreach ( Object obj in selectedObjects )
35+ {
36+ if ( obj is ISOCItem item )
37+ items . Add ( item ) ;
38+ }
39+ if ( items . Count == 0 )
40+ return ;
41+
42+ // Get available collections for the item type.
43+ List < ScriptableObjectCollection > possibleCollections =
44+ CollectionsRegistry . Instance . GetCollectionsByItemType ( items [ 0 ] . GetType ( ) ) ;
45+ if ( possibleCollections == null || possibleCollections . Count == 0 )
46+ {
47+ EditorUtility . DisplayDialog ( "Move to Different Collection" , "No collections available." , "OK" ) ;
48+ return ;
49+ }
50+ // Exclude the current collection of the first item.
51+ ScriptableObjectCollection currentCollection = items [ 0 ] . Collection ;
52+ List < ScriptableObjectCollection > filteredCollections = new List < ScriptableObjectCollection > ( ) ;
53+ foreach ( ScriptableObjectCollection collection in possibleCollections )
54+ {
55+ if ( collection != currentCollection )
56+ filteredCollections . Add ( collection ) ;
57+ }
58+ if ( filteredCollections . Count == 0 )
59+ {
60+ EditorUtility . DisplayDialog ( "Move to Different Collection" , "No other collections available." , "OK" ) ;
61+ return ;
62+ }
63+ // Present a GenericMenu so the user can choose a new collection.
64+ GenericMenu menu = new GenericMenu ( ) ;
65+ foreach ( ScriptableObjectCollection collection in filteredCollections )
66+ {
67+ menu . AddItem ( new GUIContent ( collection . name ) , false , delegate
68+ {
69+ foreach ( ISOCItem item in items )
70+ {
71+ SOCItemUtility . MoveItem ( item , collection ) ;
72+ }
73+ EditorUtility . SetDirty ( collection ) ;
74+ } ) ;
75+ }
76+ menu . ShowAsContext ( ) ;
77+ }
78+
79+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Select Collection" , true ) ]
80+ private static bool ValidateSelectCollection ( )
81+ {
82+ Object [ ] selectedObjects = Selection . objects ;
83+ if ( selectedObjects == null || selectedObjects . Length != 1 )
84+ return false ;
85+ ISOCItem socItem = selectedObjects [ 0 ] as ISOCItem ;
86+ return socItem != null && socItem . Collection != null ;
87+ }
88+
89+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Select Collection" ) ]
90+ private static void SelectCollection ( )
91+ {
92+ Object [ ] selectedObjects = Selection . objects ;
93+ if ( selectedObjects == null || selectedObjects . Length != 1 )
94+ return ;
95+ ISOCItem socItem = selectedObjects [ 0 ] as ISOCItem ;
96+ if ( socItem != null && socItem . Collection != null )
97+ Selection . activeObject = socItem . Collection ;
98+ }
99+
100+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Duplicate" , true ) ]
101+ private static bool ValidateDuplicateItem ( )
102+ {
103+ Object [ ] selectedObjects = Selection . objects ;
104+ if ( selectedObjects == null || selectedObjects . Length == 0 )
105+ return false ;
106+ foreach ( Object obj in selectedObjects )
107+ {
108+ if ( ! ( obj is ScriptableObject ) )
109+ return false ;
110+ }
111+ return true ;
112+ }
113+
114+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Duplicate" ) ]
115+ private static void DuplicateItem ( )
116+ {
117+ Object [ ] selectedObjects = Selection . objects ;
118+ foreach ( Object obj in selectedObjects )
119+ {
120+ string assetPath = AssetDatabase . GetAssetPath ( obj ) ;
121+ if ( string . IsNullOrEmpty ( assetPath ) )
122+ continue ;
123+ string newPath = AssetDatabase . GenerateUniqueAssetPath ( assetPath ) ;
124+ bool copySuccess = AssetDatabase . CopyAsset ( assetPath , newPath ) ;
125+ if ( copySuccess )
126+ AssetDatabase . Refresh ( ) ;
127+ }
128+ }
129+
130+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Delete" , true ) ]
131+ private static bool ValidateDeleteItem ( )
132+ {
133+ Object [ ] selectedObjects = Selection . objects ;
134+ if ( selectedObjects == null || selectedObjects . Length == 0 )
135+ return false ;
136+ foreach ( Object obj in selectedObjects )
137+ {
138+ if ( ! ( obj is ScriptableObject ) )
139+ return false ;
140+ }
141+ return true ;
142+ }
143+
144+ [ MenuItem ( "Assets/ScriptableObjectCollections/ISOCItem/Delete" ) ]
145+ private static void DeleteItem ( )
146+ {
147+ Object [ ] selectedObjects = Selection . objects ;
148+ if ( EditorUtility . DisplayDialog ( "Delete Item" ,
149+ "Are you sure you want to delete the selected item(s)?" , "Yes" , "No" ) )
150+ {
151+ List < ScriptableObjectCollection > objectCollections = new List < ScriptableObjectCollection > ( ) ;
152+ foreach ( Object obj in selectedObjects )
153+ {
154+ string assetPath = AssetDatabase . GetAssetPath ( obj ) ;
155+ if ( obj is ISOCItem socItem )
156+ objectCollections . Add ( socItem . Collection ) ;
157+
158+ if ( ! string . IsNullOrEmpty ( assetPath ) )
159+ AssetDatabase . DeleteAsset ( assetPath ) ;
160+ }
161+
162+ foreach ( ScriptableObjectCollection objectCollection in objectCollections )
163+ {
164+ objectCollection . RefreshCollection ( ) ;
165+ }
166+
167+ AssetDatabase . Refresh ( ) ;
168+ }
169+ }
170+
171+ // ================================
172+ // ScriptableObjectCollection commands
173+ // ================================
174+
175+ [ MenuItem ( "Assets/ScriptableObjectCollections/ScriptableObjectCollection/Duplicate Collection" , true ) ]
176+ private static bool ValidateDuplicateCollection ( )
177+ {
178+ Object [ ] selectedObjects = Selection . objects ;
179+ if ( selectedObjects == null || selectedObjects . Length != 1 )
180+ return false ;
181+ return selectedObjects [ 0 ] is ScriptableObjectCollection ;
182+ }
183+
184+ [ MenuItem ( "Assets/ScriptableObjectCollections/ScriptableObjectCollection/Duplicate Collection" ) ]
185+ private static void DuplicateCollection ( )
186+ {
187+ Object [ ] selectedObjects = Selection . objects ;
188+ if ( selectedObjects == null || selectedObjects . Length != 1 )
189+ return ;
190+ ScriptableObjectCollection originalCollection = selectedObjects [ 0 ] as ScriptableObjectCollection ;
191+ if ( originalCollection == null )
192+ return ;
193+ string assetPath = AssetDatabase . GetAssetPath ( originalCollection ) ;
194+ if ( string . IsNullOrEmpty ( assetPath ) )
195+ return ;
196+ string newPath = AssetDatabase . GenerateUniqueAssetPath ( assetPath ) ;
197+ bool copySuccess = AssetDatabase . CopyAsset ( assetPath , newPath ) ;
198+ if ( copySuccess )
199+ {
200+ AssetDatabase . Refresh ( ) ;
201+ CollectionsRegistry . Instance . ValidateCollections ( ) ;
202+ EditorUtility . DisplayDialog ( "Duplicate Collection" ,
203+ "Collection duplicated successfully at:\n " + newPath , "OK" ) ;
204+ }
205+ else
206+ {
207+ EditorUtility . DisplayDialog ( "Duplicate Collection" ,
208+ "Failed to duplicate collection." , "OK" ) ;
209+ }
210+ }
211+ }
212+ }
0 commit comments