unity资源打包和加载的接口使用

我们项目中用到打包比较复杂,我打算从简到繁一点一点学习一下。 今天先做个简单的例子,模拟安卓平台打包和加载资源的流程。


1、打包

我先建立一个预制,这个预制上只有一个材质球,如下图所示。

unity资源打包和加载的接口使用_第1张图片

我们将这个预制在android平台下打包并放在一个单独的文件夹下面。最终的打出的结果如下图

打包出来的资源

打包出来的这个文件 2e442ada0d6d205c04656b8701d14e35.unity3d的名字就是资源名的md5以unity3d作为后缀。
先上代码

    public class Builder  
    {
        static string BuildTargetPath = "D:\\WorkSpace\\MobilePreview\\BundleNew";

        static string path = "Assets\\Art\\MyCube.prefab";

        [MenuItem("Build/test")]
        public static void BuildCompleteBundle()
        {
            Object obj = UnityEditor.AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object));  //先把资源加载到内存
            List array = new List();
            array.Add(obj);
            List<string> names = new List<string>();
            names.Add(path);
            string buildPath = BuildTargetPath + "\\" + CommonTool.CommonTool.get_md5_from_string(path) + ".unity3d"; //构建出打包出来的名字

            //调用接口打包 
            bool is_build = BuildPipeline.BuildAssetBundleExplicitAssetNames(
                                    array.ToArray(), 
                                    names.ToArray(), 
                                    buildPath, 
                                    //   打个完整包                               收集所有依赖
                                    BuildAssetBundleOptions.CompleteAssets|BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.DeterministicAssetBundle, //
                                    BuildTarget.Android);
            Debug.Log("[BuildCompleteBundle] " +  is_build); //搞定
        }

    } 
  

这个函数就是把一堆的资源根据目标平台打成一个压缩的unity3d文件, assetNames 包含所有资源的名字,这个名字相当重要, 最后读取资源的时候还需要这个名字来读取。pathName就是打包之后文件名,加载文件的时候需要这个文件名。第四个参数是打包的形式, 这里把所有的依赖资源都打在一起,所以就用了 BuildAssetBundleOptions.CompleteAssets|BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.DeterministicAssetBundle, 第五个参数标识目标平台为android

2、资源加载

我们需要先把打包好的那个unity加载到内存,然后在从里面读取我们需要的那个prefab文件。在unity可以用WWW这个类来加载资源, 加载好之后就是一个 AssetBundle对象, 在用AssetBundle.load 就可以读取到我们想要的那个prefab对象了。代码如图:

    public IEnumerator load_bundle_from_www_coroutine(string _url, string path)
    {
        WWW www = null;
        int try_count = 0;
        const int try_max = 10;
        AssetBundle bundle = null;
        do
        {
            www = new WWW(_url);  //开始异步加载
            yield return www;

            if (String.IsNullOrEmpty(www.error)) //如果没有报错
            {
                bundle = www.assetBundle;
            }

            if (bundle == null)
            {
                if (++try_count < try_max)
                {
                    www.Dispose();
                    yield return null;
                    continue;
                }
            }
            www.Dispose();
            break;
        } while (true);


        if( bundle == null )
        {
            Debug.LogError("can load from dist " + _url);
            yield return null;
        }

        UnityEngine.Object obj = bundle.Load(path, typeof(UnityEngine.Object));  //拿到我们需要的prefab   path = Assets\\Art\\MyCube.prefab
        GameObject target = GameObject.Instantiate(obj) as GameObject;  //实例化一下
        target.SetActive(true);
        target.transform.parent = transform;
        target.name = "LoadFromBundle";
        Debug.LogError("load_bundle_from_www_coroutine success ");
    }

最后GameObject加载出来了
unity资源打包和加载的接口使用_第2张图片

你可能感兴趣的:(unity)