Unty5.x里Assetbundle的自动标记和打包

最近研究了一下assetbundle的自动标记和打包,参考了很多网上教程写了一篇代码,废话不多说,直接看代码把

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class AssetBundleEditor
{
    #region 自动做标记

    //思路
    //1.找到资源保存的文件夹
    //2.遍历里面每个场景文件夹
    //3.遍历场景文件夹里的所有文件系统
    //4.如果访问的是文件夹:继续访问里面所有的文件系统,直到找到 文件(递归)
    //5.找到文件,修改他的 assetbundle labels
    //6.用 AssetImporter 类 修改名称和后缀
    //7.保存对应的文件夹名和具体路径

    [MenuItem("AssetBundle/Set AssetBundle Labels")]
    public static void SetAssetBundleLabels()
    {
        //移除所有没有使用的标记
        AssetDatabase.RemoveUnusedAssetBundleNames();
        //1.找到资源保存的文件夹
        string assetDirectory = Application.dataPath + "/Res";//"D:/WorkSpace/UnityWorkSpace/MobaClient/AssetBundles/Assets/Res";
        DirectoryInfo directoryInfo = new DirectoryInfo(assetDirectory);
        DirectoryInfo[] sceneDirectories = directoryInfo.GetDirectories();
        //2.遍历里面每个场景文件夹
        foreach (DirectoryInfo tempDirectoryInfo in sceneDirectories)
        {
            string sceneDirectory = assetDirectory + "/" + tempDirectoryInfo.Name;
            DirectoryInfo sceneDirectoryInfo = new DirectoryInfo(sceneDirectory);
            //错误检测
            if (sceneDirectoryInfo == null)
            {
                Debug.LogError(sceneDirectory + "不存在");
                return;
            }
            else
            {
                Dictionary<string, string> namePathDict = new Dictionary<string, string>();
                //3.遍历场景文件夹里的所有文件系统
                //D:\WorkSpace\UnityWorkSpace\MobaClient\AssetBundles\Assets\Res
                //D:/WorkSpace/UnityWorkSpace/MobaClient/AssetBundles/Assets/Res
                int index = sceneDirectory.LastIndexOf("/");
                string sceneName = sceneDirectory.Substring(index + 1);
                onSceneFileSystemInfo(sceneDirectoryInfo, sceneName, namePathDict);

                onWriteConfig(sceneName, namePathDict);
            }
        }//end foreach
        AssetDatabase.Refresh();
        Debug.LogWarning("设置成功");
    }//end set
    /// <summary>
    /// 记录配置文件
    /// </summary>
    private static void onWriteConfig(string sceneName, Dictionary<string, string> namePathDict)
    {
        string path = PathUtil.GetAssetBundleOutPath()+ "/" + sceneName  + "Record.txt";
        using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
        {
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(namePathDict.Count);
                foreach (KeyValuePair<string, string> kv in namePathDict)
                    sw.WriteLine(kv.Key + " " + kv.Value);
            }
        }

    }

    private static void onSceneFileSystemInfo(FileSystemInfo fileSystemInfo, string sceneName, Dictionary<string, string> namePathDict)
    {
        if (!fileSystemInfo.Exists)
        {
            Debug.LogError(fileSystemInfo.FullName + "不存在");
            return;
        }
        DirectoryInfo directoryInfo = fileSystemInfo as DirectoryInfo;
        FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos();
        foreach (var tempfileInfo in fileSystemInfos)
        {
            FileInfo fileInfo = tempfileInfo as FileInfo;
            if (fileInfo == null)
            {
                //代表强转失败,不是文件 就是文件夹
                //4.如果访问的是文件夹:继续访问里面所有的文件系统,直到找到 文件(递归)
                onSceneFileSystemInfo(tempfileInfo, sceneName, namePathDict);
            }
            else
            {
                //文件
                //5.找到文件,修改他的 assetbundle labels
                setLables(fileInfo, sceneName, namePathDict);
            }
        }


    }
    /// <summary>
    /// 修改资源文件的 assetbundle labels
    /// </summary>
    /// <param name="fileInfo"></param>
    /// <param name="sceneName"></param>
    private static void setLables(FileInfo fileInfo, string sceneName, Dictionary<string, string> namePathDict)
    {
        //忽视unity自身生成的meta文件
        if (fileInfo.Extension == ".meta")
            return;
        string bundleName = getBundleName(fileInfo, sceneName);
        int index = fileInfo.FullName.IndexOf("Assets");
        string assetPath = fileInfo.FullName.Substring(index);
        //6.用 AssetImporter 类 修改名称和后缀
        AssetImporter assetImporter = AssetImporter.GetAtPath(assetPath);
        assetImporter.assetBundleName = bundleName;
        if (fileInfo.Extension == ".unity")
            assetImporter.assetBundleVariant = "u3d";
        else
            assetImporter.assetBundleVariant = "assetbundle";
        //添加到字典
        string folderName = "";
        if (bundleName.Contains("/"))
            folderName = bundleName.Split('/')[1];
        else
            folderName = bundleName;
        string bundlePath = assetImporter.assetBundleName + "." + assetImporter.assetBundleVariant;
        if (!namePathDict.ContainsKey(folderName))
            namePathDict.Add(folderName, bundlePath);


    }
    /// <summary>
    /// 获取包名
    /// </summary>
    private static string getBundleName(FileInfo fileInfo, string sceneName)
    {
        string windowPath = fileInfo.FullName;
        string unityPath = windowPath.Replace(@"\", "/");
        int Index = unityPath.IndexOf(sceneName) + sceneName.Length;
        string bundlePath = unityPath.Substring(Index + 1);

        if (bundlePath.Contains("/"))
        {
            string[] temp = bundlePath.Split('/');
            return sceneName + "/" + temp[0];
        }
        else
        {
            return sceneName;
        }

    }
    #endregion

    #region 打包

    [MenuItem("AssetBundle/Build AssetBundles")]
    static void BuildAssetBundles()
    {
        string outPath = PathUtil.GetAssetBundleOutPath();        
        BuildPipeline.BuildAssetBundles(outPath, 0, BuildTarget.StandaloneWindows64);
    }
    #endregion

    #region 删除
    [MenuItem("AssetBundle/Delete All")]
    static void DeleteAssetBundle()
    {
        string outPath = PathUtil.GetAssetBundleOutPath();

        Directory.Delete(outPath, true);
        File.Delete(outPath + ".meta");
        AssetDatabase.Refresh();
    }

    #endregion
}

