AssetBundle打包依赖(宽宽又欠我一顿烧烤)



using
UnityEngine; using System.Collections; using UnityEditor; public class dabao : EditorWindow { /* * 主要就这俩东西 - -、 *BuildPipeline.PushAssetDependencies():依赖资源压栈;    BuildPipeline.PopAssetDependencies():依赖资源出栈。 */ [MenuItem("z/make")] static void CreateAssetBunldesMain_exe() { BuildPipeline.PushAssetDependencies(); Object Ztexture = Resources.Load("z"); string path = Application.dataPath + "/StreamingAssets/z.bytes"; BuildPipeline.BuildAssetBundle(Ztexture, null, path, BuildAssetBundleOptions.CollectDependencies); Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); foreach (Object obj in SelectedAsset) { BuildPipeline.PushAssetDependencies(); string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".bytes"; BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies); BuildPipeline.PopAssetDependencies(); } BuildPipeline.PopAssetDependencies(); //刷新编辑器 AssetDatabase.Refresh(); } }

打包代码完成的功能是: 先把选中预设们公用的那个贴图打包了。然后再打包选中的这几个预设(贴图名字是“z”,预设们名字是 t1和t2)通过push压栈和pop出栈来隔离开,内层可以包含外层的引用,但不包含外层的资源本身。

加载代码的逻辑是:为了试验一下是否有依赖关系,先加载预设t2,等确保加载出来之后,再加载贴图z,等确保加载完贴图之后,再加载预设t1。最后会发现t1上有贴图,而t2上没有贴图。(这个代码编写的逻辑并不好,只是通过协程来挂起时间。但是用来实验还是不错吧~~~~所以 在加载预设之前,必须先把他们引用到的资源加载上)

QQ  745701540
using UnityEngine;
using System.Collections;

public class jiazai : MonoBehaviour
{


    IEnumerator Start()
    {
        Caching.CleanCache();

        #region 无贴图实验
        StartCoroutine(Load("file://" + Application.dataPath + "/StreamingAssets/t2.bytes"));
        yield return new WaitForSeconds(3);
        #endregion

        #region  贴图实验
        StartCoroutine(LoadTP());//加载贴图
        yield return new WaitForSeconds(3);
        StartCoroutine(Load("file://" + Application.dataPath + "/StreamingAssets/t1.bytes"));
      
      
        #endregion
       
       
    }

    IEnumerator LoadTP()
    {
        string path = "file://" + Application.dataPath + "/StreamingAssets/z.bytes";
        WWW bundle = WWW.LoadFromCacheOrDownload(path, 5);
        yield return bundle;
    }


    private IEnumerator Load(string path)
    {
        WWW bundle = WWW.LoadFromCacheOrDownload(path, 5);
        yield return bundle;
        if (bundle != null)
        {
            //加载到游戏中
            Instantiate(bundle.assetBundle.mainAsset);
        }
       
    }
}

这种方式打包之后的三个包的大小(一个贴图   俩预设)

最简单粗暴的打包那俩预设的大小(俩预设)

哎呦不错喔足足小了一个贴图z的资源大小呀~

 另外,忽然发现jpg啊等等的图片打包还不如不打包....越打越大,所以直接把它扔streamAsset里去,然后加载贴图比较好吧大概。

原文地址:https://www.cnblogs.com/Feiyuzhu/p/5646699.html