基于web的AssetBundle的研究

来源:互联网 发布:python post文件 编辑:程序博客网 时间:2024/05/18 01:55

原因:(1)在实际开发中如果场景很大,加载时间会比较花费时间,尤其是在unity的web项目中比较明显。比如我做的幼儿园项目web版在网络比较慢的情况下打开一次要花费半个小时左右,这就很浪费时间。而用AssetBundle的流加载方式,通过把预设导出成.assetBundle文件,在项目运行过程中逐步加载克隆将大大缩短加载时间,有利于项目完美运行。下个项目大庆展馆我们考虑用这项技术。

(2)AssetBundle可以将Prefab封装起来,预设身上的游戏组件如材质脚本在封装成AssetBundle后,我们只需要Instantiate就可以放在游戏中使用,并且身上的脚本材质等依然存在。

实现方式:

1.       导出.AssetBundle

[MenuItem("Assets/Build AssetBundle FromSelection")] 

staticvoid ExportResourceRGB2() 

           //打开保存面板,获得用户选择的路径 

           stringpath = EditorUtility.SaveFilePanel("Save Resource",Application.dataPath,"NewResource", "unity3d"); 

          

           if(path.Length != 0) 

           { 

                    //选择的要保存的对象 

                    Object[]selection = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets); 

                    //打包 

                    BuildPipeline.BuildAssetBundle(Selection.activeObject,selection, path, BuildAssetBundleOptions.CollectDependencies |BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows); 

           } 

}

2.       加载:

void Start () {

           bundlePath="file:///C:/plane.unity3d";

           StartCoroutine("downLoadAssetBundle");

      }

IEnumeratordownLoadAssetBundle()

      {

               asset=new WWW(bundlePath);

 

               yield return asset;

               if(rateText!=null)

               rateText.text=asset.progress.ToString();

               AssetBundlebundle=asset.assetBundle;

               Instantiate(bundle.Load("plane"));

               bundle.Unload(false);

      }

3.       缺点:(1)目前我觉得缺点是物体之间的关联度不好调整,如在一个物体中声明一个物体,public GameObject wuti;调用不是很方便,必须通过GameObject.Find()/GameObject.FindObjectWithTag()方法获取(2)模型的Prefab一个个导出所占大小比较小,叠加导出时容量比较大,不利于网络资源加载

 


0 0