Extend UnityGLTF with custom Import/Export plugins
mainUnityGLTF provides a plugin system to modify node structures, extension data, or materials during the import/export process. Plugins are ScriptableObjects enabled via Project Settings > UnityGLTF.
To create a plugin:
- Create a class inheriting from
GLTFImportPluginorGLTFExportPlugin(this holds the settings). - Create a class inheriting from
GLTFImportPluginContextorGLTFExportPluginContext(this contains the actual callbacks). - Implement
CreateInstancein the plugin class to return the context instance. - Override the desired callbacks in the context class.
If your plugin handles custom extension data, implement GLTF.Schema.IExtension for serialization.
// Example for custom export plugin
public class MyExportPlugin : GLTFExportPlugin
{
public override string DisplayName { get => "My Custom Plugin"; }
public override bool EnabledByDefault => true;
public override bool AlwaysEnabled => false;
public override GLTFExportPluginContext CreateInstance(ExportContext context)
{
return new MyExportPluginContext();
}
}
public class MyExportPluginContext: GLTFExportPluginContext
{
public override bool ShouldNodeExport(GLTFSceneExporter exporter, GLTFRoot gltfRoot, Transform transform)
{
return !transform.CompareTag("ignore");
}
}
// Example for custom import plugin
public class MyImportPlugin: GLTFImportPlugin
{
public override string DisplayName => "My Import Plugin";
public override string Description => "";
public override GLTFImportPluginContext CreateInstance(GLTFImportContext context)
{
return new MyImportPluginContext();
}
}
public class MyImportPluginContext: GLTFImportPluginContext
{
public override void OnAfterImportScene(GLTFScene scene, int sceneIndex, GameObject sceneObject)
{
// Set all to static
var objs = sceneObject.GetComponentsInChildren<Transform>();
foreach (var obj in objs)
obj.gameObject.isStatic = true;
}
}