• unity3d Resources.Load動態加載模型資源

    2019/1/22      點擊:

    兩種加載資源方案:Resources.LoadAssetBundle Resources.Load就(jiù)是從一個缺省(shěng)打進程(chéng)序包裏的AssetBundle裏加載資(zī)源而一般AssetBundle文件需要你自己(jǐ)創(chuàng)建,運(yùn)行(háng)時動態加(jiā)載,可以指定路徑和來源的。其實場景裏所有(yǒu)靜態的對象也有這麽一個加載過程,隻是Unity後台替你自動完成(chéng)。

    Resources.Load方法:使用這種方式加載資源,首先需要下Asset目錄下創建一個名為Resources的文件夾,這個命名是U3D規定的方式,然後把資源文件放進(jìn)去,

    當(dāng)然也可以在Resources中再創建子文件夾,當然在代碼加載(zǎi)時需要(yào)添加相應的(de)資源路徑,下麵是一個(gè)簡demo,兩個預設,CubeSphere

    其中Cube放在Resource中的Prebs中,而(ér)Sphere放在Resources跟目錄下,下麵(miàn)分別實(shí)現Resources.Load資源的加(jiā)載:

    using UnityEngine;
    using System.Collections;
    public class LoadResDemo : MonoBehaviour {    private string cubePath = "Prebs/MyCubePreb";
        private string spherePath = "MySpherePreb";
        void Start () {
            //把(bǎ)資源(yuán)加載到(dào)內存中
            Object  cubePreb = Resources.Load(cubePath, typeof(GameObject));
            //用加載得到的資源對象,實例化(huà)遊戲對象,實現遊戲(xì)物體的動態加載
            GameObject cube = Instantiate(cubePreb) as GameObject;
            //以下同理實現Sphere的動態實例化
            //把資源加載(zǎi)到內(nèi)存中
            Object spherePreb = Resources.Load(spherePath, typeof(GameObject));
            //用加載得到的資源對象,實例化遊戲對象,實現遊戲物體的動態加載
            GameObject sphere = Instantiate(spherePreb) as GameObject;
        } 
        void Update () {   
        }
    }
    將上麵的腳本附加到某個遊戲對象上(shàng),在運行遊戲時就可以看到場景中動態創建的上麵的遊(yóu)戲對象了。

    AssetBundle的方動態加(jiā)載(zǎi)遊戲對象。使用AssetBundle打包預(yù)設或者場景可以(yǐ)將與其相關的所有資源打包,這樣很(hěn)好地解決資(zī)源的依賴問題,使得我們可以方便的(de)加載GameObject,首先需要打包資源:

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using System.IO;
    public class AesstBundleTest : MonoBehaviour {
        [MenuItem("Custom Bundle/Create Bundel Main")]
        public static void creatBundleMain()
        {
            //獲取選(xuǎn)擇的對象的路徑
            Object[] os = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            bool isExist = Directory.Exists(Application.dataPath + "/StreamingAssets");
            if (!isExist)
            {
                Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");
            }
            foreach (Object o in os)
            {
                string sourcePath = AssetDatabase.GetAssetPath(o);            string targetPath = Application.dataPath + "/StreamingAssets/" + o.name + ".assetbundle";
                if (BuildPipeline.BuildAssetBundle(o, null, targetPath, BuildAssetBundleOptions.CollectDependencies))
                {
                    print("create bundle cuccess!");
                }
                else
                {
                    print("failure happen");
                }
                AssetDatabase.Refresh();
            }
        }
        [MenuItem("Custom Bundle/Create Bundle All")]
        public static void CreateBundleAll()
        {
            bool isExist = Directory.Exists(Application.dataPath + "/StreamingAssets");
            if (!isExist)
            {
                Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");
            }
            Object[] os = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
            if (os == null || os.Length == 0)
            {
                return;
            }
            string targetPath = Application.dataPath + "/StreamingAssets/" + "All.assetbundle";
            if (BuildPipeline.BuildAssetBundle(null, os, targetPath, BuildAssetBundleOptions.CollectDependencies))
            {
                print("create bundle all cuccess");
            }
            else
            {
                print("failure happen");
            }
            AssetDatabase.Refresh();
        }
    }

    把上麵的代碼放在Editor中,在菜單欄中就可以看見自定的菜單項,選中需要打包的預設,就可(kě)以把對應的預設打包並輸出到StreamAssets中了,然後是動態加(jiā)載資源:

    using UnityEngine;
    using System.Collections;public class LoadBundleTest : MonoBehaviour {
        //不同平台下StreamingAssets的路徑是(shì)不同的,這(zhè)裏需要注意一下。
        public static readonly string PathURL =
        #if UNITY_androids
            "jar:file://" + Application.dataPath + "!/assets/";
        #elif UNITY_iphoness
            Application.dataPath + "/Raw/";
        #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
        "file://" + Application.dataPath + "/StreamingAssets/";
        #else
            string.Empty;
        #endif  // Update is called once per frame
        void Update () {
        
        }
        void OnGUI()
        {
            if (GUILayout.Button("Load Bundle Main"))
            {
                string path_shpere = PathURL + "MySpherePreb.assetbundle";
                StartCoroutine(loadBundleMain(path_shpere));            string path_cube = PathURL + "MyCubePreb.assetbundle";
                StartCoroutine(loadBundleMain(path_cube));
                print(path_cube);
            }        if (GUILayout.Button("Load Bundle All"))
            {
                StartCoroutine(loadBundleAll(PathURL + "All.assetbundle"));
            }
        }    private IEnumerator loadBundleMain(string path)
        {
            WWW bundle = new WWW(path);
          //  yield return bundle;
             Instantiate(bundle.assetBundle.mainAsset);
             bundle.assetBundle.Unload(false);
             yield return 1;
        }    private IEnumerator loadBundleAll(string path)
        {
            WWW bundle = new WWW(path);
            yield return bundle;
            Instantiate(bundle.assetBundle.Load("MyCubePreb"));
            Instantiate(bundle.assetBundle.Load("MySpherePreb"));
            yield return 1;
        }
    }



    AV永久天堂网_奇米狠狠色_亚洲欧美日韩动漫_欧美日韩视频在线观看免费一区二区_日韩精品一二三区_国产AV网站18禁止人_久久久久久精品人妻免费网站不卡_国产最新视频_另类免费视频在线视频二区_久久精品免视国产