久久精品国产精品亚洲精品_日日爽爽_国产免费不卡av_男人的网站你懂得_少妇和子乱视频_超碰碰人人

  • unity3d Resources.Load動(dòng)態(tài)加載模型資源

    2019/1/22??????點(diǎn)擊:

    兩種加載資源方案:Resources.LoadAssetBundle Resources.Load就是從一個(gè)缺省打進(jìn)程序包里的AssetBundle里加載資源而一般AssetBundle文件需要你自己創(chuàng)建,運(yùn)行時(shí)動(dòng)態(tài)加載,可以指定路徑和來(lái)源的。其實(shí)場(chǎng)景里所有靜態(tài)的對(duì)象也有這么一個(gè)加載過(guò)程,只是Unity后臺(tái)替你自動(dòng)完成。

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

    當(dāng)然也可以在Resources中再創(chuàng)建子文件夾,當(dāng)然在代碼加載時(shí)需要添加相應(yīng)的資源路徑,下面是一個(gè)簡(jiǎn)demo,兩個(gè)預(yù)設(shè),CubeSphere

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

    using UnityEngine;
    using System.Collections;
    public class LoadResDemo : MonoBehaviour {
    
        private string cubePath = "Prebs/MyCubePreb";
        private string spherePath = "MySpherePreb";
        void Start () {
            //把資源加載到內(nèi)存中
            Object  cubePreb = Resources.Load(cubePath, typeof(GameObject));
            //用加載得到的資源對(duì)象,實(shí)例化游戲?qū)ο螅瑢?shí)現(xiàn)游戲物體的動(dòng)態(tài)加載
            GameObject cube = Instantiate(cubePreb) as GameObject;
            //以下同理實(shí)現(xiàn)Sphere的動(dòng)態(tài)實(shí)例化
            //把資源加載到內(nèi)存中
            Object spherePreb = Resources.Load(spherePath, typeof(GameObject));
            //用加載得到的資源對(duì)象,實(shí)例化游戲?qū)ο螅瑢?shí)現(xiàn)游戲物體的動(dòng)態(tài)加載
            GameObject sphere = Instantiate(spherePreb) as GameObject;
        } 
        void Update () {   
        }
    }
    將上面的腳本附加到某個(gè)游戲?qū)ο笊希谶\(yùn)行游戲時(shí)就可以看到場(chǎng)景中動(dòng)態(tài)創(chuàng)建的上面的游戲?qū)ο罅恕?/span>

    AssetBundle的方動(dòng)態(tài)加載游戲?qū)ο蟆J褂肁ssetBundle打包預(yù)設(shè)或者場(chǎng)景可以將與其相關(guān)的所有資源打包,這樣很好地解決資源的依賴問(wèn)題,使得我們可以方便的加載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()
        {
            //獲取選擇的對(duì)象的路徑
            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中,在菜單欄中就可以看見(jiàn)自定的菜單項(xiàng),選中需要打包的預(yù)設(shè),就可以把對(duì)應(yīng)的預(yù)設(shè)打包并輸出到StreamAssets中了,然后是動(dòng)態(tài)加載資源:

    using UnityEngine;
    using System.Collections;
    
    public class LoadBundleTest : MonoBehaviour {
        //不同平臺(tái)下StreamingAssets的路徑是不同的,這里需要注意一下。
        public static readonly string PathURL =
        #if UNITY_ANDROID
            "jar:file://" + Application.dataPath + "!/assets/";
        #elif UNITY_IPHONE
            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;
        }
    }



    主站蜘蛛池模板: 国产一级在线免费观看 | 久久精品日产第一区二区三区的特点 | 成人做爰9片免费看网站 | 国产一区不卡在线观看 | 一级成人毛片 | 国产二区在线观看视频 | 色姑娘天天干 | 芭乐草 永久视频在线观看 日韩人妻熟女中文字幕 | 亚洲精品乱码久久久久膏 | 成人免费一级伦理片在线播放 | 国产在线乱码一区二区三区 | 中文字幕一精品亚洲无线一区 | 在线观看免费人成视频播放 | 不卡一本 | 艳妇接受性按摩av | 永久555www成人免费 | 国产精品秘入口A级一区二区 | 精品久久久久久久久无忧传媒 | 国产高中美女福利剧情简介 | 久久久久国产一区二区三区不卡 | 日韩婷婷| 日日摸夜夜骑 | 视频二区亚洲精品 | 免费在线视频a | 久久久久久国产精品免费免费 | 亚洲av无码精品网站 | 国产美女精品AⅤ在线播放 国产免费高清在线 | 国产免费av大片在线观看 | 免费国产在线精品一区二区三区 | 国产夜夜草 | 久久人人人人做人人玩人人视频 | 免费a一毛片 | 码专区—VA亚洲V天堂 | 久久精品国产亚洲AV狼友 | 在线观看免费av片 | 超嫩小younv 国产经典久久 | 中文字幕天天躁日日躁狠狠躁免费 | 久久精品高清 | 日韩啪啪免费视频 | 中文字幕.亚洲无码日 | 亚洲高清免费视频 |