TriLib – Tutorial : Importing a model from within your game runtime

Let´s learn how to import an external model from our game code:

First, import TriLib package from the Asset Store.

For this simple model loading purpose, you can check only the “TriLib/TriLib” folder in the import dialog.

The first time you import TriLib to your project, Unity will ask you to restart the editor. If you choose “Ask to save changes and restart”, you will be prompted to save your changes before the editor restarts.

When Unity restart:

Create a new GameObject and add a new CSharp script to it.
We can name it “MyLoader” for the demonstration.

Add the following code to the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TriLib;
using System.IO;

public class MyLoader : MonoBehaviour {
    protected void Start() {
        using (var assetLoader = new AssetLoader()) { //Initializes our Asset Loader.
            var assetLoaderOptions = ScriptableObject.CreateInstance<AssetLoaderOptions>(); //Creates an Asset Loader Options object.
            assetLoaderOptions.AutoPlayAnimations = true; //Indicates that our model will automatically play its first animation, if any.
            var filename = Path.Combine(Path.GetFullPath("."), "turtle1.b3d"); //Combines our current directory with our model filename "turtle1.b3d" and generates the full model path.
            assetLoader.LoadFromFile(filename, assetLoaderOptions); //Loads our model.
        }
    }
}

You have to replace the “turtle1.b3d” string with your model filename. The model will be loaded from within your application folder , so, remember to copy it to your final executable’s path when building it.

Ready!
Your model is already being loaded from an external source.