@@ -52,13 +52,13 @@ private static Type[] GetGeneratorTypes()
52
52
return InterfaceType . GetAllAssignableClasses ( ) ;
53
53
}
54
54
55
- public static Type GetGeneratorTypeForCollection ( Type collectionType )
55
+ public static Type GetGeneratorTypeForCollection ( Type collectionType , bool allowSubclasses = true )
56
56
{
57
57
Type [ ] generatorTypes = GetGeneratorTypes ( ) ;
58
58
foreach ( Type generatorType in generatorTypes )
59
59
{
60
60
GetGeneratorTypes ( generatorType , out Type generatorCollectionType , out Type generatorTemplateType ) ;
61
- if ( generatorCollectionType == collectionType )
61
+ if ( generatorCollectionType == collectionType || collectionType . IsSubclassOf ( generatorCollectionType ) )
62
62
return generatorType ;
63
63
}
64
64
@@ -76,7 +76,14 @@ public static void RunAllGenerators()
76
76
AssetDatabase . SaveAssets ( ) ;
77
77
AssetDatabase . Refresh ( ) ;
78
78
}
79
+
80
+ public static void RunGenerator ( Type generatorType , ScriptableObjectCollection targetCollection = null , bool generateStaticAccess = false )
81
+ {
82
+ IScriptableObjectCollectionGeneratorBase generator = GetGenerator ( generatorType ) ;
79
83
84
+ RunGeneratorInternal ( generator , targetCollection , true , generateStaticAccess ) ;
85
+ }
86
+
80
87
public static void RunGenerator ( Type generatorType , bool generateStaticAccess = false )
81
88
{
82
89
RunGeneratorInternal ( generatorType , true , generateStaticAccess ) ;
@@ -91,32 +98,35 @@ public static void RunGenerator<GeneratorType>(bool generateStaticAccess = false
91
98
public static void RunGenerator (
92
99
IScriptableObjectCollectionGeneratorBase generator , bool generateStaticAccess = false )
93
100
{
94
- RunGeneratorInternal ( generator , true , generateStaticAccess ) ;
101
+ RunGeneratorInternal ( generator , null , true , generateStaticAccess ) ;
95
102
}
96
103
97
104
private static void RunGeneratorInternal ( Type generatorType , bool refresh , bool generateStaticAccess = false )
98
105
{
99
106
IScriptableObjectCollectionGeneratorBase generator = GetGenerator ( generatorType ) ;
100
107
101
- RunGeneratorInternal ( generator , refresh , generateStaticAccess ) ;
108
+ RunGeneratorInternal ( generator , null , refresh , generateStaticAccess ) ;
102
109
}
103
110
104
111
private static void RunGeneratorInternal (
105
- IScriptableObjectCollectionGeneratorBase generator , bool refresh , bool generateStaticAccess )
112
+ IScriptableObjectCollectionGeneratorBase generator , ScriptableObjectCollection collection , bool refresh , bool generateStaticAccess )
106
113
{
107
114
Type generatorType = generator . GetType ( ) ;
108
115
109
116
GetGeneratorTypes ( generatorType , out Type collectionType , out Type itemTemplateType ) ;
110
117
111
- // Check that the corresponding collection exists.
112
- CollectionsRegistry . Instance . TryGetCollectionOfType (
113
- collectionType , out ScriptableObjectCollection collection ) ;
114
118
if ( collection == null )
115
119
{
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
+ }
120
130
}
121
131
122
132
// Make an empty list that will hold the generated item templates.
@@ -137,8 +147,6 @@ private static void RunGeneratorInternal(
137
147
{
138
148
for ( int i = collection . Items . Count - 1 ; i >= 0 ; i -- )
139
149
{
140
- bool shouldRemoveItem = false ;
141
-
142
150
// Remove any items for which there isn't a template by the same name.
143
151
bool foundItemOfSameName = false ;
144
152
for ( int j = 0 ; j < templates . Count ; j ++ )
@@ -176,6 +184,14 @@ private static void RunGeneratorInternal(
176
184
177
185
CopyFieldsFromTemplateToItem ( itemTemplate , itemInstance ) ;
178
186
}
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
+
179
195
180
196
if ( refresh )
181
197
{
0 commit comments