Unity几种加载AssetBundle的方式

AseetBundle分组策略:
1,把经常更新的资源放在一个单独的包里面,跟不经常更新的包分离
2,把需要同时加载的资源放在一个包里面
3,可以把其他包共享的资源放在一个单独的包里面
4,把一些需要同时加载的小资源打包成一个包
5,如果对于一个同一个资源有两个版本,可以考虑通过后缀来区分  v1  v2  v3  unity3dv1 unity3dv2

1,Build的路径(随意只要是在硬盘上都可以的)
2,BuildAssetBundleOptions
BuildAssetBundleOptions.None:使用LZMA算法压缩,压缩的包更小,但是加载时间更长。使用之前需要整体解压。
                            一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。
                            在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。
BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快
BuildAssetBundleOptions.ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,但是我们可以加载指定资源而不用解压全部。

注意使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。

卸载有两个方面
1,减少内存使用
2,有可能导致丢失
所以什么时候去卸载资源
AssetBundle.Unload(true)卸载所有资源,即使有资源被使用着
   (1,在关切切换、场景切换2,资源没被用的时候 调用)
AssetBundle.Unload(false)卸载所有没用被使用的资源
   个别资源怎么卸载1,通过 Resources.UnloadUnusedAssets.    2,场景切换的时候

文件的校验:
CRC MD5 SHA1
相同点:
CRC、MD5、SHA1都是通过对数据进行计算,来生成一个校验值,该校验值用来校验数据的完整性。
 不同点:
1. 算法不同。CRC采用多项式除法,MD5和SHA1使用的是替换、轮转等方法;
2. 校验值的长度不同。CRC校验位的长度跟其多项式有关系,一般为16位或32位;MD5是16个字节(128位);SHA1是20个字节(160位);
3. 校验值的称呼不同。CRC一般叫做CRC值;MD5和SHA1一般叫做哈希值(Hash)或散列值;
4. 安全性不同。这里的安全性是指检错的能力,即数据的错误能通过校验位检测出来。CRC的安全性跟多项式有很大关系,相对于MD5和SHA1要弱很多;MD5的安全性很高,不过大概在04年的时候被山东大学的王小云破解了;SHA1的安全性最高。
5. 效率不同,CRC的计算效率很高;MD5和SHA1比较慢。

6. 用途不同。CRC一般用作通信数据的校验;MD5和SHA1用于安全(Security)领域,比如文件校验、数字签名等。


存在图集重复的问题:用到一张图片就会把那个图片所在的图集打包一次

AssetBundle包查看工具(官方): https://github.com/Unity-Technologies/AssetBundles-Browser

具体代码如下:

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

public class LoadFromFileExample : MonoBehaviour {

	// Use this for initialization
	IEnumerator Start () {
        string path = "AssetBundles/cubewall.unity3d";
        //AssetBundle ab = AssetBundle.LoadFromFile(path);
        //GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
        //Instantiate(wallPrefab);

        //AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");
        //Object[] objs= ab.LoadAllAssets();
        //foreach(Object o in objs)
        //{
        //    Instantiate(o);
        //}

        //第一种加载AB的方式 LoadFromMemoryAsync
        //AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
        //yield return request;
        //AssetBundle ab = request.assetBundle;
        //AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));


        //第二种加载AB的方式 LoadFromFile
        //AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
        //yield return request;
        //AssetBundle ab = request.assetBundle;

        //第三种加载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;

        //第四种方式 使用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 manifestAB =  AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
        AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        //foreach(string name in manifest.GetAllAssetBundles())
        //{
        //    print(name);
        //}
        string[] strs =  manifest.GetAllDependencies("cubewall.unity3d");
        foreach (string name in strs)
        {
            print(name);
            AssetBundle.LoadFromFile("AssetBundles/" + name);
        }
    }
    private void Update()
    {
        //if (Input.GetMouseButtonDown(0))
        //{
        //    AssetBundle.LoadFromFile("AssetBundles/share.unity3d");
        //}
    }

}

关于Unity通过AssetBundle包动态加载场景的方式,代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour {

    void Awake()
    {
        StartCoroutine(LoadingScene());
    }

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {

    }

    IEnumerator LoadingScene()
    {
        string path = Application.dataPath + "/steam.unity3d";
        WWW download = WWW.LoadFromCacheOrDownload(path, 0);
        yield return download;
        SceneManager.LoadScene("LoadScene");
    }
}

附件代码如下:(此代码为unity Edition代码,需要放在Edition文件夹中)

using UnityEditor;

public class BuildSceneEditor
{
    [@MenuItem("build/BuildWebplayerStreamed")]
    static void Build()
    {
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = new[] { "Assets/Scenes/LoadScene/LoadScene.unity" , "Assets/scene1.unity" };
        buildPlayerOptions.locationPathName = "steam.unity3d";
        buildPlayerOptions.target = BuildTarget.StandaloneWindows64;
        buildPlayerOptions.options = BuildOptions.None;
        BuildPipeline.BuildPlayer(buildPlayerOptions);  //不会自己创建文件夹
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34818497/article/details/79465053