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);    }}