Skip to content

Commit 9ec9cf9

Browse files
authored
Merge pull request #52 from brunomikoski/feature/improvements
fix: general tweaks and improvements
2 parents 88ebbdb + 7a3e0b3 commit 9ec9cf9

14 files changed

Lines changed: 78 additions & 184 deletions

CHANGELOG.MD

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.4.0]
8+
### Added
9+
- Added quick access to the .Values from the collection, to be used without code generation, you can now use `CustomCollection.Values` to gett all your items
10+
- Added a `GetValues()` to the generated static file, you can quickly get a filtered list of items that you need
11+
12+
### Changed
13+
- Changed Create Collection Wizzard to behave like a modal window that can loose focus.
14+
- Removed the TryGet static access generation, was not been used and was causing some problems
15+
716

817
## [1.3.2]
918
### Added
@@ -176,8 +185,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
176185
- First initial working version
177186

178187
### [Unreleased]
179-
### Changed
180-
- Changed Create Collection Wizzard to behave like a modal window that can loose focus.
188+
181189

182190

183191
[1.3.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.3.2

Scripts/Editor/CollectableDropdown.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public CollectableDropdown(AdvancedDropdownState state, ScriptableObjectCollecti
2121

2222
protected override AdvancedDropdownItem BuildRoot()
2323
{
24-
Type collectableType = collection.GetCollectionType();
24+
Type collectableType = collection.GetCollectableType();
2525
AdvancedDropdownItem root = new AdvancedDropdownItem(collectableType.Name);
2626

2727
root.AddChild(new AdvancedDropdownItem("None"));
@@ -61,7 +61,7 @@ protected override void ItemSelected(AdvancedDropdownItem item)
6161

6262
if (item.name.Equals(CREATE_NEW_TEXT, StringComparison.OrdinalIgnoreCase))
6363
{
64-
CollectableScriptableObject collectable = collection.AddNew(collection.GetCollectionType());
64+
CollectableScriptableObject collectable = collection.AddNew(collection.GetCollectableType());
6565
callback?.Invoke(collectable);
6666
Selection.objects = new Object[] {collection};
6767
ScriptableObjectCollectionCustomEditor.LastAddedEnum = collectable;

Scripts/Editor/CollectionsAssetsPostProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static Dictionary<Type, ScriptableObjectCollection> typeToCollections
2727
if(collection == null)
2828
continue;
2929

30-
cachedTypeToCollections.Add(collection.GetCollectionType(), collection);
30+
cachedTypeToCollections.Add(collection.GetCollectableType(), collection);
3131
}
3232
}
3333

Scripts/Editor/CreateCollectionWizzard.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ static void OnAfterScriptsReloading()
347347

348348
Selection.objects = new[] {collectionScriptableObject.targetObject};
349349
EditorGUIUtility.PingObject(collectionScriptableObject.targetObject);
350-
GetWindowInstance().Close();
350+
351+
CreateCollectionWizzard openWindowInstance = GetWindow<CreateCollectionWizzard>();
352+
openWindowInstance.Close();
351353
}
352354
}
353355
}

Scripts/Editor/Extensions.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ private void DrawBottomMenu()
211211

212212
private void AddNewItem()
213213
{
214-
List<Type> collectableSubclasses = TypeUtility.GetAllSubclasses(collection.GetCollectionType(), true);
214+
List<Type> collectableSubclasses = TypeUtility.GetAllSubclasses(collection.GetCollectableType(), true);
215215

216-
collectableSubclasses.Add(collection.GetCollectionType());
216+
collectableSubclasses.Add(collection.GetCollectableType());
217217
GenericMenu optionsMenu = new GenericMenu();
218218

219219
for (int i = 0; i < collectableSubclasses.Count; i++)
@@ -503,21 +503,6 @@ private void DrawSettings()
503503
}
504504
}
505505

506-
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
507-
{
508-
GeneratedStaticFileType staticCodeGeneratorType =
509-
(GeneratedStaticFileType) EditorGUILayout.EnumPopup("Static File Generator Type",
510-
ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(
511-
collection));
512-
513-
if (changeCheck.changed)
514-
{
515-
ScriptableObjectCollectionSettings.Instance.SetStaticFileGeneratorTypeForCollection(
516-
collection,
517-
staticCodeGeneratorType);
518-
}
519-
}
520-
521506
bool overwriteStaticFileLocation = false;
522507
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
523508
{

Scripts/Editor/Utils/CodeGenerationUtility.cs

Lines changed: 21 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Scripts/Runtime/CollectionsRegistry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public bool TryGetCollectionFromCollectableType(Type targetType,
121121
for (int i = 0; i < collections.Count; i++)
122122
{
123123
ScriptableObjectCollection collection = collections[i];
124-
if(collection.GetCollectionType() == targetType
125-
|| targetType.BaseType == collection.GetCollectionType())
124+
if(collection.GetCollectableType() == targetType
125+
|| targetType.BaseType == collection.GetCollectableType())
126126
{
127127
scriptableObjectCollection = collection;
128128
return true;

Scripts/Runtime/GeneratedStaticFileType.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

Scripts/Runtime/GeneratedStaticFileType.cs.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)