Unity自定义源加载for Ms.LiLi By Vincent


使用TriLib2使unity加载绝对路径模型资源
以stl文件为例。

step1:模型文件准备

首先准备好STL文件。放在任意位置。我这里放在桌面

在这里插入图片描述

step2:TriLib基础使用

再已经导入了TriLib插件的基础上,新建一个scene,新建一个自己的脚本,这里命名为TestLoader.cs 。 把该脚本挂在到摄像头上面。

在这里插入图片描述

step3 :TestLoader.cs 源码
using TriLibCore;
using TriLibCore.General;
using UnityEngine;


public class TestLoader : MonoBehaviour
    {
    // Lets the user load a new model by clicking a GUI button.

    public string ModelPath= "C:\\Users\\Vincent\\Desktop\\dianjiao.stl";

        private void OnGUI()
        {
            // Displays a button to begin the model loading process.
            if (GUILayout.Button("Load Model from File"))
            {
            var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();
            AssetLoader.LoadModelFromFile(ModelPath, OnLoad, OnMaterialsLoad, OnProgress, OnError, null, assetLoaderOptions);

//取消下面注释,就是选择加载的demo,来自官网。
            //    // Creates an AssetLoaderOptions instance.
            //    // AssetLoaderOptions is a class used to configure many aspects of the loading process.
            //    // We won't change the default settings this time, so we can use the instance as it is.
            //    var assetLoaderOptions = AssetLoader.CreateDefaultLoaderOptions();

             Creates the AssetLoaderFilePicker instance.
             AssetLoaderFilePicker is a class that allows users to select models from the local file system.
            var assetLoaderFilePicker = AssetLoaderFilePicker.CreateInstance();  //应该被修改为CreateInstance()
            //    var assetLoaderFilePicker = AssetLoaderFilePicker.Create();
             Shows the model selection file-picker.
            //    assetLoaderFilePicker.LoadModelFromFilePickerAsync("Select a File", OnLoad, OnMaterialsLoad, OnProgress, OnBeginLoad, OnError, null, assetLoaderOptions);
        }
        }

        // This event is called when the model is about to be loaded.
        // You can use this event to do some loading preparation, like showing a loading screen in platforms without threading support.
        // This event receives a Boolean indicating if any file has been selected on the file-picker dialog.
        private void OnBeginLoad(bool anyModelSelected)
        {

        }

        // This event is called when the model loading progress changes.
        // You can use this event to update a loading progress-bar, for instance.
        // The "progress" value comes as a normalized float (goes from 0 to 1).
        // Platforms like UWP and WebGL don't call this method at this moment, since they don't use threads.
        private void OnProgress(AssetLoaderContext assetLoaderContext, float progress)
        {

        }

        // This event is called when there is any critical error loading your model.
        // You can use this to show a message to the user.
        private void OnError(IContextualizedError contextualizedError)
        {

        }

        // This event is called when all model GameObjects and Meshes have been loaded.
        // There may still Materials and Textures processing at this stage.
        private void OnLoad(AssetLoaderContext assetLoaderContext)
        {
            // The root loaded GameObject is assigned to the "assetLoaderContext.RootGameObject" field.
            // If you want to make sure the GameObject will be visible only when all Materials and Textures have been loaded, you can disable it at this step.
            var myLoadedGameObject = assetLoaderContext.RootGameObject;
            myLoadedGameObject.SetActive(false);
        }

        // This event is called after OnLoad when all Materials and Textures have been loaded.
        // This event is also called after a critical loading error, so you can clean up any resource you want to.
        private void OnMaterialsLoad(AssetLoaderContext assetLoaderContext)
        {
            // The root loaded GameObject is assigned to the "assetLoaderContext.RootGameObject" field.
            // You can make the GameObject visible again at this step if you prefer to.
            var myLoadedGameObject = assetLoaderContext.RootGameObject;
            myLoadedGameObject.SetActive(true);
        }
    }
step4 :运行结果

在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值