码迷,mamicode.com
首页 > 其他好文 > 详细

assetbundle

时间:2018-08-18 13:03:30      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:类型   abs   android   request   参数   开始   oid   object c   bundle   

---恢复内容开始---

此文源于unityManul(高版本)

1)生成assetbundle

    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string dir = "AB";
        if (Directory.Exists(dir) == false)
        {
            Directory.CreateDirectory(dir);
        }
        BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
    }

2)同步本地加载

        var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));//可以实全路径也可以是相对路径如 "AB/cc.dd"
        if (myLoadedAssetBundle == null) {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset.<GameObject>("MyObject");
        Instantiate(prefab);

3)异步本地加载

    IEnumerator LoadFromMemoryAsync(string path)
    {
        AssetBundleCreateRequest createRequest = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));//
        yield return createRequest;
        AssetBundle bundle = createRequest.assetBundle;
        var prefab = bundle.LoadAsset<GameObject>("MyObject");
        Instantiate(prefab);
    }

问题:AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path))LoadFromMemoryAsync只要传入byte[]类型参数即可,而且在android中如果assetbundle放在streamingassets下file类是无法读取的,所以可以通过其他方法异步加载,如www类。加载到byte[]然后传给LoadFromMemoryAsync方法调用。异步方法还有更直接的方法如下。

4)WWW异步加载

此加载assetbundle的方法将被弃用

            while (!Caching.ready)
                    yield return null;

        var www = WWW.LoadFromCacheOrDownload("http://myserver.com/myassetBundle", 5);
        yield return www;
        if(!string.IsNullOrEmpty(www.error))
        {
            Debug.Log(www.error);
            yield return;
        }
        var myLoadedAssetBundle = www.assetBundle;

        var asset = myLoadedAssetBundle.mainAsset;

如果存在本地,则LoadFromCacheOrDownload("http://myserver.com/myassetBundle", 5)中的http链接改为本地路径,但是路径前要加http://,或者采用类new System.Uri(本地路径).absoluteUri;获取相关相关uri路径。

 5)UnityWebRequest方法

        string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;       
 UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
        yield return request.Send();
        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
        GameObject cube = bundle.LoadAsset<GameObject>("Cube");
        GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
        Instantiate(cube);
        Instantiate(sprite);

6)加载Assetbundle内容

官方api中提供了众多方法,最基本的方法为abCube.LoadAsset<T>方法

        AssetBundle abMat = AssetBundle.LoadFromFile(mat);
        AssetBundle abCube = AssetBundle.LoadFromFile(cubep);
        AssetBundle abSphere = AssetBundle.LoadFromFile(spherep);

        GameObject cube = abCube.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
        GameObject sphere = abSphere.LoadAsset<GameObject>("Sphere");
        Instantiate(sphere);

7)通过打包的总文件获取依赖

AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath);//打包的总文件位置
AssetBundleManifest manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] dependencies = manifest.GetAllDependencies("cc.dd"); //获取cc.dd的所有依赖文件.
foreach(string dependency in dependencies)
{
    AssetBundle.LoadFromFile(Path.Combine(assetBundlePath, dependency));
}

 

 

 

---恢复内容结束---

assetbundle

标签:类型   abs   android   request   参数   开始   oid   object c   bundle   

原文地址:https://www.cnblogs.com/llstart-new0201/p/9496593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!