Unity5 AssetBundle 打包代码和加载代码
来源:互联网 发布:导购网站源码 下载 编辑:程序博客网 时间:2024/06/06 09:39
用代码给选中的对象打包Assetbundle,方便给自己打包的内容分类。如果先设置Assetbundlename在打包的时候会一次将设置了Assetbundle的内容打包,不利于分类。
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using System.IO;public class BuildAssetBundle { [MenuItem("Assets/AssetBundle/Build Each",false,1)] static void BuildAssetbundle1() { BuildEach(); } [MenuItem("Assets/AssetBundle/Build Each(Uncompress)",false,1)] static void BuildAssetbundle2() { BuildEach(false); } static void BuildEach(bool compressed = true) { if(Selection.objects.Length ==0) { EditorUtility.DisplayDialog("Error","No resource selection in project panel","OK"); return; } UnityEngine.Object[] selectedAsset = Selection.objects; string savePath = EditorUtility.SaveFolderPanel("Save AssetBundle", Application.dataPath + "/StreamingAssets/",""); if(savePath!="") { string assetPath = Application.dataPath + "/StreamingAssets/"; if(!Directory.Exists(assetPath)){ Directory.CreateDirectory(assetPath); } #if UNITY_IPHONE BuildTarget bt = BuildTarget.iOS; #elif UNITY_ANDROID BuildTarget bt = BuildTarget.Android; #else BuildTarget bt = BuildTarget.StandaloneOSXUniversal; #endif BuildAssetBundleOptions bo = BuildAssetBundleOptions.DeterministicAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle; if(compressed == false) { bo = bo | BuildAssetBundleOptions.UncompressedAssetBundle; } AssetBundleBuild[] abb = new AssetBundleBuild[selectedAsset.Length]; int counter = 0; foreach(UnityEngine.Object obj in selectedAsset) { abb[counter] = new AssetBundleBuild(); abb[counter].assetBundleName = obj.name; abb[counter].assetNames = new string[1]; abb[counter].assetNames[0] = AssetDatabase.GetAssetPath(obj); Debug.Log(AssetDatabase.GetAssetPath(obj)); Debug.Log(abb[counter].assetNames[0]); counter++; } BuildPipeline.BuildAssetBundles(savePath,abb,bo,bt); AssetDatabase.Refresh(); } }}
加载AssetBundle的两种方法,里面的一些路径需要根据自己的文件路径来写。特别要注意各平台的路径的不同
using UnityEngine;using System.Collections;public class LoadAssetBundel : MonoBehaviour { public string mainfestname = "Prefab"; public string assetbundlename = "test"; // Use this for initialization public string path; private void Start() { #if UNITY_EDITOR || UNITY_IOS path = "file://" + Application.streamingAssetsPath; #elif UNITY_ANDROID path = Application.streamingAssetsPath; #endif } void OnGUI() { if(GUILayout.Button("LoadAssetBundle")) { // 加载总的manifest的assetbundle AssetBundle mainfestbunde = AssetBundle.LoadFromFile(Application.dataPath + "/StreamingAssets/Prefab/Prefab"); //AssetBundleManifest AssetBundleManifest mainfest = (AssetBundleManifest)mainfestbunde.LoadAsset("AssetBundleManifest"); //处理依赖关系 string[] dependentAssetBundles = mainfest.GetAllDependencies("sphere.assetbundle"); //加载所有的依赖文件 AssetBundle[] abs = new AssetBundle[dependentAssetBundles.Length]; for(int i = 0; i< abs.Length; i++) { abs[i] = AssetBundle.LoadFromFile(Application.dataPath + "/StreamingAssets/Prefab/" + dependentAssetBundles[i]); } //加载需要的文件 AssetBundle spherebundle = AssetBundle.LoadFromFile(Application.dataPath + "/StreamingAssets/Prefab/sphere.assetbundle"); GameObject Sphere = (GameObject)spherebundle.LoadAsset("Sphere"); Instantiate(cube); mainfestbunde.Unload(false); spherebundle.Unload(false); } if(GUILayout.Button("LoadFromCacheOrDownload")) { StartCoroutine(LoadFromCacheOrDownload()); } } IEnumerator LoadFromCacheOrDownload() { WWW www = WWW.LoadFromCacheOrDownload(path + "/Prefab/Prefab", 0); yield return www; AssetBundle mainfestbundle = www.assetBundle; AssetBundleManifest manifest = (AssetBundleManifest)mainfestbundle.LoadAsset("AssetBundleManifest"); mainfestbundle.Unload(false); string[] dependAssetbundle = manifest.GetAllDependencies("human.assetbundle"); //加载所有的依赖文件 AssetBundle[] abs = new AssetBundle[dependAssetbundle.Length]; for (int i = 0; i < abs.Length; i++) { WWW www2 = WWW.LoadFromCacheOrDownload(path + "/Prefab/" + dependAssetbundle[i], 0); yield return www2; abs[i] = www2.assetBundle; } WWW www3 = WWW.LoadFromCacheOrDownload(path + "/Prefab/human.assetbundle", 0); yield return www3; AssetBundle human = www3.assetBundle; GameObject obj = (GameObject)Instantiate(human.LoadAsset("human")); human.Unload(false); }}
阅读全文
0 0
- Unity5 AssetBundle 打包代码和加载代码
- unity5.x assetbundle打包和加载
- Unity5 AssetBundle 打包以及加载
- Unity5 AssetBundle 打包以及加载
- Unity5.x AssetBundle打包和加载深入分析
- Unity5.X打包与加载AssetBundle
- AssetBundle打包和加载
- Unity5打包assetbundle
- Unity5.X AssetBundle打包
- Unity5.4打包AssetBundle
- Unity5.4 AssetBundle的完整项目源码(包括打包和加载)
- Unity5.X AssetBundle加载
- AssetBundle -- 打包和本地加载
- Unity5和unity4的AssetBundle打包策略对比分析
- unity5.0资源打包AssetBundle
- Unity5的AssetBundle(一、打包)
- Unity5-ABSystem(三):AssetBundle加载
- Unity5.3.5的AssetBundle加载
- IP问问:从无到有——一种基于IP地址定位的位置|场景营销技术
- spring 事务JDBCTransaction JTATransaction CMTTransaction
- Tensorflow方法tf.SparseTensor使用说明
- mybatis:元素内容必须由格式正确的字符数据或标记组成
- python 正则化
- Unity5 AssetBundle 打包代码和加载代码
- 欢迎页设置和访问静态资源
- 【14.4】c++ primer plus 课后编程答案
- 反射获取对象成员的字段值,getFields()和getDeclaredFields()用法区别
- Web开发之:Java与PHP的八大比较
- NDK提供的共享库(Prebuilt)
- 深入理解HTTP协议(转载)
- python 实现斯坦福机器学习实验2.1
- Oracle 中MERGE语句的用法(转载)