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解决!

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

希望大家能把自己的所学和他人一起分享,不要去鄙视别人索取时的贪婪,因为最应该被鄙视的是不肯分享时的吝啬。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值