Unity AssetBundle 之 (进阶)简单的实现不同平台给 AB 标签的资源打包 Asset Bundle 包(分平台)的方法

 

 

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
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仙魁XAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值