最近在做动态加载读取外部模型的功能使用了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()
{
}
}