Unity5 AssetBundle 打包代码和加载代码

用代码给选中的对象打包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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值