docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Batch Export Scene Roots to glTF Files

    A common Editor scripting use case is exporting GameObjects to individual .glb files.

    The example below adds a Tools > glTFast Examples > Batch Export menu entry.

    [MenuItem("Tools/glTFast Examples/Batch Export")]
    static async Task BatchExportAllObjects()
    {
        var currentScene = SceneManager.GetActiveScene();
        var rootObjects = currentScene.GetRootGameObjects();
    
        var exportSettings = new ExportSettings
        {
            Format = GltfFormat.Binary
        };
    
        try
        {
            for (var index = 0; index < rootObjects.Length; index++)
            {
                var rootObject = rootObjects[index];
    
                EditorUtility.DisplayProgressBar(
                    "glTF Batch Export", rootObject.name, index / (float)rootObjects.Length);
    
                var export = new GameObjectExport(exportSettings);
                export.AddScene(new[] { rootObject }, rootObject.name);
    
                var success = await export.SaveToFileAndDisposeAsync(
                    $"Assets/{rootObject.name}.glb",
                    // Edit Mode does not pump the main-thread SynchronizationContext;
                    // awaited I/O continuations could hang. Force the synchronous path.
                    forceSync: true
                    );
                if (!success)
                {
                    Debug.LogError($"Exporting {rootObject.name} failed.");
                }
            }
        }
        finally
        {
            EditorUtility.ClearProgressBar();
            AssetDatabase.Refresh();
        }
    }
    

    A few details worth highlighting:

    • forceSync: true is required because the menu item runs in Edit Mode — see Editor Scripting: Enforce Synchronous I/O for the rationale.

    Trademarks

    Unity® is a registered trademark of Unity Technologies.

    Khronos® is a registered trademark and glTF™ is a trademark of The Khronos Group Inc.

    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)