Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/collection generator tweaks #129

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Scripts/Editor/CustomEditors/CollectionCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ private void DrawBottomMenu()
{
EditorApplication.delayCall += () =>
{
CollectionGenerators.RunGenerator(generatorType);
CollectionGenerators.RunGenerator(generatorType, collection);
};
}
}
Expand Down
44 changes: 30 additions & 14 deletions Scripts/Editor/Generators/CollectionGenerators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ private static Type[] GetGeneratorTypes()
return InterfaceType.GetAllAssignableClasses();
}

public static Type GetGeneratorTypeForCollection(Type collectionType)
public static Type GetGeneratorTypeForCollection(Type collectionType, bool allowSubclasses = true)
{
Type[] generatorTypes = GetGeneratorTypes();
foreach (Type generatorType in generatorTypes)
{
GetGeneratorTypes(generatorType, out Type generatorCollectionType, out Type generatorTemplateType);
if (generatorCollectionType == collectionType)
if (generatorCollectionType == collectionType || collectionType.IsSubclassOf(generatorCollectionType))
return generatorType;
}

Expand All @@ -76,7 +76,14 @@ public static void RunAllGenerators()
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}

public static void RunGenerator(Type generatorType, ScriptableObjectCollection targetCollection = null, bool generateStaticAccess = false)
{
IScriptableObjectCollectionGeneratorBase generator = GetGenerator(generatorType);

RunGeneratorInternal(generator, targetCollection, true, generateStaticAccess);
}

public static void RunGenerator(Type generatorType, bool generateStaticAccess = false)
{
RunGeneratorInternal(generatorType, true, generateStaticAccess);
Expand All @@ -91,32 +98,35 @@ public static void RunGenerator<GeneratorType>(bool generateStaticAccess = false
public static void RunGenerator(
IScriptableObjectCollectionGeneratorBase generator, bool generateStaticAccess = false)
{
RunGeneratorInternal(generator, true, generateStaticAccess);
RunGeneratorInternal(generator, null, true, generateStaticAccess);
}

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

RunGeneratorInternal(generator, refresh, generateStaticAccess);
RunGeneratorInternal(generator, null, refresh, generateStaticAccess);
}

private static void RunGeneratorInternal(
IScriptableObjectCollectionGeneratorBase generator, bool refresh, bool generateStaticAccess)
IScriptableObjectCollectionGeneratorBase generator, ScriptableObjectCollection collection, bool refresh, bool generateStaticAccess)
{
Type generatorType = generator.GetType();

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

// Check that the corresponding collection exists.
CollectionsRegistry.Instance.TryGetCollectionOfType(
collectionType, out ScriptableObjectCollection collection);
if (collection == null)
{
Debug.LogWarning(
$"Tried to generate items for collection '{collectionType.Name}' but no such " +
$"collection existed.");
return;
// Check that the corresponding collection exists.
CollectionsRegistry.Instance.TryGetCollectionOfType(
collectionType, out collection);
if (collection == null)
{
Debug.LogWarning(
$"Tried to generate items for collection '{collectionType.Name}' but no such " +
$"collection existed.");
return;
}
}

// Make an empty list that will hold the generated item templates.
Expand All @@ -137,8 +147,6 @@ private static void RunGeneratorInternal(
{
for (int i = collection.Items.Count - 1; i >= 0; i--)
{
bool shouldRemoveItem = false;

// Remove any items for which there isn't a template by the same name.
bool foundItemOfSameName = false;
for (int j = 0; j < templates.Count; j++)
Expand Down Expand Up @@ -176,6 +184,14 @@ private static void RunGeneratorInternal(

CopyFieldsFromTemplateToItem(itemTemplate, itemInstance);
}


// Optional Callback to be called when the generation completes
MethodInfo completionCallback = generatorType.GetMethod(
"OnItemsGenerationComplete", BindingFlags.Public | BindingFlags.Instance);
if (completionCallback != null)
completionCallback!.Invoke(generator, new object[] {collection });


if (refresh)
{
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Core/CollectionsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public bool TryGetCollectionsOfType<T>(out List<T> inputActionMapCollections) wh
for (int i = 0; i < collections.Count; i++)
{
ScriptableObjectCollection scriptableObjectCollection = collections[i];
if (scriptableObjectCollection.GetType() == typeof(T))
if (scriptableObjectCollection.GetType() == typeof(T) || scriptableObjectCollection.GetType().IsSubclassOf(typeof(T)))
result.Add((T)scriptableObjectCollection);
}

Expand Down