Unity3d 动态加载fbx模型文件

来源:互联网 发布:ajax上传文件到阿里云 编辑:程序博客网 时间:2024/05/21 09:22

方法1(已测试过)
1 将模型拖动到场景中 ,调整好位置。(制作prefab需要)
2 新建Resources(如果工程中有的话 就不用新建了,Resource.Load调用的就是该文件夹下的资源),在该文件夹下建一个prefab,将上面的模型拖动到这个prefab上
3 删除场景中的该物体模型
4 编写脚本,把它仍随便一个GameObject
主要代码如下

 

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class LoadFBX : MonoBehaviour {  
  5.   
  6.     // Use this for initialization  
  7.     void Start () {  
  8.     GameObject gFbx=(GameObject)Instantiate( Resources.Load("che"));  
  9.     }  
  10.       
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.       
  14.     }  
  15. }  

 

搞定

方法2:(没测试过,应该可以,因为之前能成功加载GameObject对象)

1 按方法1 制作prefab 注意调整好位置

2 然后使用AssetBundle导出包选项 create single AssetBundle(这之前需要在工程文件夹中新建一个叫做“Dynamic_Asset”的文件夹)

3 这时可以看到导出的.AssetBundle文件了
4 编写代码

如下

[csharp] view plain copy
  1. public string url;  
  2.     void Start () {  
  3.         string Scname = "scene1_part2.assetbundle";  
  4.         url = "file://F:/EZGUI/Dynamic_Asset/";  
  5.         StartCoroutine(DLAsset(url,Scname));  
  6.     }  
  7.     void Update () {  
  8.   
  9.     }      
  10.     public IEnumerator DLAsset (string url,string Scname) {  
  11.         WWW www = new WWW(url+Scname);  
  12.         yield return www;  
  13.         GameObject GO = (GameObject)Instantiate(www.assetBundle.mainAsset);  
  14.     }  


========================================================================================
如何动态加载模型-0
如何动态加载模型   1,加载封装好的内部文件。 var aaa : Material;//空材质   
var bbb : GameObject;//要绑定材质的模型

[csharp] view plain copy
  1. function Start()     
  2. {     
  3.         aaa.mainTexture = Resources.Load("你的资源名,例如“pic1”不需要文件扩展名");     
  4.        bbb.renderer.material = aaa;     
  5. }  2,加载磁盘文件 var bbb : GameObject;     
  6. function Start () {     
  7.        var www = new WWW ("file://D:\\pic1.jpg"这里也可以是网络图片地址);     
  8.        yield www;     
  9.        bbb.renderer.material.SetTexture("_MainTex", www.texture);     
  10. }   
0 0