• 您的位置:首頁 > 新聞動態(tài) > Unity3D

    Unity3d 動態(tài)加載模型文件的方法

    2019/1/22??????點(diǎn)擊:
    方法1(已測試過)
    1 將模型拖動到場景中 ,調(diào)整好位置。(制作prefab需要)
    2 新建Resources(如果工程中有的話 就不用新建了,Resource.Load調(diào)用的就是該文件夾下的資源),在該文件夾下建一個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:(沒測試過,應(yīng)該可以,因?yàn)橹澳艹晒虞dGameObject對象)
    1 按方法1 制作prefab 注意調(diào)整好位置
    2 然后使用AssetBundle導(dǎo)出包選項(xiàng) create single AssetBundle(這之前需要在工程文件夾中新建一個叫做“Dynamic_Asset”的文件夾)
    3 這時可以看到導(dǎo)出的.AssetBundle文件了
    4 編寫代碼
    public string url;
        void Start () {
            string Scname = "scene1_part2.assetbundle";
            url = "file://F://asd//Dynamic//";
            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);
        }