1
1
using LitJson ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . Text ;
4
5
using UnityEditor ;
6
+ using UnityEditor . AddressableAssets ;
7
+ using UnityEditor . AddressableAssets . Build ;
8
+ using UnityEditor . AddressableAssets . Settings . GroupSchemas ;
5
9
using UnityEditor . EditorTools ;
6
10
using UnityEngine ;
7
11
@@ -15,11 +19,13 @@ public class AssetGroupEditor:EditorWindow
15
19
16
20
private Vector2 _scrollView = Vector2 . zero ;
17
21
private string [ ] _assetFilter ;
22
+ private string [ ] _labels = new string [ ] { "none" } ;
23
+ private List < string > _profileVariables = new List < string > { "LocalBuildPath" , "LocalLoadPath" } ;
18
24
19
25
private EditorMenuItemView _menuItem ;
20
26
private EditorFormView _editorForm ;
21
27
22
- [ MenuItem ( "Tools/Assets Management/Asset Group #&G" ) ]
28
+ [ MenuItem ( "Tools/Asset Management/Asset Group #&G" ) ]
23
29
private static void OpenWindow ( )
24
30
{
25
31
GetWindow < AssetGroupEditor > ( "Asset Group Editor" ) ;
@@ -28,6 +34,12 @@ private static void OpenWindow()
28
34
private void OnFocus ( )
29
35
{
30
36
_assetFilter = AssetFilterEditor . GetAssetFilters ( ) . ToArray ( ) ;
37
+ var settings = AddressableAssetSettingsDefaultObject . Settings ;
38
+ if ( settings != null )
39
+ {
40
+ _labels = settings . GetLabels ( ) . ToArray ( ) ;
41
+ _profileVariables = settings . profileSettings . GetVariableNames ( ) ;
42
+ }
31
43
}
32
44
33
45
private void OnEnable ( )
@@ -39,48 +51,182 @@ private void OnEnable()
39
51
_config . SetJsonType ( JsonType . Array ) ;
40
52
}
41
53
54
+ //var settings = AddressableAssetSettingsDefaultObject.Settings;
55
+ //if (settings != null)
56
+ //{
57
+ // _labels = settings.GetLabels().ToArray();
58
+ // _profileVariables = settings.profileSettings.GetVariableNames();
59
+ //}
60
+
42
61
OnMenuInit ( ) ;
43
- _assetFilter = AssetFilterEditor . GetAssetFilters ( ) . ToArray ( ) ;
62
+ // _assetFilter = AssetFilterEditor.GetAssetFilters().ToArray();
44
63
_editorForm = new EditorFormView ( _config ) ;
45
64
OnFormInit ( ) ;
46
65
}
47
66
48
67
private void OnMenuInit ( )
49
68
{
50
69
_menuItem = new EditorMenuItemView ( ) ;
51
- _menuItem . SetMenuItem ( "File" , new string [ ] { "Save Config" , "Exit" } , ( itemIndex ) => {
52
- if ( itemIndex == 0 )
70
+ _menuItem . SetMenuItem ( "File" , new string [ ] { "Save Config" , "Set Addressables" , "Exit" } , ( itemIndex ) => {
71
+ if ( itemIndex == 0 )
53
72
{
54
73
ProjectSettingsConfig . SaveJsonData ( _configName , _config ) ;
55
74
EditorUtility . DisplayDialog ( "Save Config" , "Data saved successfully!" , "OK" ) ;
56
75
}
76
+ else if ( itemIndex == 1 )
77
+ {
78
+ var settings = AddressableAssetSettingsDefaultObject . Settings ;
79
+ if ( settings != null )
80
+ {
81
+ string binPath = ContentUpdateScript . GetContentStateDataPath ( false ) ;
82
+ if ( _config != null && _config . Count > 0 )
83
+ {
84
+ for ( int i = 0 ; i < _config . Count ; i ++ )
85
+ {
86
+ JsonData item = _config [ i ] ;
87
+ string groupName = item [ "GroupName" ] . ToString ( ) ;
88
+ var group = settings . FindGroup ( groupName ) ;
89
+ if ( group == null )
90
+ {
91
+ group = settings . CreateGroup ( groupName , false , false , false , null ) ;
92
+ }
93
+ BundledAssetGroupSchema bagSchema = group . GetSchema < BundledAssetGroupSchema > ( ) ;
94
+ if ( bagSchema == null )
95
+ {
96
+ bagSchema = group . AddSchema < BundledAssetGroupSchema > ( ) ;
97
+ }
98
+ bagSchema . BuildPath . SetVariableByName ( settings , item [ "BuildPath" ] . ToString ( ) ) ;
99
+ bagSchema . LoadPath . SetVariableByName ( settings , item [ "LoadPath" ] . ToString ( ) ) ;
100
+
101
+ ContentUpdateGroupSchema cugSchema = group . GetSchema < ContentUpdateGroupSchema > ( ) ;
102
+ if ( cugSchema == null )
103
+ {
104
+ cugSchema = group . AddSchema < ContentUpdateGroupSchema > ( ) ;
105
+ }
106
+ cugSchema . StaticContent = ( ( int ) item [ "UpdateRestriction" ] == 1 ) ;
107
+
108
+ //Filter
109
+ StringBuilder filterBuilder = new StringBuilder ( ) ;
110
+ for ( int filterIndex = 0 ; filterIndex < item [ "Filter" ] . Count ; filterIndex ++ )
111
+ {
112
+ filterBuilder . Append ( $ "t:{ item [ "Filter" ] [ filterIndex ] . ToString ( ) } ") ;
113
+ }
114
+ //SearchInFolders
115
+ List < string > folders = new List < string > ( ) ;
116
+ for ( int folderIndex = 0 ; folderIndex < item [ "SearchInFolders" ] . Count ; folderIndex ++ )
117
+ {
118
+ folders . Add ( item [ "SearchInFolders" ] [ folderIndex ] . ToString ( ) ) ;
119
+ }
120
+ //Labels
121
+ List < string > labels = new List < string > ( ) ;
122
+ for ( int labelIndex = 0 ; labelIndex < item [ "Labels" ] . Count ; labelIndex ++ )
123
+ {
124
+ labels . Add ( item [ "Labels" ] [ labelIndex ] . ToString ( ) ) ;
125
+ }
126
+
127
+ //Find All Asset
128
+ var findAssets = AssetDatabase . FindAssets ( filterBuilder . ToString ( ) , folders . ToArray ( ) ) ;
129
+ for ( int findIndex = 0 ; findIndex < findAssets . Length ; findIndex ++ )
130
+ {
131
+ string guid = findAssets [ findIndex ] ;
132
+ string assetPath = AssetDatabase . GUIDToAssetPath ( guid ) ;
133
+ if ( AssetDatabase . IsValidFolder ( assetPath ) || assetPath . EndsWith ( ".cs" ) )
134
+ {
135
+ continue ;
136
+ }
137
+ if ( group . GetAssetEntry ( guid ) == null )
138
+ {
139
+ var entry = settings . CreateOrMoveEntry ( guid , group ) ;
140
+ foreach ( var itemLabel in labels )
141
+ {
142
+ entry . SetLabel ( itemLabel , true ) ;
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ EditorUtility . SetDirty ( settings ) ;
149
+ AssetDatabase . Refresh ( ) ;
150
+
151
+ EditorApplication . ExecuteMenuItem ( "Window/Asset Management/Addressables/Groups" ) ;
152
+ }
153
+ }
154
+ else
155
+ {
156
+ Close ( ) ;
157
+ }
57
158
} , 50 )
58
- . SetMenuItem ( "Tools" , new string [ ] { "Filter Edit" } , ( itemIndex ) => { EditorApplication . ExecuteMenuItem ( "Tools/Assets Management/Asset Filter" ) ; } ) ;
159
+ . SetMenuItem ( "Tools" , new string [ ] { "Filter Edit" , "Labels Edit" } , ( itemIndex ) =>
160
+ {
161
+ if ( itemIndex == 0 )
162
+ {
163
+ EditorApplication . ExecuteMenuItem ( "Tools/Asset Management/Asset Filter" ) ;
164
+ }
165
+ else if ( itemIndex == 1 )
166
+ {
167
+ EditorApplication . ExecuteMenuItem ( "Window/Asset Management/Addressables/Groups" ) ;
168
+ }
169
+ } ) ;
59
170
}
60
171
61
172
private void OnFormInit ( )
62
173
{
63
174
_editorForm . SetTitle ( "GroupName" , 150 , JsonType . String , null )
64
175
. SetTitle ( "Description" , 100 , JsonType . String , null )
65
176
//.SetTitle("Variant", 100, JsonType.String, null)
66
- . SetTitle ( "Filter" , 100 , JsonType . Int , ( jsonData , width ) => {
67
- int filter = ( int ) jsonData ;
68
- int newFilter = EditorGUILayout . MaskField ( filter , _assetFilter , GUILayout . Width ( 100 ) ) ;
69
- if ( filter != newFilter )
70
- {
71
- ( jsonData as IJsonWrapper ) . SetInt ( newFilter ) ;
72
- }
177
+ . SetTitle ( "Filter" , 100 , JsonType . Array , ( jsonData , width ) => {
178
+ string buttonText = "None" ;
179
+ HashSet < string > selectFilter = new HashSet < string > ( ) ;
180
+ if ( jsonData != null && jsonData . Count > 0 )
181
+ {
182
+ if ( jsonData . Count == 1 )
183
+ {
184
+ buttonText = jsonData [ 0 ] . ToString ( ) ;
185
+ }
186
+ else
187
+ {
188
+ buttonText = "Mixed..." ;
189
+ }
190
+ for ( int i = 0 ; i < jsonData . Count ; i ++ )
191
+ {
192
+ selectFilter . Add ( jsonData [ i ] . ToString ( ) ) ;
193
+ }
194
+ }
195
+ if ( GUILayout . Button ( buttonText , EditorStyles . toolbarDropDown , GUILayout . Width ( width ) ) )
196
+ {
197
+ GenericMenu gm = new GenericMenu ( ) ;
198
+ for ( int i = 0 ; i < _assetFilter . Length ; i ++ )
199
+ {
200
+ string itemFilter = _assetFilter [ i ] ;
201
+ bool filterOn = selectFilter . Contains ( itemFilter ) ;
202
+ gm . AddItem ( new GUIContent ( itemFilter ) , filterOn , ( ) => {
203
+ if ( filterOn )
204
+ {
205
+ jsonData . Remove ( itemFilter ) ;
206
+ selectFilter . Remove ( itemFilter ) ;
207
+ }
208
+ else
209
+ {
210
+ selectFilter . Add ( itemFilter ) ;
211
+ jsonData . Add ( itemFilter ) ;
212
+ }
213
+ } ) ;
214
+ }
215
+ gm . ShowAsContext ( ) ;
216
+ }
73
217
} )
74
218
. SetTitle ( "SearchInFolders" , 120 , JsonType . Array , ( jsonData , width ) => {
75
219
string buttonText = "No folder selected" ;
76
220
if ( jsonData != null && jsonData . Count > 0 )
77
221
{
78
- buttonText = $ "[{ jsonData . Count } ]{ jsonData [ 0 ] . ToString ( ) } ";
79
- if ( buttonText . Length > 13 )
222
+ if ( jsonData . Count == 1 )
80
223
{
81
- buttonText = buttonText . Substring ( 0 , 13 ) ;
224
+ buttonText = jsonData [ 0 ] . ToString ( ) ;
225
+ }
226
+ else
227
+ {
228
+ buttonText = "Mixed..." ;
82
229
}
83
- buttonText += "...|..." ;
84
230
}
85
231
if ( GUILayout . Button ( buttonText , EditorStyles . toolbarDropDown , GUILayout . Width ( width ) ) )
86
232
{
@@ -100,8 +246,79 @@ private void OnFormInit()
100
246
}
101
247
} ) ;
102
248
}
103
- } ) ;
104
- }
249
+ } )
250
+ . SetTitle ( "Labels" , 100 , JsonType . Array , ( jsonData , width ) => {
251
+ string buttonText = "None" ;
252
+ HashSet < string > selectLabels = new HashSet < string > ( ) ;
253
+ if ( jsonData != null && jsonData . Count > 0 )
254
+ {
255
+ if ( jsonData . Count == 1 )
256
+ {
257
+ buttonText = jsonData [ 0 ] . ToString ( ) ;
258
+ }
259
+ else
260
+ {
261
+ buttonText = "Mixed..." ;
262
+ }
263
+ for ( int i = 0 ; i < jsonData . Count ; i ++ )
264
+ {
265
+ selectLabels . Add ( jsonData [ i ] . ToString ( ) ) ;
266
+ }
267
+ }
268
+ if ( GUILayout . Button ( buttonText , EditorStyles . toolbarDropDown , GUILayout . Width ( width ) ) )
269
+ {
270
+ GenericMenu gm = new GenericMenu ( ) ;
271
+ for ( int i = 0 ; i < _labels . Length ; i ++ )
272
+ {
273
+ string itemLabel = _labels [ i ] ;
274
+ bool labelOn = selectLabels . Contains ( itemLabel ) ;
275
+ gm . AddItem ( new GUIContent ( itemLabel ) , labelOn , ( ) => {
276
+ if ( labelOn )
277
+ {
278
+ jsonData . Remove ( itemLabel ) ;
279
+ selectLabels . Remove ( itemLabel ) ;
280
+ }
281
+ else
282
+ {
283
+ selectLabels . Add ( itemLabel ) ;
284
+ jsonData . Add ( itemLabel ) ;
285
+ }
286
+ } ) ;
287
+ }
288
+ gm . ShowAsContext ( ) ;
289
+ }
290
+
291
+ } )
292
+ . SetTitle ( "BuildPath" , 150 , JsonType . String , ( jsonData , width ) => {
293
+ string buildPath = jsonData . ToString ( ) ;
294
+ int buildPathIndex = _profileVariables . IndexOf ( buildPath ) ;
295
+ int newBuildPathIndex = EditorGUILayout . Popup ( buildPathIndex , _profileVariables . ToArray ( ) , GUILayout . Width ( width ) ) ;
296
+ if ( newBuildPathIndex != buildPathIndex )
297
+ {
298
+ buildPath = _profileVariables [ newBuildPathIndex ] ;
299
+ ( jsonData as IJsonWrapper ) . SetString ( buildPath ) ;
300
+ }
301
+ } )
302
+ . SetTitle ( "LoadPath" , 150 , JsonType . String , ( jsonData , width ) => {
303
+ string loadPath = jsonData . ToString ( ) ;
304
+ int loadPathIndex = _profileVariables . IndexOf ( loadPath ) ;
305
+ int newLoadPathIndex = EditorGUILayout . Popup ( loadPathIndex , _profileVariables . ToArray ( ) , GUILayout . Width ( width ) ) ;
306
+ if ( newLoadPathIndex != loadPathIndex )
307
+ {
308
+ loadPath = _profileVariables [ newLoadPathIndex ] ;
309
+ ( jsonData as IJsonWrapper ) . SetString ( loadPath ) ;
310
+ }
311
+ } )
312
+ . SetTitle ( "UpdateRestriction" , 200 , JsonType . Int , ( jsonData , width ) => {
313
+ int updateType = ( int ) jsonData ;
314
+ int newUpdateType = EditorGUILayout . Popup ( updateType , new string [ ] { "Can Change Post Release" , "Cannot Change Post Release" } , GUILayout . Width ( width ) ) ;
315
+ if ( newUpdateType != updateType )
316
+ {
317
+ ( jsonData as IJsonWrapper ) . SetInt ( newUpdateType ) ;
318
+ }
319
+ } )
320
+ ;
321
+ }
105
322
106
323
private void OnDisable ( )
107
324
{
0 commit comments