资源解决方案 AssetBundle.压缩,解压,实例化,依赖关系

参考博客
打包策略

笔记内容

预制体:
AssetBundle: 预制体 --> UI加载? resources 预制体 都是加载过来的. UI 都采用动态绑定

就是assetbundle下载过程 通过网络下载的
红警 腾讯 资源包更新 需要消耗100MB 流量
现在正在解压本过程不消耗流量 进度条
跳转场景
WWW www = new WWW(ABURL)
AssetBundle ab = www.assetBundle;

AB包 是一个特殊格式的压缩包 关于unity资源都能压缩 不包含代码的 UI 模型 音乐
热更新: 不干掉原来的进行更新(打补丁)

具体步骤

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

BuildAssetBundleTool

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

示例

编译器扩展及压缩AB包

在这里插入图片描述

  1. 编译器扩展必须在Editor文档内
  2. 需要引用UnityEditor
  3. 不要继承MonoBehaviour
  4. [MenuItem(“AssetBundleTools/BuidAB”)]
  5. 代码实现

压缩AB包

  1. 创建好路径,streamingAssets文件下再加Windows文件夹用以存储
  2. 代码实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;//编译器扩展菜单引用
using System.IO;


public class BuildAssetBundleTools{
    [MenuItem("AssetBundleTools/BuidAB")]//固定格式,创建菜单选项AssetBundleTools,二级菜单为BuidAB
    public static void Building()
    {
        string abPath = Application.streamingAssetsPath + "\\Windows";//定义路径,固定格式,要写入到Windows文件夹内
        //固定格式,压缩                 (路径,服务器,标准格式)
        BuildPipeline.BuildAssetBundles(abPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
    }
}

实例化预制体

  1. 预制体制作完成后需要在右下角进行设置AssetBundle项,这个名字即是AB包的名字,如名字相同则在同个压缩包内
  2. 创建脚本并挂载
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Load : MonoBehaviour {

	// Use this for initialization
	void Start () {
		string abPath= Application.streamingAssetsPath + "\\Windows\\Cube";//路径
        AssetBundle ab = AssetBundle.LoadFromFile(abPath);//解压固定格式
        if (ab==null)
        {
            Debug.LogError("加载失败");
            return;
        }
        GameObject cubeObj = ab.LoadAsset<GameObject>("Sphere");//解压固定格式
        if (cubeObj!=null)
        {
            Instantiate(cubeObj);//实例化出来
        }
        else
        {
            Debug.LogError("该资源不存在");
        }
        StartCoroutine("creat",ab);//开启协程
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    IEnumerator creat(AssetBundle ab)
    {
        //yield return new WaitForSeconds(3);
        //ab.Unload(false);//释放内存
        yield return new WaitForSeconds(3);
        Instantiate(ab.LoadAsset<GameObject>("Cube"));
    }
}

WWW实现资源加载

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

public class WWWL : MonoBehaviour {

	// Use this for initialization
	void Start () {
        //本地必须加入file:// 这样子www才能进行访问
        string path = "file://" + Application.streamingAssetsPath + "/Windows/cube";
        StartCoroutine("load", path);
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    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>("Cube");
                    Instantiate(obj);
                }
            }
            else
            {
                Debug.LogError("加载失败");
            }
        }
    }
}

依赖关系加载示例

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

public class load1 : MonoBehaviour {
    public string ABName;//加载的AB包设置的名字
    string _abPath;//AB包路径
    string _manPath;//依赖关系包路径
    List<AssetBundle> MyABList;//用来装依赖关系
	// Use this for initialization
	void Start () {
        MyABList = new List<AssetBundle>();//初始化变量
        _abPath = Application.streamingAssetsPath + "/Windows/";//路径未写完调用需要加上ABname;
        _manPath = Application.streamingAssetsPath + "/Windows/Windows";//依赖路径
        #region 加载依赖固定格式
        AssetBundle _manAB = AssetBundle.LoadFromFile(_manPath);
        AssetBundleManifest manifest = _manAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        string[] depen = manifest.GetAllDependencies(ABName);//传递您想要依赖项的包的名称。
        if (depen.Length!=0)
        {
            foreach (string s in depen)
            {
                MyABList.Add(LoadABByName(s));//加进顺序表,通过方法传递名字返回一个加载好的AssetBundle值
            }
        }
        #endregion
        
        #region 实例化
        AssetBundle _ab = AssetBundle.LoadFromFile(_abPath + ABName);
        GameObject _copyAB = _ab.LoadAsset<GameObject>("Capsule");
        Instantiate(_copyAB);
        #endregion
        #region 释放内存
        foreach (var item in MyABList)
        {
            item.Unload(false);
        }
        #endregion
    }

    // Update is called once per frame
    void Update () {
		
	}
    private AssetBundle LoadABByName(string name)
    {
        return AssetBundle.LoadFromFile(_abPath + name);
    }
}

猜你喜欢

转载自blog.csdn.net/luxifa1/article/details/83547571