Unity插件分享——TriLib 2 - Model Loading Package

4,554 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第5天,点击查看活动详情

概述

本篇文章主要分享一个作者平时在开发过程中使用过的感觉还比较好的插件,希望对各位日后开发过程中能够有所帮助。本篇文章主要分享的插件是TriLib 2 - Model Loading Package。一款非常好用的动态导入模型插件(本地模型)

问题描述

最近做项目遇到一个需求,是要做一个模型库,而这个模型库里所有的模型都是用户可自行编辑的,比如导入,删除,预览等功能。我们都知道Unity中动态加载模型可以直接使用Resource.Load的方法,但是加载本地模型这个方法就不好用了,所以在通过查阅资料后,发现了这款比较好用的插件。(建议各位购买官方正版,本篇文章所使用的是通过他人分享的版本)

image.png

使用

由于本人使用的功能只是导入本地模型,所以插件的其他功能并没有进行深入了解,所以本篇文章只简单的介绍下导入本地模型的使用。
首先导入插件

image.png

我们可以看到在,TriLib文件夹下有很多的案例(TriLibSamples文件夹中),我们找到第一个案例AssetViewer,打开后可以看到,整个案例的界面是如下图所示

image.png

我们运行一下这个案例,通过第一个按钮,LoadModelFromFile从本地加载一个模型看一下

0531-1.gif

接下来我们就通过这个案例,来找一下导入模型的脚本。首先在Scene视图中,我们找到刚刚点击的按钮,可以看到在Hierarchy面板中,整个UI结构如下,而LoadModelFromFile按钮就是我们刚刚点击的导入按钮

image.png

紧接着我们先看一下按钮身上是否有点击事件,如果有,则可以快速的通过点击事件定位到脚本

image.png

通过OnClick点击事件,我们可以大概的看出来,导入模型的方法通过AssetViewer基本上就可以找到了。我们可以看到,在AssetViewer上,只有一个组件,所以我们就从AssetViewer脚本入手

image.png

打开脚本,我们首先可以通过注释找到打开本地文件系统加载模型的方法

/// <summary>Shows the file picker for loading a model from the local file-system.</summary>
public void LoadModelFromFile()
{
    base.LoadModelFromFile();
}

接着往下找(通过F12),定位到AssetViewerBase.cs脚本,可以看到显示模型选择的窗口代码

/// <summary>
/// Shows the file picker for loading a model from local file-system.
/// </summary>
protected void LoadModelFromFile(GameObject wrapperGameObject = null, Action<AssetLoaderContext> onMaterialsLoad = null)
{
    SetLoading(false);
    var filePickerAssetLoader = AssetLoaderFilePicker.Create();
    filePickerAssetLoader.LoadModelFromFilePickerAsync("Select a File", OnLoad, onMaterialsLoad ?? OnMaterialsLoad, OnProgress, OnBeginLoadModel, OnError, wrapperGameObject ?? gameObject, AssetLoaderOptions);
}

接下来就是异步加载模型,这里面回调函数比较多,可能需要大家好好的分析一下,包括加载时调用什么方法,加载完毕又调用什么方法等。

        /// <summary>Loads a Model from the OS file picker asynchronously, or synchronously when the OS doesn't support Threads.</summary>
        /// <param name="title">The dialog title.</param>
        /// <param name="onLoad">The Method to call on the Main Thread when the Model Meshes and hierarchy are loaded.</param>
        /// <param name="onMaterialsLoad">The Method to call on the Main Thread when the Model (including Textures and Materials) has been fully loaded.</param>
        /// <param name="onProgress">The Method to call when the Model loading progress changes.</param>
        /// <param name="onBeginLoad">The Method to call when the model begins to load.</param>
        /// <param name="onError">The Method to call on the Main Thread when any error occurs.</param>
        /// <param name="wrapperGameObject">The Game Object that will be the parent of the loaded Game Object. Can be null.</param>
        /// <param name="assetLoaderOptions">The Asset Loader Options reference. Asset Loader Options contains various options used during the Model loading process.</param>
        public void LoadModelFromFilePickerAsync(string title, Action<AssetLoaderContext> onLoad, Action<AssetLoaderContext> onMaterialsLoad, Action<AssetLoaderContext, float> onProgress, Action<bool> onBeginLoad, Action<IContextualizedError> onError, GameObject wrapperGameObject, AssetLoaderOptions assetLoaderOptions)
        {
            _onLoad = onLoad;
            _onMaterialsLoad = onMaterialsLoad;
            _onProgress = onProgress;
            _onError = onError;
            _onBeginLoad = onBeginLoad;
            _wrapperGameObject = wrapperGameObject;
            _assetLoaderOptions = assetLoaderOptions;
            try
            {
				StandaloneFileBrowser.OpenFilePanelAsync(title, null, GetExtensions(), true, OnItemsWithStreamSelected);
            }
            catch (Exception)
            {
#if (UNITY_WSA || UNITY_ANDROID) && !UNITY_EDITOR
                Dispatcher.InvokeAsync(new ContextualizedAction(DestroyMe));
#else
                DestroyMe();
#endif
                throw;
            }
        }


上述方法是通过打开资源管理器进行加载的方法。加下来大致介绍下直接通过路径加载的方法。
这里就需要打开另一个示例场景LoadModelFromFile,这是直接运行的时候就在在模型。我们可以看到LoadModelFromFileSample.cs脚本就实现了自动加载的功能,那么我们在看一下这个脚本

image.png

首先是一个返回路径的方法

image.png

紧接着就是加载模型方法,最为关键的就是AssetLoader.LoadModelFromFile这个方法。这个也是我们苦苦找寻的加载模型的跟方法。

image.png 而下面的一些方法则全部都是加载模型时的回调函数

image.png

AssetLoader.cs脚本的内容比较多,这里就不一一介绍了,感兴趣的伙伴可以自行的进行了解。
下面展示一下自动加载本地模型而不是Unity自带的模型效果,我们只需要将调用的LoadModelFromFile方法的第一个参数改成我们要加载的模型的地址即可。

image.png 下面第一个是官方案例的,第二个是加载我本地的模型

0531-2.gif

0531-3.gif

结束

加载本地的模型有的时候会出现加载的模型没有材质的问题,会变成白模,这个就需要和美术人员进行沟通,让他们重新设置下材质球的格式才可以。。
今天的插件就介绍到这里,同时也欢迎各位伙伴留言,有什么好用的插件大家共同分享学习。