Files
AR-Menu/Library/PackageCache/com.needle.engine-exporter@8c046140a1d9/Gltf/Runtime/GltfExportHandlerFactory.cs
2025-11-30 08:35:03 +02:00

54 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
namespace Needle.Engine.Gltf
{
/// <summary>
/// Use to create implementation for gltf exporter
/// </summary>
public static class GltfExportHandlerFactory
{
public static GltfExporterType DefaultExporter = GltfExporterType.UnityGLTF;
private static Dictionary<GltfExporterType, Type> knownTypes;
private static void RegisterExportHandlerType(GltfExporterType type, Type t)
{
if (!knownTypes.ContainsKey(type))
knownTypes.Add(type, t);
else knownTypes[type] = t;
}
public static IGltfExportHandler CreateHandler()
{
return CreateHandler(DefaultExporter);
}
public static IGltfExportHandler CreateHandler(GltfExporterType exporterType)
{
#if UNITY_EDITOR
if (knownTypes == null)
{
knownTypes = new Dictionary<GltfExporterType, Type>();
foreach (var type in TypeCache.GetTypesWithAttribute<GltfExportHandlerAttribute>())
{
var attribute = type.GetCustomAttribute<GltfExportHandlerAttribute>();
RegisterExportHandlerType(attribute.Type, type);
}
}
#endif
if (knownTypes != null)
{
if (knownTypes.TryGetValue(exporterType, out var t))
{
var instance = Activator.CreateInstance(t);
return instance as IGltfExportHandler;
}
}
return null;
}
}
}