当前位置:   article > 正文

Unity中的StreamingAssets— AB包打包方法及AB包加载_unityab包打包

unityab包打包

打包StreamingAssets

创建资源
1.  Assets下新建GameRes文件夹
2.  新建一个UI预设
  • 1
  • 2

在这里插入图片描述

手动打包
  1. 加载官方提供好的打包工具:Asset Bundle Browser
  2. Windows->Package Manager 搜索Asset Bundle Browser,Install在这里插入图片描述
  3. Windows-> AssetBundle Browser打开打包界面
  4. 直接将要打包的资源拖进Confihgure界面,或者右键点击添加新建在这里插入图片描述在这里插入图片描述
  5. Build界面
    1. Build Target :选择目标打包平台
    2. Output Path:输出路径
    3. Clear Folders:每次打包时,将文件夹全部清空再打包
    4. Copy to StreamingAssets:打包时将资源自动复制到StreamingAssets文件夹中
    5. Compression:压缩方式,No Compression(不会把AB包进行压缩,解压快,但包特别大),LZMA(压缩包最小,解压满,如果需要使用AB包中的一个资源,需要将整个包解压),LZ4(压缩包大于LZMA,用什么就解压什么,内存占用低)
    6. Exclude Type Information:打包时不写入资源的类型信息
    7. Force Rebuild:重新打包时需要重新构建包。和Clear Folders类似,不同点在于不会删除不再存在的包。一般选择Clear Folders代替。
    8. Ignore Type Tree Changes:增量构建检查时,忽略类型树的更改
    9. Append Hash:将文件的哈希值附加到资源包名上
    10. Strict Mode:严格模式,如果打包时报错,则打包直接失败(无法成功)。一般可以打开。
    11. Dry Run Build:运行时构建
  6. 打包出的文件
    1. 主包 : StreamingAssets文件夹中会有一个和AssetBundle文件夹中预设输出路径名字一样的打包文件(全是二进制),这些就是主包,包含包与包的依赖关系
    2. 资源文件:没有后缀名的文件就是资源文件
    3. Manifest文件:对应资源文件的配置信息,AB包文件信息;当加载时,提供了关键信息,资源信息,依赖关系,版本信息等等。
代码打包
  1. Editor下的代码
public class ABTool : Editor
{
    [MenuItem("GameTool/BuildAB包")]
    public static void BuildAB()
    {
        string targetPath = Application.streamingAssetsPath + "/ABRes";
        BuildUtils.BuildGameResBundle(targetPath);
        Debug.Log("BuildAssetBundle Done");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  1. 打包代码
public class BuildUtils
{
    /// <summary>
    /// 打包游戏资源AssetBundle
    /// </summary>
    public static void BuildGameResBundle(string targetPath)
    {
        Hashtable tb = new Hashtable();
        tb["uiprefabs.bundle"] = "GameRes/AUI";
        AssetBundleBuild[] buildArray = MakeAssetBundleBuildArray(tb);
        BuildBundles(buildArray, targetPath);
    }

    /// <summary>
    /// 根据哈希表构建AssetBundleBuild列表
    /// </summary>
    /// <param name="tb">哈希表,key为assetBundleName,value为目录</param>
    /// <returns></returns>
    public static AssetBundleBuild[] MakeAssetBundleBuildArray(Hashtable tb)
    {
        AssetBundleBuild[] buildArray = new AssetBundleBuild[tb.Count];
        int index = 0;
        foreach (string key in tb.Keys)
        {
            buildArray[index].assetBundleName = key;
            List<string> fileList = new List<string>();
            fileList = GetFiles(Application.dataPath + "/" + tb[key], true);
            buildArray[index].assetNames = fileList.ToArray();
            ++index;
        }

        return buildArray;
    }

    /// <summary>
    /// 递归遍历获取目标目录中的所有文件
    /// </summary>
    /// <param name="sourceDir">目标目录</param>
    /// <param name="splitAssetPath">是否要切割目录,以Assets目录为根</param>
    public static List<string> GetFiles(string sourceDir, bool splitAssetPath)
    {
        List<string> fileList = new List<string>();
        string[] fs = Directory.GetFiles(sourceDir);
        string[] ds = Directory.GetDirectories(sourceDir);
        for (int i = 0, len = fs.Length; i < len; ++i)
        {
            var index = splitAssetPath ? fs[i].IndexOf("Assets") : 0;
            fileList.Add(fs[i].Substring(index));
        }
        for (int i = 0, len = ds.Length; i < len; ++i)
        {
            fileList.AddRange(GetFiles(ds[i], splitAssetPath));
        }
        return fileList;
    }

    /// <summary>
    /// 打AssetBundle
    /// </summary>
    /// <param name="buildArray">AssetBundleBuild列表</param>
    public static void BuildBundles(AssetBundleBuild[] buildArray, string targetPath)
    {
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }

        BuildPipeline.BuildAssetBundles(targetPath, buildArray, BuildAssetBundleOptions.ChunkBasedCompression, GetBuildTarget());
    }

    /// <summary>
    /// 获取当前平台
    /// </summary>
    public static BuildTarget GetBuildTarget()
    {

#if UNITY_STANDALONE
        return BuildTarget.StandaloneWindows;
#elif UNITY_ANDROID
        return BuildTarget.Android;
#else
        return BuildTarget.iOS;
#endif
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  1. 点击工具栏上的GameTool/BuildAB包开始自动打包

  2. 生成AB资源在这里插入图片描述

  3. 注意事项

    1. **打包带Shader的资源或者特效的预设时,要在设置中添加对应的Shaser。**设置方法如下—
    2. 打开BuIild Setting -> Player Setting
    3. 打开Graphics 下滑找到** Built-in Shader Setting -> Always Included Shaders
    4. 添加用到的Shaders在这里插入图片描述

资源加载

加载资源
 /// AssetBundle资源字典
 Dictionary<string, AssetBundle> dic_abres;
 
 /// <summary>
 /// 加载资源包
 /// </summary>
 void LoadABRes()
 {
     dic_abres = new Dictionary<string, AssetBundle>();
     string[] res = { "uiprefabs"};
     for (int i = 0; i < res.Length; i++)
     {
         StartCoroutine(LoadAb(res[i]));
     }
 }

 IEnumerator LoadAb(string name)
 {
    AssetBundleCreateRequest ab = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/ABRes/" + name + ".bundle");
    yield return ab;
    if (ab.assetBundle != null && !dic_abres.ContainsKey(name)) dic_abres.Add(name, ab.assetBundle);
 }
 
 /// <summary>
 /// 加载Assets资源
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="Abname"></param>
 /// <param name="Resname"></param>
 /// <returns></returns>
 public T LoadAssets<T>(string Abname, string Resname) where T : UnityEngine.Object
 {
    T obj = null;
    //Debug.Log("Type: " + typeof(T).ToString() + "/" + "Res: " + Resname);
    if (!dic_abres.ContainsKey(Abname))
    {
        Debug.LogError("AB资源不存在");
    }
    else
    {
        if (typeof(T) == typeof(GameObject))
        {
        	/// 如果是GameObject就返回一个Clone的Obj
            obj = Instantiate(dic_abres[Abname].LoadAsset<T>(Resname));
        }
        else
        {
            obj = dic_abres[Abname].LoadAsset<T>(Resname);
        }
    }

    return obj;
  }
  
   private void Awake()
  {
     LoadABRes();
  }
  
 void Start()
{
    GameObject go = LoadAssets<GameObject>("loadui", name);
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/540843
推荐阅读
相关标签
  

闽ICP备14008679号