Unity AssetBundle 之 (进阶)简单的实现根据当前平台给 AB 标签的资源打包 Asset Bundle 包(自动取分平台打包)的方法
目录
Unity AssetBundle 之 (进阶)简单的实现根据当前平台给 AB 标签的资源打包 Asset Bundle 包(自动取分平台打包)的方法
一、简单介绍
Unity中的一些基础知识点。
本节介绍,Asset Bundle 在 Unity中的使用,进阶第二篇,给已经自动 AB 标签上资源,会自动区分不同平台进行 Asset Bundle 不同平台的进行打包,有不对的地方欢迎指正。
二、实现原理
1、[MenuItem()] 实现在 Editor 下执行打包操作
2、BuildPipeline.BuildAssetBundles() 实现AB打包到目标路径
3、根据 Unity 平台宏 区分打包 AssetBundle 包
三、注意事项
1、根据需要切换平台,打包不同的 Asset Bundle 包
2、打包脚本一定要放在 Editor 文件夹下
3、打包函数接口注意要是 staic 静态函数
四、效果预览
五、实现步骤
1、打开Unity,新建一个空工程
2、把资源导进来,这里 AssetBundle 标记标签的文件夹为 AB_Resources,文件结构如下,需要标记的资源如图功能文件夹放置
3、新建脚本 BuildAssetBundle,用来给标签了资源打包成AB包,PathTools 一些 AB 路径的管理脚本
4、打包 Asset Bundle,要提前给资源打上AB 标签
可以参看这篇博文,自动给资源打上 AB 标签
Unity AssetBundle 之 (进阶)简单的自动给资源打上 AssetBundle 标签(分平台),方便 AssetBundle 打包的方法
5、菜单栏就会有 AssetBundleTools -- BuildAllAssetBundles 工具了
6、点击 BuildAllAssetBundles 就会给AB标签的资源进行 Asset Bundle 打包
六、关键代码
1、BuildAssetBundle
/****************************************************
文件:BuildAssetBundle.cs
作者:Administrator
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/14 17:26:18
功能:Nothing
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace ABFw
{
/// <summary>
/// AssetBundle 打包工具
/// </summary>
public class BuildAssetBundle
{
/// <summary>
/// 打包生成所有的AssetBundles(包)
/// </summary>
[MenuItem("AssetBundleTools/BuildAllAssetBundles")]
public static void BuildAllAB()
{
// 打包AB输出路径
string strABOutPAthDir = string.Empty;
// 获取“StreamingAssets”文件夹路径(不一定这个文件夹,可自定义)
strABOutPAthDir = PathTools.GetABOutPath();
// 判断文件夹是否存在,不存在则新建
if (Directory.Exists(strABOutPAthDir) == false)
{
Directory.CreateDirectory(strABOutPAthDir);
}
// 打包生成AB包 (目标平台自动根据当前平台设置)
#if UNITY_STANDALONE_WIN
BuildPipeline.BuildAssetBundles(strABOutPAthDir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
#elif UNITY_IPHONE
BuildPipeline.BuildAssetBundles(strABOutPAthDir, BuildAssetBundleOptions.None, BuildTarget.iOS);
#elif UNITY_ANDROID
BuildPipeline.BuildAssetBundles(strABOutPAthDir, BuildAssetBundleOptions.None, BuildTarget.Android);
#endif
}
}
}
2、PathTools
/****************************************************
文件:PathTools.cs
作者:仙魁XAN
邮箱:https://blog.csdn.net/u014361280
日期:2020/08/14 17:11:52
功能:AB框架的 路径工具类
1、路径常量
2、路径方法
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace ABFw
{
public class PathTools
{
/* 路径常量 */
public const string AB_RESOURCES = "AB_Resources"; // 打包AB包根路径
/* 路径方法 */
/// <summary>
/// 得到 AB 资源的输入目录
/// </summary>
/// <returns></returns>
public static string GetABResourcesPath()
{
return Application.dataPath + "/" + AB_RESOURCES;
}
/// <summary>
/// 获得 AB 包输出路径
/// 1\ 平台(PC/移动端等)路径
/// 2\ 平台名称
/// </summary>
/// <returns></returns>
public static string GetABOutPath()
{
return GetPlatformPath() + "/" + GetPlatformName();
}
/// <summary>
/// 获得平台路径
/// </summary>
/// <returns></returns>
private static string GetPlatformPath()
{
string strReturenPlatformPath = string.Empty;
#if UNITY_STANDALONE_WIN
strReturenPlatformPath = Application.streamingAssetsPath;
#elif UNITY_IPHONE
strReturenPlatformPath = Application.persistentDataPath;
#elif UNITY_ANDROID
strReturenPlatformPath = Application.persistentDataPath;
#endif
return strReturenPlatformPath;
}
/// <summary>
/// 获得平台名称
/// </summary>
/// <returns></returns>
public static string GetPlatformName()
{
string strReturenPlatformName = string.Empty;
#if UNITY_STANDALONE_WIN
strReturenPlatformName = "Windows";
#elif UNITY_IPHONE
strReturenPlatformName = "IPhone";
#elif UNITY_ANDROID
strReturenPlatformName = "Android";
#endif
return strReturenPlatformName;
}
/// <summary>
/// 返回 WWW 下载 AB 包加载路径
/// </summary>
/// <returns></returns>
public static string GetWWWAssetBundlePath()
{
string strReturnWWWPath = string.Empty;
#if UNITY_STANDALONE_WIN
strReturnWWWPath = "file://" + GetABOutPath();
#elif UNITY_IPHONE
strReturnWWWPath = GetABOutPath() + "/Raw/";
#elif UNITY_ANDROID
strReturnWWWPath = "jar:file://" + GetABOutPath();
#endif
return strReturnWWWPath;
}
}//Class_End
}