樓層一層一層的加,把模型分開,弄成prefab放到Resourse文件夾裏,在代碼裏用Instantiate(Resources.Load("模型名字") as GameObject);html
不一樣的按鈕用Resources.Load加載不一樣的物體。加載其中一個prefab的時候如何把以前加載的Prefab銷燬呢?由於prefab後面會加(clone),因此能夠用destroy(XXX(clone));網絡
文章轉載:http://www.j2megame.com/html/xwzx/ty/2623.htmliphone
動態加載FBX文件 方法1(已測試過) 1 將模型拖動到場景中,調整好位置。(製做prefab須要) 2 新建Resources(若是工程中有的話就不用新建了,Resource.Load調用的就是該文件夾下的資源),在該文件夾下建一個prefab,將上面的模型拖動到這個prefab上 3 刪除場景中的該物體模型 4 編寫腳本,把它仍隨便一個GameObject 主要代碼以下
using UnityEngine; using System.Collections;
public class LoadFBX : MonoBehaviour {
// Use this for initialization void Start () { GameObject gFbx=(GameObject)Instantiate( Resources.Load("che")); } // Update is called once per frame void Update () { } } 搞定
方法2:(沒測試過,應該能夠,由於以前能成功加載GameObject對象)
1 按方法1 製做prefab 注意調整好位置
2 而後使用AssetBundle導出包選項 create single AssetBundle(這以前須要在工程文件夾中新建一個叫作「Dynamic_Asset」的文件夾)
3 這時能夠看到導出的.AssetBundle文件了 4 編寫代碼
以下 public string url; void Start () { string Scname = "scene1_part2.assetbundle"; url = "file://F:/EZGUI/Dynamic_Asset/"; StartCoroutine(DLAsset(url,Scname)); } void Update () {
} public IEnumerator DLAsset (string url,string Scname) { WWW www = new WWW(url+Scname); yield return www; GameObject GO = (GameObject)Instantiate(www.assetBundle.mainAsset); } 工具
========================================================================================測試
下面部分來自:http://www.unity3d8.com/content/如何動態加載模型-0this
如何動態加載模型 1,加載封裝好的內部文件。 var aaa : Material;//空材質 var bbb : GameObject;//要綁定材質的模型 function Start() { aaa.mainTexture = Resources.Load("你的資源名,例如「pic1」不須要文件擴展名"); bbb.renderer.material = aaa; } 2,加載磁盤文件 var bbb : GameObject; function Start () { var www = new WWW ("file://D:\\pic1.jpg"這裏也能夠是網絡圖片地址); yield www; bbb.renderer.material.SetTexture("_MainTex", www.texture); } url
最近作的項目是一個iphone的u3d項目,會用到須要動態加載模型的地方。簡單總結一下。spa