5.x AssetBundle初步使用

来源:互联网 发布:炒股软件哪款好 编辑:程序博客网 时间:2024/06/02 12:55

首先,定制好你的prefab预制体,并为其设置好assetBundle设定,如图:
这里写图片描述

如图,红色部分可以设置包名,斜线可以创建打包的父级目录,可以多层,点击箭头可以拉出菜单,里面的NEW可以创建多个包名并可随意选择;

    [MenuItem("Assets/Build AssetBundles")]    static void BuildAllAssetBundles()//打包assetBundle标记的文件    {        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath+ "/AssetBundles", BuildAssetBundleOptions.None, EditorUserBuildSettings.activeBuildTarget); Debug.Log("Get Ab At:" + EditorUserBuildSettings.activeBuildTarget);    }

然后就可以打包了,只有上面一段代码,记得引用编辑器名称空间,参数分别为存储路径,打包设置选项,以及平台(!注意一定要打包成对应平台的assetBundle),然后就可以在Asset下找到这个新建的菜单按钮打包;
此功能会自动打包所有设置有包名的prefeb,并通过包名下划线自动分配父级目录;

这里写图片描述

如图,两个AsstBundles文件是打包时会自动生成的,统一管理所有AB包(在后面会有其他作用),而我们的ab包是在environment目录下,因为我们在包名设置上划分了父级路径,每个AB包都会有一个text文件和无后缀的AB包,这个文本文件只是给我们看的,没有其他作用;

同时,也要将用到的材质打个包,贴图打个包,因为之前打包的预制体只是模型;
这样,在系统自动生成的这个叫AsstBundles的总AB包中,会为其创造一个依赖链,这个在实例化模型AB包时需要依据依赖链先将材质包和贴图释放出来;

通过此方法按指定路径下载AB包:

    IEnumerator wwwLoadAssetBundle(string url)//读取AB进行操作    {        currentDownAB_www = null;        disText.text = url;        WWW www = new WWW(url);        yield return www;        if (!string.IsNullOrEmpty(www.error))        {            Debug.Log("LogError-----------------" + www.error);        }        currentDownAB_www = www;        print("-----------------DownLoadEnd>>"+ url);    }

另外,我们需要下载三种类型AB包:

    IEnumerator LoadAB_and_Ins()//下载AB并实例化    {        yield return StartDownManifest();//下载manifest表单        yield return StartDownDepend(manifestAB);//下载依赖包        yield return StartDownLoadMyInsAsset();//目标模型资源包        InsAssetBundle(currentDownAB_www.assetBundle);//实例化目标资源    }

这里写图片描述

通过这个主AB包的文本可以查看到内容,管理着所有AB包的,AB包下面的Dependencies就是该AB包所依赖的其他AB包,所以我们需要先下载主AB包,下载目标AB包,在主AB包里面查找目标AB包所需要的依赖AB包资源(材质,贴图等)并下载

1.下载主AB包:

IEnumerator StartDownManifest()    {             string tempLocalPath = null;//#if UNITY_STANDALONE_WIN || UNITY_EDITOR        tempLocalPath = DeleteSomeStr("file:///", localABPath);//#elif UNITY_ANDROID//        tempLocalPath =localABPath;//#endif        if (CheckLocalExist(tempLocalPath + "AssetBundles"))        {           yield return wwwLoadAssetBundle(localABPath + "AssetBundles");//本地下载manifest表单        }       else        {            yield return wwwLoadAssetBundle(serverABPath + "AssetBundles");//服务器下载manifest表单            StoreAssbdToLocal(tempLocalPath, "AssetBundles", currentDownAB_www.bytes);//存储到本地        }        manifestAB = currentDownAB_www.assetBundle;        print("-----------------End>>StartDownManifest()");    }

2.下载目标资源AB包:

 IEnumerator StartDownLoadMyInsAsset()    {        string tempLocalPath = null;//#if UNITY_STANDALONE_WIN || UNITY_EDITOR        tempLocalPath = DeleteSomeStr("file:///", localABPath);//#elif UNITY_ANDROID//        tempLocalPath =localABPath;//#endif        if (CheckLocalExist(tempLocalPath + "environment/all"))        {            yield return wwwLoadAssetBundle(localABPath + "environment/all");//本地下载目标资源        }        else        {            yield return wwwLoadAssetBundle(serverABPath + "environment/all");//服务器下载目标资源            StoreAssbdToLocal(tempLocalPath, "environment/all", currentDownAB_www.bytes);//存储到本地        }        print("-----------------End>>StartDownLoadMyInsAsset()");    }

3.下载依赖资源:

    IEnumerator DownDependencies(AssetBundle mainAB) //下载所有依赖(在下载完主AB后,回调)    {        AssetBundleManifest mainfest= mainAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");        string tempLocalPath = null;//#if UNITY_STANDALONE_WIN || UNITY_EDITOR        tempLocalPath = DeleteSomeStr("file:///", localABPath);//#elif UNITY_ANDROID//        tempLocalPath =localABPath;//#endif        string[] dps = mainfest.GetAllDependencies("environment/all");        AssetBundle[] dpsBundle = new AssetBundle[dps.Length];        for (int i = 0; i < dps.Length; ++i)        {                      if (CheckLocalExist(tempLocalPath + dps[i]))//本地读取            {                yield return wwwLoadAssetBundle(localABPath + dps[i]);                dpsBundle[i] = currentDownAB_www.assetBundle;            }            else            {                yield return wwwLoadAssetBundle(serverABPath + dps[i]);//服务器下载                dpsBundle[i] = currentDownAB_www.assetBundle;                StoreAssbdToLocal(tempLocalPath, dps[i], currentDownAB_www.bytes);            }        }        currentDownDependAB = dpsBundle;        print("-----------------DownLoadDependtEnd");    }

4.实例化模型:只要把依赖包通过www加载到缓存中,就可以实例化目标模型了

    void InsAssetBundle(AssetBundle insAsset)    {        GameObject newObj = Instantiate(insAsset.LoadAsset("Environment") as GameObject, Vector3.zero, Quaternion.identity) as GameObject;        disText.text = newObj.GetComponent<environmentInfo>().info;        GameObject newObj1 = Instantiate(insAsset.LoadAsset("Environment") as GameObject, new Vector3(1000,10,10), Quaternion.identity) as GameObject;        TextAsset testJson = insAsset.LoadAsset("Json") as TextAsset;        print(testJson.text);    }

确认的一些问题:
1.脚本无法打包,只会在包内存有指引,因此工程中必须保留此脚本,解包后会自动查找添加此脚本;
2.除脚本以为,其他物体上的组件应该都可以打包,目前确认了动画是可以的,虽然动画资源也是在物体上的一个动画组件添加的引用,但是也通过这个引用关系,将本地动画资源打包到包里;
3.如果预制体的引用没有打包设置,unity会在打包预制体时将所有引用资源一并打包.

0 0
原创粉丝点击