@@ -176,12 +176,12 @@ public static void AppendFooter(StreamWriter writer, ref int identation, string
176176
177177 public static void GenerateStaticCollectionScript ( ScriptableObjectCollection collection )
178178 {
179- string fileName = $ "{ collection . GetCollectionType ( ) . Name } Static";
179+ string fileName = $ "{ collection . GetCollectableType ( ) . Name } Static";
180180 bool isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings . Instance . IsGeneratingCustomStaticFile ( collection ) ;
181181 if ( isGeneratingCustomStaticFile )
182182 fileName = ScriptableObjectCollectionSettings . Instance . GetGeneratedStaticFileName ( collection ) ;
183183
184- string nameSpace = collection . GetCollectionType ( ) . Namespace ;
184+ string nameSpace = collection . GetCollectableType ( ) . Namespace ;
185185 string finalFolder = ScriptableObjectCollectionSettings . Instance . GetStaticFileFolderForCollection ( collection ) ;
186186
187187 if ( string . IsNullOrEmpty ( finalFolder ) )
@@ -200,24 +200,22 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
200200 List < string > directives = new List < string > ( ) ;
201201 directives . Add ( typeof ( CollectionsRegistry ) . Namespace ) ;
202202 directives . Add ( collection . GetType ( ) . Namespace ) ;
203+ directives . Add ( typeof ( List < > ) . Namespace ) ;
204+ directives . Add ( "System.Linq" ) ;
203205 directives . AddRange ( GetCollectionDirectives ( collection ) ) ;
204206
205207 if ( ! isGeneratingCustomStaticFile )
206208 {
207209 AppendHeader ( writer , ref indentation , nameSpace , "" ,
208- collection . GetCollectionType ( ) . Name , true , false , directives . Distinct ( ) . ToArray ( ) ) ;
210+ collection . GetCollectableType ( ) . Name , true , false , directives . Distinct ( ) . ToArray ( ) ) ;
209211 }
210212 else
211213 {
212214 AppendHeader ( writer , ref indentation , nameSpace , "" ,
213215 fileName , false , false , directives . Distinct ( ) . ToArray ( ) ) ;
214216 }
215217
216- GeneratedStaticFileType staticFileTypeForCollection = ScriptableObjectCollectionSettings . Instance . GetStaticFileTypeForCollection ( collection ) ;
217- if ( staticFileTypeForCollection == GeneratedStaticFileType . DirectAccess )
218- WriteDirectAccessCollectionStatic ( collection , writer , ref indentation ) ;
219- else
220- WriteTryGetAccessCollectionStatic ( collection , writer , ref indentation ) ;
218+ WriteDirectAccessCollectionStatic ( collection , writer , ref indentation ) ;
221219
222220 indentation -- ;
223221 AppendFooter ( writer , ref indentation , nameSpace ) ;
@@ -235,101 +233,6 @@ private static string[] GetCollectionDirectives(ScriptableObjectCollection colle
235233
236234 return directives . ToArray ( ) ;
237235 }
238-
239- private static void WriteTryGetAccessCollectionStatic ( ScriptableObjectCollection collection , StreamWriter writer ,
240- ref int indentation )
241- {
242- string cachedValuesName = $ "values";
243- string valuesName = $ "Values";
244- string tryGetValuesName = $ "TryGet{ valuesName } ";
245-
246- AppendLine ( writer , indentation , $ "private static { collection . GetType ( ) . Name } { cachedValuesName } ;") ;
247-
248- for ( int i = 0 ; i < collection . Items . Count ; i ++ )
249- {
250- CollectableScriptableObject collectionItem = collection . Items [ i ] ;
251- AppendLine ( writer , indentation ,
252- $ "private static { collectionItem . GetType ( ) . Name } { collectionItem . name . Sanitize ( ) . FirstToLower ( ) } ;") ;
253- }
254-
255- AppendLine ( writer , indentation ) ;
256-
257- AppendLine ( writer , indentation , $ "public static bool { tryGetValuesName } (out { collection . GetType ( ) . Name } result)") ;
258- AppendLine ( writer , indentation , "{" ) ;
259- indentation ++ ;
260- AppendLine ( writer , indentation , $ "if ({ cachedValuesName } != null)") ;
261- AppendLine ( writer , indentation , "{" ) ;
262- indentation ++ ;
263- AppendLine ( writer , indentation , $ "result = { cachedValuesName } ;") ;
264- AppendLine ( writer , indentation , "return true;" ) ;
265- indentation -- ;
266- AppendLine ( writer , indentation , "}" ) ;
267- AppendLine ( writer , indentation ) ;
268- AppendLine ( writer , indentation , $ "if (!CollectionsRegistry.Instance.TryGetCollectionByGUID(\" { collection . GUID } \" , out ScriptableObjectCollection resultCollection))") ;
269- AppendLine ( writer , indentation , "{" ) ;
270- indentation ++ ;
271- AppendLine ( writer , indentation , $ "result = { cachedValuesName } ;") ;
272- AppendLine ( writer , indentation , "return false;" ) ;
273- indentation -- ;
274- AppendLine ( writer , indentation , "}" ) ;
275- AppendLine ( writer , indentation ) ;
276- AppendLine ( writer , indentation , $ "{ cachedValuesName } = ({ collection . GetType ( ) . Name } ) resultCollection;") ;
277- AppendLine ( writer , indentation , $ "result = { cachedValuesName } ;") ;
278- AppendLine ( writer , indentation , $ "return true;") ;
279- indentation -- ;
280- AppendLine ( writer , indentation , "}" ) ;
281-
282- AppendLine ( writer , indentation ) ;
283-
284- for ( int i = 0 ; i < collection . Items . Count ; i ++ )
285- {
286- CollectableScriptableObject collectionItem = collection . Items [ i ] ;
287- string pascalizedItemName = collectionItem . name . Sanitize ( ) . FirstToUpper ( ) ;
288- string camelizedItemName = collectionItem . name . Sanitize ( ) . FirstToLower ( ) ;
289- Type itemType = collectionItem . GetType ( ) ;
290-
291-
292- AppendLine ( writer , indentation , $ "public static bool TryGet{ pascalizedItemName } (out { itemType . Name } result)") ;
293- AppendLine ( writer , indentation , "{" ) ;
294- indentation ++ ;
295- AppendLine ( writer , indentation , $ "if ({ camelizedItemName } != null)") ;
296- AppendLine ( writer , indentation , "{" ) ;
297- indentation ++ ;
298- AppendLine ( writer , indentation , $ "result = { camelizedItemName } ;") ;
299- AppendLine ( writer , indentation , "return true;" ) ;
300- indentation -- ;
301- AppendLine ( writer , indentation , "}" ) ;
302- AppendLine ( writer , indentation ) ;
303-
304- AppendLine ( writer , indentation , $ "if (!{ tryGetValuesName } (out { collection . GetType ( ) . Name } collectionResult))") ;
305- AppendLine ( writer , indentation , "{" ) ;
306- indentation ++ ;
307- AppendLine ( writer , indentation , "result = null;" ) ;
308- AppendLine ( writer , indentation , "return false;" ) ;
309- indentation -- ;
310- AppendLine ( writer , indentation , "}" ) ;
311- AppendLine ( writer , indentation ) ;
312-
313-
314- AppendLine ( writer , indentation , $ "if (!collectionResult.TryGetCollectableByGUID(\" { collectionItem . GUID } \" , out CollectableScriptableObject resultCollectable))") ;
315- AppendLine ( writer , indentation , "{" ) ;
316- indentation ++ ;
317- AppendLine ( writer , indentation , "result = null;" ) ;
318- AppendLine ( writer , indentation , "return false;" ) ;
319- indentation -- ;
320- AppendLine ( writer , indentation ) ;
321- AppendLine ( writer , indentation , "}" ) ;
322-
323-
324- AppendLine ( writer , indentation , $ "{ camelizedItemName } = ({ itemType . Name } ) resultCollectable;") ;
325- AppendLine ( writer , indentation , $ "result = { camelizedItemName } ;") ;
326- AppendLine ( writer , indentation , "return true;" ) ;
327- indentation -- ;
328- AppendLine ( writer , indentation , "}" ) ;
329- AppendLine ( writer , indentation ) ;
330-
331- }
332- }
333236
334237 private static void WriteDirectAccessCollectionStatic ( ScriptableObjectCollection collection , StreamWriter writer ,
335238 ref int indentation )
@@ -339,6 +242,8 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
339242 string cachedValuesName = "values" ;
340243 AppendLine ( writer , indentation , $ "private static { collection . GetType ( ) . Name } { cachedValuesName } ;") ;
341244
245+ AppendLine ( writer , indentation ) ;
246+
342247 for ( int i = 0 ; i < collection . Items . Count ; i ++ )
343248 {
344249 CollectableScriptableObject collectionItem = collection . Items [ i ] ;
@@ -350,16 +255,9 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
350255
351256 string valuesName = $ "Values";
352257
353- if ( ! isGeneratingCustomStaticFile )
354- {
355- AppendLine ( writer , indentation ,
258+ AppendLine ( writer , indentation ,
356259 $ "public static { collection . GetType ( ) . Name } { valuesName } ") ;
357- }
358- else
359- {
360- AppendLine ( writer , indentation ,
361- $ "public static { collection . GetType ( ) . Name } { valuesName } ") ;
362- }
260+
363261 AppendLine ( writer , indentation , "{" ) ;
364262 indentation ++ ;
365263 AppendLine ( writer , indentation , "get" ) ;
@@ -378,7 +276,7 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
378276 AppendLine ( writer , indentation ) ;
379277
380278 AppendLine ( writer , indentation ) ;
381-
279+
382280 for ( int i = 0 ; i < collection . Items . Count ; i ++ )
383281 {
384282 CollectableScriptableObject collectionItem = collection . Items [ i ] ;
@@ -405,6 +303,16 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
405303 AppendLine ( writer , indentation , "}" ) ;
406304 AppendLine ( writer , indentation ) ;
407305 }
306+
307+
308+ AppendLine ( writer , indentation , $ "public static IEnumerable<T> GetValues<T>() where T : { collection . GetCollectableType ( ) . Name } ") ;
309+ AppendLine ( writer , indentation , "{" ) ;
310+ indentation ++ ;
311+ AppendLine ( writer , indentation , $ "return Values.Where(item => item is T).Cast<T>();") ;
312+ indentation -- ;
313+ AppendLine ( writer , indentation , "}" ) ;
314+
315+ AppendLine ( writer , indentation ) ;
408316 }
409317 }
410318}
0 commit comments