最新国产在线视频_一色一伦一区二区三区的区别_欧美成人影院亚洲综合图_国产亚洲一区精品_大胆欧美熟妇xxbbwwbw高潮了_久久午夜无码鲁丝片

  • 您的位置:首頁 > 新聞動態 > Unity3D

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

    2019/1/22??????點擊:

    兩種加載資源方案:Resources.LoadAssetBundle Resources.Load就是從一個缺省打進程序包里的AssetBundle里加載資源而一般AssetBundle文件需要你自己創建,運行時動態加載,可以指定路徑和來源的。其實場景里所有靜態的對象也有這么一個加載過程,只是Unity后臺替你自動完成。

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

    當然也可以在Resources中再創建子文件夾,當然在代碼加載時需要添加相應的資源路徑,下面是一個簡demo,兩個預設,CubeSphere

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

    using UnityEngine;
    using System.Collections;
    public class LoadResDemo : MonoBehaviour {
    
        private string cubePath = "Prebs/MyCubePreb";
        private string spherePath = "MySpherePreb";
        void Start () {
            //把資源加載到內存中
            Object  cubePreb = Resources.Load(cubePath, typeof(GameObject));
            //用加載得到的資源對象,實例化游戲對象,實現游戲物體的動態加載
            GameObject cube = Instantiate(cubePreb) as GameObject;
            //以下同理實現Sphere的動態實例化
            //把資源加載到內存中
            Object spherePreb = Resources.Load(spherePath, typeof(GameObject));
            //用加載得到的資源對象,實例化游戲對象,實現游戲物體的動態加載
            GameObject sphere = Instantiate(spherePreb) as GameObject;
        } 
        void Update () {   
        }
    }
    將上面的腳本附加到某個游戲對象上,在運行游戲時就可以看到場景中動態創建的上面的游戲對象了。

    AssetBundle的方動態加載游戲對象。使用AssetBundle打包預設或者場景可以將與其相關的所有資源打包,這樣很好地解決資源的依賴問題,使得我們可以方便的加載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()
        {
            //獲取選擇的對象的路徑
            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中,在菜單欄中就可以看見自定的菜單項,選中需要打包的預設,就可以把對應的預設打包并輸出到StreamAssets中了,然后是動態加載資源:

    using UnityEngine;
    using System.Collections;
    
    public class LoadBundleTest : MonoBehaviour {
        //不同平臺下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;
        }
    }



    主站蜘蛛池模板: 在线观看www成人影院 | 狠狠爱免播放器 | 天天干天天插天天 | 欧美日韩一区二区在线 | 精品人伦一区二区 | 久久久久久成人毛片免费看 | 99热这里只有精品9988 | 亚洲av日韩精品久久久久久a | 精品欧洲av无码一区二区14 | 国产一级淫秽免费黄色视频 | 波多野结系列18部无码观看a | 免费观看又色又爽又黄的韩国 | 国产欧美成aⅴ人高清 | 日日摸日日踫夜夜爽无码 | 日本少妇高潮喷水视频 | 亚洲大尺度无码专区尤物 | 人妻av一区二区三区精品 | 一区二区三区在线观看免费 | 色八戒一区二区三区四区 | 91夜夜蜜桃臀一区二区三区 | 熟女少妇在线视频播放 | 少妇人体色www网站 最近中文字幕国语免费 | 亚洲日韩A∨无码久 | 国产色视频在线播放 | 国产亚洲福利 | 无遮挡又色又刺激的女人视频 | 亚洲综合中文字幕一区二区 | 在线观看国产网址你懂的 | 少妇饥渴XXHD麻豆XXHD骆驼 | 放荡老师张开双腿任我玩 | 中国农村熟妇性视频 | 野花社区wwW高清视频 | 91精品无码久久久久久久久 | 国产一级片网 | 亚洲日本欧美综合在线一 | 四虎影院久久 | 免费无码一区二区三区A片 97人妻无码一区二区精品免费 | 成人无号精品一区二区三区 | 97免费人妻无码视频 | 国产熟睡乱子伦视频 | 找av在线 |