Unity runtime program dynamically loads external .fbx.obj model file

Unity runtime program dynamically loads external .fbx.obj model file


This requirement is useful in the project. In order to realize the dynamic loading of fbx or obj models from outside the program when the Unity program is running, I have studied it. At present, TriLib is more reliable, easy to use, and applicable to multiple platforms. Will provide download plug-in address .


1. Effect display

Load fbx when unity is running

The plug-in used to download the plug-in address


2. Add the plugin to the Unity project

Unzip the contents of the Trilib compressed package to the Assets folder of the Unity project, return to the project and wait for loading. After completion, you can see the Trilib directory under the project directory, including scripts and various licenses. The Trilib plugin includes example scenes, and our project starts with the examples.
insert image description here
Choose the model first and see the effect.
insert image description here

3. Use steps

代码调用示例
        /// <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.");
        }



Summarize

The above is to use trilib2.1.7 to dynamically load the fbx model. Please leave a message if you have any questions.

Guess you like

Origin blog.csdn.net/qq_39735878/article/details/130933207