TriLib – Basic usage/Troubleshooting

Basic usage

Synchronous model loading:

using (var assetLoader = new AssetLoader()) {
    var assetLoaderOptions = AssetLoaderOptions.CreateInstance();   //Creates the AssetLoaderOptions instance.
                                                                    //AssetLoaderOptions let you specify options to load your model.
                                                                    //(Optional) You can skip this object creation and it's parameter or pass null.
    
    //You can modify assetLoaderOptions before passing it to LoadFromFile method. You can check the AssetLoaderOptions API reference at:
    //https://ricardoreis.net/trilib/manual/html/class_tri_lib_1_1_asset_loader_options.html
    
    var wrapperGameObject = gameObject;                             //Sets the game object where your model will be loaded into.
                                                                    //(Optional) You can skip this object creation and it's parameter or pass null.

    var myGameObject = assetLoader.LoadFromFile("PATH TO MY FILE.FBX", assetLoaderOptions, wrapperGameObject); //Loads the model synchronously and stores the reference in myGameObject.
}

Asynchronous model loading:

using (var assetLoaderAsync = new AssetLoaderAsync()) {
    var assetLoaderOptions = AssetLoaderOptions.CreateInstance();   //Creates the AssetLoaderOptions instance.
                                                                    //AssetLoaderOptions let you specify options to load your model.
                                                                    //(Optional) You can skip this object creation and it's parameter or pass null.
    
    //You can modify assetLoaderOptions before passing it to LoadFromFile method. You can check the AssetLoaderOptions API reference at:
    //https://ricardoreis.net/trilib/manual/html/class_tri_lib_1_1_asset_loader_options.html
    
    var wrapperGameObject = gameObject;                             //Sets the game object where your model will be loaded into.
                                                                    //(Optional) You can skip this object creation and it's parameter or pass null.

    var thread = assetLoaderAsync.LoadFromFile("PATH TO MY FILE.FBX", assetLoaderOptions, wrapperGameObject, delegate(GameObject myGameObject) {
        //Here you can get the reference to the loaded model using myGameObject.
    }); //Loads the model asynchronously and returns the reference to the created Task/Thread.
}

*Note: Async loader is meant to be used per loading request, so you should create a new AssetLoaderAssync for each model you want to load, or just re-use an AssetLoaderAsync instance when it has finished processing.

Retrieving unit scale factor from FBX models:

using (var assetLoader = new AssetLoader()) {
    var unitScaleFactor = 100f; //Sets scale factor default value.
    assetLoader.OnMetadataProcessed += delegate (AssimpMetadataType metadataType, uint metadataIndex, string metadataKey, object metadataValue) {
        //Checks if the metadata key is "UnitScaleFactor", which is the scale factor metadata used in FBX files.
        if (metadataKey == "UnitScaleFactor")
        {
            unitScaleFactor = Convert.ToSingle(metadataValue); //Sets the scale factor value.
        }
    }; //Sets a callback to process model metadata.
    var myGameObject = assetLoader.LoadFromFile("PATH TO MY FILE.FBX"); //Loads the model synchronously and stores the reference in myGameObject.
    myGameObject.transform.localScale = Vector3.one * (unitScaleFactor / 100f); //Uses the scale factor on the loaded model.
}

Troubleshooting

– I can’t load models from ZIP files.

In order to process Zip files contents, you  must check “Enable Zip loading” menu item on TriLib project settings.

– My platform project is complaining about “libassimp” is missing.

Sometimes, the native plugins may not be included in your platform project (the generated XCode project, for instance). In these cases, you can try using the “Configure Native Plugins” button on TriLib project settings, which will reconfigure the native plugins accordingly and include them on the right platforms.

– Where are TriLib project settings?

The project settings can be found on “Edit-Project Settings-TriLib” Unity menu.

– Where can I find the WebGL template used on TriLib demo?

You can download the WebGL template here:
https://ricardoreis.net/trilib/WebGLTemplate.zip