Unity運(yùn)行時(shí)程序動(dòng)態(tài)加載外部.fbx.obj模型文件
項(xiàng)目中有用到這個(gè)需求,為實(shí)現(xiàn)Unity程序運(yùn)行狀態(tài)下,從程序外部動(dòng)態(tài)加載fbx或obj模型,所以研究了一下,目前TriLib比較靠譜,好用,多平臺(tái)適用。會(huì)提供 下載插件地址。
1.效果展示
unity運(yùn)行時(shí)加載fbx
使用的插件 下載插件地址
2、添加插件至Unity工程
將Trilib壓縮包內(nèi)容解壓到Unity工程Assets文件夾下,返回工程等待加載,完成后即可在工程目錄下看到Trilib目錄,包含腳本以及各類License。Trilib插件包含了示例場(chǎng)景,我們的工程就從示例入手。
先擇模型,看效果。文章來源:http://www.zghlxwxcb.cn/news/detail-564213.html
3、使用步驟
代碼調(diào)用示例
/// <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.");
}
總結(jié)
以上就是使用trilib2.1.7 動(dòng)態(tài)加載fbx模型。有問題請(qǐng)留言。文章來源地址http://www.zghlxwxcb.cn/news/detail-564213.html
到了這里,關(guān)于Unity運(yùn)行時(shí)程序動(dòng)態(tài)加載外部.fbx.obj模型文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!