还有一篇获取路径的

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

/// <summary>
/// 路径
/// </summary>
public class PathUtil
{

    /// <summary>
    /// 获取assetbundle的输出目录
    /// </summary>
    /// <returns></returns>
    public static string GetAssetBundleOutPath()
    {
        string outPath = getPlatformPath() + "/" + getPlatformName();
        if (!Directory.Exists(outPath))
            Directory.CreateDirectory(outPath);
        return outPath;
    }
    /// <summary>
    /// 自动获取对应平台的路径
    /// </summary>
    /// <returns></returns>
    private static string getPlatformPath()
    {
        switch (Application.platform)
        {
            
            case RuntimePlatform.WindowsPlayer:          
            case RuntimePlatform.WindowsEditor:
                return Application.streamingAssetsPath;
                        
            case RuntimePlatform.Android:
                return Application.persistentDataPath;
            default:
                return null;
        }
    }

    /// <summary>
    /// 获取对应平台名字
    /// </summary>
    /// <returns></returns>
    private static string getPlatformName()
    {
        switch (Application.platform)
        {
            case RuntimePlatform.WindowsPlayer:
            case RuntimePlatform.WindowsEditor:
                return "Windows";

            case RuntimePlatform.Android:
                return "Android";
            default:
                return null;
        }
    }
    /// <summary>
    /// 获取www协议的路径
    /// </summary>
    public static string GetWWWPath()
    {
        switch (Application.platform)
        {
            case RuntimePlatform.WindowsPlayer:
            case RuntimePlatform.WindowsEditor:
                return "file:///"+ GetAssetBundleOutPath();

            case RuntimePlatform.Android:
                return "jar:file://" + GetAssetBundleOutPath();
            default:
                return null;
        }
    }
}


本课程总体分为两大部分:理论篇与架构篇AssetBundle架构篇:     1: 讲解Unity原生AssetBundle不适用工程化实战开发的原因与解决方案。     2: 详述AssetBundle框架设计原理图,以及核心设计理念。     3: Untiy编辑器界面全自动化创建AssetBundle打包理念与代码实现。     4: 关于单一AssetBundle包的综合加载与管理以及相应测试实现。     5AssetBundle整体管理。          本模块包含*.Manifest清单文件读取、AB包之间复杂依赖关系管理、整体场景化自动打包与加载管理流程、项目辅助全局定义与路径管理等 一、热更新系列(技术含量:中高级):A:《lua热更新技术中级篇》https://edu.csdn.net/course/detail/27087B:《热更新框架设计之Xlua基础视频课程》https://edu.csdn.net/course/detail/27110C:《热更新框架设计之热更流程与热补丁技术》https://edu.csdn.net/course/detail/27118D:《热更新框架设计之客户端热更框架(上)》https://edu.csdn.net/course/detail/27132E:《热更新框架设计之客户端热更框架(中)》https://edu.csdn.net/course/detail/27135F:《热更新框架设计之客户端热更框架(下)》https://edu.csdn.net/course/detail/27136二:框架设计系列(技术含量:中级): A:《游戏UI界面框架设计系列视频课程》https://edu.csdn.net/course/detail/27142B:《Unity客户端框架设计PureMVC篇视频课程(上)》https://edu.csdn.net/course/detail/27172C:《Unity客户端框架设计PureMVC篇视频课程(下)》https://edu.csdn.net/course/detail/27173D:《AssetBundle框架设计_框架篇视频课程》https://edu.csdn.net/course/detail/27169三、Unity脚本从入门到精通(技术含量:初级)A:《C# For Unity系列之入门篇》https://edu.csdn.net/course/detail/4560B:《C# For Unity系列之基础篇》https://edu.csdn.net/course/detail/4595C: 《C# For Unity系列之中级篇》https://edu.csdn.net/course/detail/24422D:《C# For Unity系列之进阶篇》https://edu.csdn.net/course/detail/24465四、虚拟现实(VR)与增强现实(AR):(技术含量:初级)A:《虚拟现实之汽车仿真模拟系统 》https://edu.csdn.net/course/detail/26618五、Unity基础课程系列(技术含量:初级) A:《台球游戏与FlappyBirds—Unity快速入门系列视频课程(第1部)》 https://edu.csdn.net/course/detail/24643B:《太空射击与移动端发布技术-Unity快速入门系列视频课程(第2部)》https://edu.csdn.net/course/detail/24645 C:《Unity ECS(二) 小试牛刀》https://edu.csdn.net/course/detail/27096六、Unity ARPG课程(技术含量:初中级):A:《MMOARPG地下守护神_单机版实战视频课程(上部)》https://edu.csdn.net/course/detail/24965B:《MMOARPG地下守护神_单机版实战视频课程(中部)》https://edu.csdn.net/course/detail/24968C:《MMOARPG地下守护神_单机版实战视频课程(下部)》https://edu.csdn.net/course/detail/24979
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值