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

AssetBundle

时间:2018-07-09 19:12:26      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:from   direct   编辑器   als   hunk   lse   str   create   start   

1.使用AB包的原因

(1)减小资源大小

(2)方便更新资源

2.AB使用流程

(1)制定资源的AB属性

(2)构建AB包

(3)上传AB包

(4)加载AB包和包里面的资源

 

实际操作步骤:

(1)取名

技术分享图片

(2)编辑器扩展,方便打包

技术分享图片

技术分享图片
using UnityEditor;
using System.IO;

public class CreateAB
{

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

(3)点击Build AB,打包成AB包

技术分享图片

在文件夹中可看到打包后的资源

技术分享图片

BuildAssetBundleOptions的几个重要属性:

(1)None:使用LZMA算法,对资源整体打包。体积小,加载时间长。

(2)ChunkBasedCompression:使用LZ4算法,对资源分块打包,体积较小,加载时间较短.

(3)UncompressedAssetBundle:不打包,体积较大,加载时间短。

 manifest文件

两个重要属性:

(1)Assets:资源路径

(2)Dependencies:依赖

在场景中创建材质并取名为red,材质包名为red.unity3d,创建cube作为预制体,资源包名为cube.unity3d。

添加如下脚本加载资源:

技术分享图片
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Text : MonoBehaviour {

    void Start () {
        AssetBundle ab1 = AssetBundle.LoadFromFile("AssetsBundles/red.unity.3d");
        AssetBundle ab = AssetBundle.LoadFromFile("AssetsBundles/cube.unity.3d");
        GameObject cube = ab.LoadAsset<GameObject>("Cube");
        Instantiate(cube);
        
    }
}
View Code

AssetBundle的几个重要加载方式:

(1)AssetBundle.LoadFromMemoryAsync

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class Text : MonoBehaviour {

    IEnumerator Start () {
        string path="AssetBundles/red.unity3d";
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        yield return request; 
     AssetBundle bundle = createRequest.assetBundle
        //使用里面的资源
     GameObject wallPrefab = bundle .LoadAsset<GameObject>("CubeWall");
        Instantiate(wallPrefab)

    }
}

(2)AssetBundle.LoadFromFile

        //第二种加载AB的方式 LoadFromFile
        AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        yield return request;
        AssetBundle ab = request.assetBundle;
        //使用里面的资源
     GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
        Instantiate(wallPrefab)
 

(3)WWW.LoadFromCacheOrDownload

        //第三种加载AB的方式 WWW
        while (Caching.ready == false)
        {
            yield return null;
        }
        file://  file:///
        WWW www = WWW.LoadFromCacheOrDownload(@"file:/E:\Unity Project Workspace\AssetBundleProject\AssetBundles\cubewall.unity3d", 1);
        WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundles/cubewall.unity3d", 1);
        yield return www;
        if (string.IsNullOrEmpty(www.error) == false)
        {
            Debug.Log(www.error); yield break;
        }
        AssetBundle ab = www.assetBundle;
        //使用里面的资源
     GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
        Instantiate(wallPrefab)

(4)UnityWebRequest

//第四种方式 使用UnityWebRequest
        //string uri = @"file:///E:\Unity Project Workspace\AssetBundleProject\AssetBundles\cubewall.unity3d";
        string uri = @"http://localhost/AssetBundles/cubewall.unity3d";
        UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
        yield return request.Send();
        //AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
        //使用里面的资源
        GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
        Instantiate(wallPrefab)

AssetBundle资源卸载:

 (1)AssetBundle.Unload(true:)卸载所有资源,即使有资源被使用着。适用于:

  1。在关切切换、场景切换

  2。资源没被用的时候
(2)AssetBundle.Unload(false):卸载所有没用被使用的资源
    个别资源怎么卸载:

  1。通过 Resources.UnloadUnusedAssets.    

  2。场景切换的时候

AssetBundle

标签:from   direct   编辑器   als   hunk   lse   str   create   start   

原文地址:https://www.cnblogs.com/shirln/p/9266529.html

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