unity 使用TriLib插件动态读取外部模型

本文介绍了如何在Unity中使用triLib插件实现动态加载外部模型,包括下载插件、配置渲染管线、处理错误和进度更新,以及提供示例代码以快速实现功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做动态加载读取外部模型的功能使用了triLib插件,废话不多说直接干货。
第一步下载导入插件,直接分享主打白嫖共享,不搞花里胡哨的。
链接:https://pan.baidu.com/s/1DK474wSrIZ0R6i0EBh5V8A
提取码:tado
导入后第一步新建一个管理渲染管线的文件
在这里插入图片描述
第二步建立一个渲染管线的文件,放入上一个创建的文件中在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
配置完成后新建一个文件夹需要把文件和贴图放在同一文件夹内
在这里插入图片描述
注意添加后缀名称,在这里插入图片描述

然后抄一下demo代码 有兴趣的可以去研究demo 我是为了快速实现就不细讲了

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


public class GetLoadFbx : MonoBehaviour
{
 

    //文件路径要加载的模型的绝对路径,不能为空
    string modelFilename;
    [Header("模型贴图必须在同级目录,会自动读取原始模型的使用贴图")]
    //文件名称
    public string name;
    public SetUV GetSetUV;//这个可以忽略
    public AssetLoaderOptions options;//这个就是你创建管理渲染的物体
    void Start()
    {
        GetSetUV=GetComponent<SetUV>();
        modelFilename = Application.dataPath + "/GetModel/Model/SAST5000/";
        LoadNameModel(name);
    }

    void LoadNameModel(string name)
    {
       modelFilename += name;
       AssetLoader.LoadModelFromFile(modelFilename, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, options);     
    }

    /// <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)
    {

    }

    /// <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.");
    }

    void Update() 
    {
    }
  
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值