Unity高阶-项目优化-资源管理

www

WWW可以从网络上加载文本、图片、音频等形式的内容.WWW可以支持http,hppts和file三种协议。我们通常接触到的WWW默认都是指http协议,现在我们来说说file协议,该协议可以用来访问本地资源(绝对路径)。例如我们希望加载文件D:\TestFile\1.png这个文件,则此时对应的C#脚本为:using UnityEngine;using System.Collections;public class Test : MonoBehaviour {// Use this for initializationIEnumerator Start () {WWW www = new WWW("file://D:\\TestFile\\1.png");yield return www; Debug.Log(www.error);if(www != null && string.IsNullOrEmpty(www.error) ){Debug.Log("here is done"); Texture texture=www.texture; GetComponent().material.mainTexture =texture;

}

}

}

注意到这里出现了yield return结构,这表示这里使用到了协程,因此我们需要付出的代价就是需要在项目中使用StartCoroutine等协程相关的方法来调用这些协程。

AssetBundle资源动态加载

AssetBundle只是一种使用LZMA压缩方式压缩的资源文件。AssetBundle打包的时候,你可以指定一个mainAsset,那么加载完之后就可以通过AssetBundle.mainAsset来获取到了。你也可以不指定mainAsset,直接打包一堆内容进去,然后加载后通过AssetBundle.LoadAsset指定名字的读取出来。IEnumerator Start () { //PC端本地文件路径 //url = @"file://D:\\MyJpg.png"; //移动端 //url = "file://" + Application.streamingAssetsPath + "/MyJpg.png"; //WWW www = new WWW(url); //yield return www; //Texture tex = www.texture; //cube.GetComponent().material.mainTexture = tex;

        string url = "file://" + Application.streamingAssetsPath + "/abc.assetbundle";

        WWW www = new WWW(url);

        yield return www;

        Debug.Log(www.isDone);

        Instantiate(www.assetBundle.LoadAsset("Cube"));

        Instantiate(www.assetBundle.LoadAsset("Sphere"));

    }

AssetBundle的依赖结构

在资源之间,存在着依赖的关系。你可以把资源拆分得很细,比如一个模型,由网格模型、材质、贴图构成,你可以把每一个小部分都拆开,各自打包成压缩文件。当Unity需要加载使用的时候,把该模型的所有依赖的小资源都加载起来,然后根据依赖关系组装,就变回了我们看到的资源了。

//加载资源前,先加载依赖

public class LoadAssetbundle : MonoBehaviour

{

    void Start()

    {

        // 1.加载Manifest文件 

        AssetBundle manifestBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/StreamingAssets");

        if (manifestBundle != null)

        {

            AssetBundleManifest manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");

            // 2.获取依赖文件列表 

            string[] cubeDepens = manifest.GetAllDependencies("qiku");

            AssetBundle[] dependsAssetbundle = new AssetBundle[cubeDepens.Length];

            for (int index = 0; index < cubeDepens.Length; index++)

            {

                // 3.加载所有的依赖资源 

                string depPath = Application.streamingAssetsPath + "/" + cubeDepens[index];

                dependsAssetbundle[index] = AssetBundle.LoadFromFile(depPath);

            }

            // 4.加载资源 

            string Path = "file://" + Application.streamingAssetsPath + "/qiku.unity3d";

            AssetBundle cubeBundle = AssetBundle.LoadFromFile(Path);

            GameObject cube = cubeBundle.LoadAsset("cube") as GameObject;

            GameObject.Instantiate(cube);

        }

    }

}

技术陷阱

Resources.Load方法传入的资源路径需是从Resources文件夹下一级开始的相对路径且不能包含扩展名;而AssetBundle.LoadAsset方法传入的资源名需是从Assets文件开始的全路径且要包含扩展名。路径不区分大小写,建议全用小写,因为AssetBundle.GetAllAssetNames方法返回的资源名都是小写的。

Unity5打包AssetBundle时会自动处理依赖关系,但是在运行时加载的时候却不会,程序需要自己处理,先加载依赖包。

AssetBundle.CreateFromFile不能加载压缩过的AssetBundle,所以我们只能用WWW来异步加载AssetBundle。

参看BundleManager使用文档

1、资源依赖

2、资源打包

3、资源加载

using UnityEngine;

public class TestDownloadManager : MonoBehaviour

{

IEnumerator Start()

{

//

You should only use the file name to access your bundle but not the full url.

yield return StartCoroutine( DownloadManager.Instance.WaitDownload("cube.assetBundle") );

WWW www = DownloadManager.Instance.GetWWW("cube.assetBundle");

var bundle = www.assetBundle;

GameObject cube = Instantiate(bundle.Load(“cube”)) as GameObject;

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容