TriLib Exporter
Search Results for

    Basic Usage

    To export a model with TriLib Exporter, use the AssetWriter class. The AssetWriter class contains two methods:

    • ExportToFile
    • ExportToStream

    Before explaining these methods, here are a few considerations when exporting a GameObject with TriLib Exporter:

    • In order to export meshes, all GameObject meshes must be readable. You should mark them as readable in the Unity Editor, or pass false to Mesh.UploadMeshData if you are generating or modifying your meshes procedurally.
    • In order to export materials, all GameObject materials must be readable. You should mark them as readable in the Unity Editor, or pass false to the second parameter of Texture.Apply if you are generating or modifying your meshes procedurally.

    ExportToFile

    Use this method when you want to export your model to a file in your local file system. All desktop platforms (Windows, macOS, and Linux) can perform this action.

    This method requires the following parameters:

    • sourceGameObject: The root GameObject to be exported. Any other GameObject inside it will be exported as well.
    • path: The path to save the exported model.
    • options: A series of options that configure the exporting process. For more information, refer to AssetWriterOptions.
    • onComplete: A callback executed when the model finishes exporting.
    • onError: A callback executed when any exporting error occurs.

    The type of file to be exported is determined by the extension in the path parameter. For example, providing path_to_my_model.fbx will make TriLib Exporter export the model as an FBX file. Only FBX files are supported at the moment.

    Here is an example of how to use ExportToFile:

    using UnityEngine;
    using TriLibExporter;
    
    // Attach this script to the GameObject you want to export
    public class ExportToFile : MonoBehaviour
    {
        // Exports the current GameObject to an FBX file.
        private void Start()
        {
            // Define the export file path in the temporary cache directory
            var filename = $"{Application.temporaryCachePath}/exported.fbx";
    
            // Create a new instance of AssetWriterOptions to configure the export (optional)
            var options = ScriptableObject.CreateInstance<AssetWriterOptions>();
    
            // Use TriLib's AssetWriter to export the object with the specified options
            AssetWriter.ExportToFile(gameObject, filename, options, OnExportComplete);
        }
    
        // Callback invoked when the FBX export process is complete. 
        private void OnExportComplete(AssetWriterContext context)
        {
            // Reveals the exported file
            ProcessUtil.RevealFile(context.FilePath);
        }
    }
    

    ExportToStream

    Use this method to export your model to a custom stream. This is especially useful on platforms that do not allow direct file access or that lack a file system altogether, such as WebGL. You can then use the resulting stream to write data on these platforms.

    This method requires the following parameters:

    • sourceGameObject: The root GameObject to be exported. Any other GameObject inside it will be exported as well.
    • stream: This is where TriLib Exporter will write the exported model data.
    • fileExtension: Unlike ExportToFile, ExportToStream does not necessarily write to the local file system, so it does not require specifying a path. However, you must provide the model’s file extension so TriLib Exporter can detect the file type. Only FBX files are supported at the moment.
    • options: A series of options that configure the exporting process. For more information, refer to AssetWriterOptions.
    • onComplete: A callback executed when the model finishes exporting.
    • onError: A callback executed when any exporting error occurs.
    • customPath: The ExportToStream method may generate additional files (e.g., PNG textures or other resources). You decide how to save these external files. The customPath parameter is used with the ExternalDataMapper class to identify the resource file being exported.
    In this article
    Back to top Generated by DocFX