今天在群里问朋友有没有 同步加载 Assetbundle 的方法,同事给我指点 Unity3d 不知道在哪一个版本新增了一个 AssetBundle.CreateFromFile 的接口,这个接口是同步读取Assetbundle的。于是到帮助文档查询相关资料。
http://docs.unity3d.com/ScriptReference/AssetBundle.CreateFromFile.html
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
AssetBundle.CreateFromFile public static AssetBundle CreateFromFile(string path); Parameters path Path of the file on disk See Also: WWW.assetBundle, WWW.LoadFromCacheOrDownload. Description Loads an asset bundle from a disk. Only uncompressed asset bundles are supported by this function. This is the fastest way to load an asset bundle.
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
与之前使用WWW方式相比,使用 CreateFromFile读取 Assetbundle 方式有以下不同:
1、WWW是异步的, CreateFromFile 是同步的。
2、CreateFromFile接口读取的必须是没有压缩的Assetbundle文件。
来对比两种方式的打包与读取
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
打包Assetbundle 压缩 与 不压缩
如果使用 CreateFromFile 这个API 来读取 Assetbundle ,那么打包 Assetbundle 的时候要注意打包成 非压缩的 ,也就是要加上 这个 设置。
BuildAssetBundleOptions.UncompressedAssetBundle
代码如下:
using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class NewBehaviourScript : Editor { [MenuItem ("cp/build Girl NoLZMA")] static void BuildGirlNoLZMA() { string targetdir = Application.dataPath + "/bundleNoLZMA"; uint crc = 0; //公共资源入栈; BuildPipeline.PushAssetDependencies (); string filePath = "Assets/res/01.png"; Object obj = AssetDatabase.LoadMainAssetAtPath(filePath); //Build; if (BuildPipeline.BuildAssetBundle(obj, null, targetdir+"/01.png.unity3d", out crc,BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows)) { AssetDatabase.Refresh (); } filePath = "Assets/res/Aoki Shizuku.mp3"; obj = AssetDatabase.LoadMainAssetAtPath(filePath); //Build; if (BuildPipeline.BuildAssetBundle(obj, null, targetdir+"/Aoki Shizuku.mp3.unity3d", out crc,BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows)) { AssetDatabase.Refresh (); } //普通资源入栈; BuildPipeline.PushAssetDependencies (); filePath = "Assets/Resources/Girl.prefab"; obj = AssetDatabase.LoadMainAssetAtPath(filePath); //Build; if (BuildPipeline.BuildAssetBundle(obj, null, targetdir+"/Girl.prefab.unity3d", out crc,BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows)) { AssetDatabase.Refresh (); } BuildPipeline.PopAssetDependencies (); BuildPipeline.PopAssetDependencies (); } [MenuItem ("cp/build Girl")] static void BuildGirl() { string targetdir = Application.dataPath + "/bundle"; uint crc = 0; //公共资源入栈; BuildPipeline.PushAssetDependencies (); string filePath = "Assets/res/01.png"; Object obj = AssetDatabase.LoadMainAssetAtPath(filePath); //Build; if (BuildPipeline.BuildAssetBundle(obj, null, targetdir+"/01.png.unity3d", out crc, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows)) { AssetDatabase.Refresh (); } filePath = "Assets/res/Aoki Shizuku.mp3"; obj = AssetDatabase.LoadMainAssetAtPath(filePath); //Build; if (BuildPipeline.BuildAssetBundle(obj, null, targetdir+"/Aoki Shizuku.mp3.unity3d", out crc, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows)) { AssetDatabase.Refresh (); } //普通资源入栈; BuildPipeline.PushAssetDependencies (); filePath = "Assets/Resources/Girl.prefab"; obj = AssetDatabase.LoadMainAssetAtPath(filePath); //Build; if (BuildPipeline.BuildAssetBundle(obj, null, targetdir+"/Girl.prefab.unity3d", out crc, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneWindows)) { AssetDatabase.Refresh (); } BuildPipeline.PopAssetDependencies (); BuildPipeline.PopAssetDependencies (); } }
加上 不压缩 的设置后,打包 Assetbundle 速度明显变快。
对比两种方式打包出来的 Assetbundle 文件。 文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
WWW 与 CreateFromFile 方式读取 Assetbundle
CreateFromFile 是同步操作,WWW 是异步操作。
using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { //Debug.Log ("Update"); } void OnGUI() { if(GUILayout.Button("Load Girl")) { StartCoroutine(LoadDepends("file://"+Application.dataPath+"/bundle/01.png.unity3d")); StartCoroutine(LoadDepends("file://"+Application.dataPath+"/bundle/Aoki Shizuku.mp3.unity3d")); StartCoroutine(Load("file://"+Application.dataPath+"/bundle/Girl.prefab.unity3d")); } if(GUILayout.Button("Load Girl NOLZMA")) { AssetBundle assetbundle01=AssetBundle.CreateFromFile(Application.dataPath+"/bundleNoLZMA/01.png.unity3d"); AssetBundle assetbundlemp3=AssetBundle.CreateFromFile(Application.dataPath+"/bundleNoLZMA/Aoki Shizuku.mp3.unity3d"); AssetBundle assetbundle=AssetBundle.CreateFromFile(Application.dataPath+"/bundleNoLZMA/Girl.prefab.unity3d"); Instantiate(assetbundle.mainAsset); } } private IEnumerator LoadDepends(string path) { WWW www = new WWW (path); Debug.Log("LoadDepends 1"); yield return www; Debug.Log ("LoadDepends 2"); Object obj= Instantiate (www.assetBundle.mainAsset); Debug.Log("LoadDepends 3"); yield return new WaitForSeconds (3.0f); } private IEnumerator Load(string path) { yield return new WaitForSeconds (3f); WWW www = new WWW (path); Debug.Log("Load 1"); yield return www; Debug.Log ("Load 2"); Object obj= Instantiate (www.assetBundle.mainAsset); Debug.Log("Load 3"); www.assetBundle.Unload (false); Resources.UnloadUnusedAssets (); } }
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn
注意:
使用 不压缩 打包 Assetbundle 的方式,推荐大家使用 7Zip 的 LZMA 压缩算法进行整体压缩,在游戏开始时进行解压。
示例工程下载:
http://pan.baidu.com/s/1o63fOz4
文章转自http://blog.csdn.net/huutu 星环游戏 http://www.thisisgame.com.cn