Unity进阶---AssetBundle

AssetBundle的定义和作用:
1,AssetBundle是一个压缩包包含模型、贴图、预制体、声音、甚至整个场景,可以在游戏运行的时候被加载;
2,AssetBundle自身保存着互相的依赖关系;
3,压缩包可以使用LZMA和LZ4压缩算法,减少包大小,更快的进行网络传输;
4,把一些可以下载内容放在AssetBundle里面,可以减少安装包的大小;

AssetBundle使用流程:

  1. 进行AB包的时候进行属性设置
  2. 将设置好的属性(预制体…) 压缩成为一个特定的文件 代码压缩的!!!
  3. 放置到web/gameserver上面
  4. 客户端经过检验之后下载
  5. 下载之后解压实例化

编译器扩展:
BuildAssetBundleTool
1. 在editor目录下
2. AssetBundle的名字就是压缩包的名字和实质内容基本无关!
ab.Unload(true); --> 从项目和内存中都干掉
ab.Unload(false); --> 只干掉内存
在这里插入图片描述

编译器扩展

using UnityEngine;
using UnityEditor;
using System.IO;

public class BuildAssetBundleTools{

	[MenuItem("AssetBundleTools/BuildAB")]
    public static void Building()
    {
        
        string _path = Application.streamingAssetsPath + "\\Windows";
        BuildPipeline.BuildAssetBundles(_path, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

依赖

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

public class LoadingMan : MonoBehaviour {

    public string Name;  //定义一个需要加载或实例化物体的名字
    string _path;   //路径
    string _manth;
    private List<AssetBundle> ManListAB;   //定义一个列表  用来存储加载完毕后的依赖关系
    void Start () {
        ManListAB = new List<AssetBundle>();
        _path = Application.streamingAssetsPath + "/Windows/";   //  "/Windows/Name"   写活了
        _manth = Application.streamingAssetsPath + "/Windows/Windows";  //路径文件地址

        AssetBundle manAB = AssetBundle.LoadFromFile(_manth);  //加载路径
        AssetBundleManifest manifest = manAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");  //Windows所有的依赖关系
        string[] depen = manifest.GetAllDependencies(Name);  //得到你想要的那个物体的 依赖关系(材质AssetBundle的名称)
        if (depen.Length != 0)
        {
            foreach (string v in depen)  //遍历string[]里面的依赖关系
            {
                //调用LoadByName方法   挨个加载依赖关系 并存储在列表里
                Debug.Log(v);
                ManListAB.Add(LoadByName(v));
            }
        }
        //加载物体信息 并开始实例化
        AssetBundle ab = AssetBundle.LoadFromFile(_path + Name);  //文件拼接  拼成完整的路径地址
        GameObject obj = ab.LoadAsset<GameObject>("Remo");
        Instantiate(obj);
        foreach (var v in ManListAB)
        {
            v.Unload(false);
        }
        ab.Unload(false);//必须要有这句话 否则重复加载 报错
        //下面实例化的物体将没有材质  因为依赖关系被释放
        AssetBundle ab01 = AssetBundle.LoadFromFile(_path + Name);
        GameObject obj01 = ab01.LoadAsset<GameObject>("Remo");
        Instantiate(obj01, Vector3.one, Quaternion.identity);
    }

    void Update () {
        
    }
    private AssetBundle LoadByName(string name)
    {
        return AssetBundle.LoadFromFile(_path + name);
    }
}

简单加载

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

public class Loading : MonoBehaviour {

    private Vector3 a = new Vector3(3, 0, 0);
    
    // Use this for initialization
    void Start () {
        string _path = Application.streamingAssetsPath + "\\Windows\\asd";
        AssetBundle ab = AssetBundle.LoadFromFile(_path);
        if (ab == null)
        {
            Debug.LogError("加载失败");
            return;
        }
        GameObject obj = ab.LoadAsset<GameObject>("Cube");
        if (obj != null)
        {
            Instantiate(obj);
        }else
        {
            Debug.LogError("该资源不存在");
        }
        StartCoroutine("Creat",ab);
	}
    IEnumerator Creat(AssetBundle ab)
    {
        yield return new WaitForSeconds(3);
        //ab.Unload(false);
        yield return new WaitForSeconds(5);
        GameObject obj = Instantiate(ab.LoadAsset<GameObject>("Sphere"),a,Quaternion.identity);
    }
 }

利用WWW加载

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

public class WWWLL : MonoBehaviour {

	// Use this for initialization
	void Start () {
        string _path = "file://" + Application.streamingAssetsPath + "/Windows/char";
        StartCoroutine("Load", _path);
    }
    IEnumerator Load(string path)
    {
        using (WWW load01 = new WWW(path))
        {
            yield return load01;
            if (load01 != null)
            {

                AssetBundle ab = load01.assetBundle;
                if (ab != null)
                {
                    GameObject obj = ab.LoadAsset<GameObject>("Capsule");
                    Instantiate(obj);
                }
            }else
            {
                Debug.LogError("加载失败");
            }

        }
    }

猜你喜欢

转载自blog.csdn.net/qq_43140883/article/details/83585548
今日推荐