Unity动态加载3D模型

在Unity中创建游戏对象的方法有 3 种:

  • 第一种是将物体模型资源由 Project 视图直接拖曳到 Hierarchy 面板中;
  • 第二种是在 Unity 3D 菜单 GameObject 中创建 Unity 3D 自带的游戏对象,如 Cube、Camera、Light 等;
  • 第三种是利用脚本编程,动态创建或删除游戏对象。

本文尝试采用第三种方法,即利用脚本动态加载3D模型文件,从而创建游戏对象。
网上搜索“Unity3d 动态加载fbx模型文件”,大部分都是对文章https://blog.csdn.net/ldkcumt/article/details/51098206的转载,先亲身做一下实践。
Unity版本:Unity 2018.3.7f1 (64-bit)  随VS2017一起安装。
方法一
1 将模型拖动到场景中 ,调整好位置。(制作prefab预制体需要)
2 新建Resources(如果工程中有的话 就不用新建了,Resources.Load调用的就是该文件夹下的资源),在该文件夹下建一个prefab,将上面的模型拖动到这个prefab上
3 删除场景中的该物体模型
4 编写脚本,把它随便扔给一个GameObject

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class LoadFBX : MonoBehaviour
 6 {
 7     public string url;
 8     // Start is called before the first frame update
 9     void Start()
10     {
11         GameObject gFbx = (GameObject)Instantiate(Resources.Load("che"));
12     }
13 
14     // Update is called once per frame
15     void Update()
16     {
17         
18     }
20 }

方法二
1 按方法1 制作prefab (注意调整好位置)
2 然后使用AssetBundle导出包
3 这时可以看到导出的.AssetBundle文件了
4 编写代码
第2步详解:参考https://www.jianshu.com/p/f4c685cf487a

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEditor;
 4 using System.IO;
 5 public class AssetBundleBuilder
 6 {
 7     [MenuItem("Assets/Build AssetBundle")]
 8     static public void BuildAssetBundle()
 9     {
10         Caching.ClearCache();
11         string path = Application.streamingAssetsPath + "/" + "AssetBundles" + "/" + "Windows";
12         if (!Directory.Exists(path))
13         {
14             Directory.CreateDirectory(path);
15         }
16         BuildPipeline.BuildAssetBundles(path, 0, EditorUserBuildSettings.activeBuildTarget);
17         AssetDatabase.Refresh();
18     }
19 }

加载代码修改:

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class LoadFBX : MonoBehaviour
 6 {
 7     public string url;
 8     // Start is called before the first frame update
 9     void Start()
10     {
11         //GameObject gFbx = (GameObject)Instantiate(Resources.Load("che"));
12         string name = "che.first";
13         url = "file://C:/Users/yakong/Documents/FirstUnity3d/Assets/StreamingAssets/AssetBundles/Windows/";
14         StartCoroutine(LoadAsset(url, name));
15     }
16 
17     // Update is called once per frame
18     void Update()
19     {
20         
21     }
22 
23     // LoadAssert 
24     public IEnumerator LoadAsset(string url, string Scname)
25     {
26         WWW www = new WWW(url + Scname);
27         yield return www;
28         AssetBundle bundle = www.assetBundle;
29         foreach (var name in bundle.GetAllAssetNames())
30         {
31             Debug.Log(name);
32             if (name != "")
33             {
34                 GameObject go = (GameObject)Instantiate(bundle.LoadAsset(name));
35             }     
36         }  
37     }
38 }

PS:直接使用www.assetBundle.mainAsset时运行为null,参考https://stackoverflow.com/questions/42775797/cannot-load-an-asset-bundle解决!

最终的启动运行加载效果:

原文地址:https://www.cnblogs.com/MakeView660/p/12196362.html