Unity运行时程序动态加载外部.fbx.obj模型文件
Unity运行时程序动态加载外部.fbx.obj模型文件
项目中有用到这个需求,为实现Unity程序运行状态下,从程序外部动态加载fbx或obj模型,所以研究了一下,目前TriLib比较靠谱,好用,多平台适用。会提供 下载插件地址。
1.效果展示
unity运行时加载fbx
使用的插件 下载插件地址
2、添加插件至Unity工程
将Trilib压缩包内容解压到Unity工程Assets文件夹下,返回工程等待加载,完成后即可在工程目录下看到Trilib目录,包含脚本以及各类License。Trilib插件包含了示例场景,我们的工程就从示例入手。
先择模型,看效果。
3、使用步骤
代码调用示例
/// <summary>
/// Loads the "Models/TriLibSample.obj" Model using the given AssetLoaderOptions.
/// </summary>
/// <remarks>
/// You can create the AssetLoaderOptions by right clicking on the Assets Explorer and selecting "TriLib->Create->AssetLoaderOptions->Pre-Built AssetLoaderOptions".
/// </remarks>
private void Start()
{
var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions);
}
/// <summary>
/// Called when any error occurs.
/// </summary>
/// <param name="obj">The contextualized error, containing the original exception and the context passed to the method where the error was thrown.</param>
private void OnError(IContextualizedError obj)
{
Debug.LogError($"An error occurred while loading your Model: {obj.GetInnerException()}");
}
/// <summary>
/// Called when the Model loading progress changes.
/// </summary>
/// <param name="assetLoaderContext">The context used to load the Model.</param>
/// <param name="progress">The loading progress.</param>
private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
{
Debug.Log($"Loading Model. Progress: {progress:P}");
}
/// <summary>
/// Called when the Model (including Textures and Materials) has been fully loaded.
/// </summary>
/// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
/// <param name="assetLoaderContext">The context used to load the Model.</param>
private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
{
Debug.Log("Materials loaded. Model fully loaded.");
}
/// <summary>
/// Called when the Model Meshes and hierarchy are loaded.
/// </summary>
/// <remarks>The loaded GameObject is available on the assetLoaderContext.RootGameObject field.</remarks>
/// <param name="assetLoaderContext">The context used to load the Model.</param>
private void OnLoad(AssetLoaderContext assetLoaderContext)
{
Debug.Log("Model loaded. Loading materials.");
}
总结
以上就是使用trilib2.1.7 动态加载fbx模型。有问题请留言。