zoukankan      html  css  js  c++  java
  • 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
Copyright © 2011-2022 走看看