Skip to content

Commit 6a36c66

Browse files
authored
Merge pull request #129 from brunomikoski/feature/collection-generator-tweaks
Feature/collection generator tweaks
2 parents c42af4d + 051ff2e commit 6a36c66

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ private void DrawBottomMenu()
607607
{
608608
EditorApplication.delayCall += () =>
609609
{
610-
CollectionGenerators.RunGenerator(generatorType);
610+
CollectionGenerators.RunGenerator(generatorType, collection);
611611
};
612612
}
613613
}

Scripts/Editor/Generators/CollectionGenerators.cs

+30-14
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ private static Type[] GetGeneratorTypes()
5252
return InterfaceType.GetAllAssignableClasses();
5353
}
5454

55-
public static Type GetGeneratorTypeForCollection(Type collectionType)
55+
public static Type GetGeneratorTypeForCollection(Type collectionType, bool allowSubclasses = true)
5656
{
5757
Type[] generatorTypes = GetGeneratorTypes();
5858
foreach (Type generatorType in generatorTypes)
5959
{
6060
GetGeneratorTypes(generatorType, out Type generatorCollectionType, out Type generatorTemplateType);
61-
if (generatorCollectionType == collectionType)
61+
if (generatorCollectionType == collectionType || collectionType.IsSubclassOf(generatorCollectionType))
6262
return generatorType;
6363
}
6464

@@ -76,7 +76,14 @@ public static void RunAllGenerators()
7676
AssetDatabase.SaveAssets();
7777
AssetDatabase.Refresh();
7878
}
79+
80+
public static void RunGenerator(Type generatorType, ScriptableObjectCollection targetCollection = null, bool generateStaticAccess = false)
81+
{
82+
IScriptableObjectCollectionGeneratorBase generator = GetGenerator(generatorType);
7983

84+
RunGeneratorInternal(generator, targetCollection, true, generateStaticAccess);
85+
}
86+
8087
public static void RunGenerator(Type generatorType, bool generateStaticAccess = false)
8188
{
8289
RunGeneratorInternal(generatorType, true, generateStaticAccess);
@@ -91,32 +98,35 @@ public static void RunGenerator<GeneratorType>(bool generateStaticAccess = false
9198
public static void RunGenerator(
9299
IScriptableObjectCollectionGeneratorBase generator, bool generateStaticAccess = false)
93100
{
94-
RunGeneratorInternal(generator, true, generateStaticAccess);
101+
RunGeneratorInternal(generator, null, true, generateStaticAccess);
95102
}
96103

97104
private static void RunGeneratorInternal(Type generatorType, bool refresh, bool generateStaticAccess = false)
98105
{
99106
IScriptableObjectCollectionGeneratorBase generator = GetGenerator(generatorType);
100107

101-
RunGeneratorInternal(generator, refresh, generateStaticAccess);
108+
RunGeneratorInternal(generator, null, refresh, generateStaticAccess);
102109
}
103110

104111
private static void RunGeneratorInternal(
105-
IScriptableObjectCollectionGeneratorBase generator, bool refresh, bool generateStaticAccess)
112+
IScriptableObjectCollectionGeneratorBase generator, ScriptableObjectCollection collection, bool refresh, bool generateStaticAccess)
106113
{
107114
Type generatorType = generator.GetType();
108115

109116
GetGeneratorTypes(generatorType, out Type collectionType, out Type itemTemplateType);
110117

111-
// Check that the corresponding collection exists.
112-
CollectionsRegistry.Instance.TryGetCollectionOfType(
113-
collectionType, out ScriptableObjectCollection collection);
114118
if (collection == null)
115119
{
116-
Debug.LogWarning(
117-
$"Tried to generate items for collection '{collectionType.Name}' but no such " +
118-
$"collection existed.");
119-
return;
120+
// Check that the corresponding collection exists.
121+
CollectionsRegistry.Instance.TryGetCollectionOfType(
122+
collectionType, out collection);
123+
if (collection == null)
124+
{
125+
Debug.LogWarning(
126+
$"Tried to generate items for collection '{collectionType.Name}' but no such " +
127+
$"collection existed.");
128+
return;
129+
}
120130
}
121131

122132
// Make an empty list that will hold the generated item templates.
@@ -137,8 +147,6 @@ private static void RunGeneratorInternal(
137147
{
138148
for (int i = collection.Items.Count - 1; i >= 0; i--)
139149
{
140-
bool shouldRemoveItem = false;
141-
142150
// Remove any items for which there isn't a template by the same name.
143151
bool foundItemOfSameName = false;
144152
for (int j = 0; j < templates.Count; j++)
@@ -176,6 +184,14 @@ private static void RunGeneratorInternal(
176184

177185
CopyFieldsFromTemplateToItem(itemTemplate, itemInstance);
178186
}
187+
188+
189+
// Optional Callback to be called when the generation completes
190+
MethodInfo completionCallback = generatorType.GetMethod(
191+
"OnItemsGenerationComplete", BindingFlags.Public | BindingFlags.Instance);
192+
if (completionCallback != null)
193+
completionCallback!.Invoke(generator, new object[] {collection });
194+
179195

180196
if (refresh)
181197
{

Scripts/Runtime/Core/CollectionsRegistry.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public bool TryGetCollectionsOfType<T>(out List<T> inputActionMapCollections) wh
176176
for (int i = 0; i < collections.Count; i++)
177177
{
178178
ScriptableObjectCollection scriptableObjectCollection = collections[i];
179-
if (scriptableObjectCollection.GetType() == typeof(T))
179+
if (scriptableObjectCollection.GetType() == typeof(T) || scriptableObjectCollection.GetType().IsSubclassOf(typeof(T)))
180180
result.Add((T)scriptableObjectCollection);
181181
}
182182

0 commit comments

Comments
 (0)