3、AssetBundle统一管理命名

代码参考蛮牛教育CTO教你万能框架教程


利用Editor统一AssetBundle的命名规则。

文件结构如下所示



命名规则:

1.对Art/Scenes下文件进行命名,所有文件的AssetBundleName为Scenes下二级子文件夹名称,例如:Cube(1)的assetBundleName为sceneone/font

2.其中普通文件.ld,场景文件后缀.u3d


代码如下

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

public class AssetBundleEditor : Editor
{

    [MenuItem("ITools/BuildeAssetBundle")]
    public static void BuildAssetBundle()
    {
        BuildPipeline.BuildAssetBundles(Application.dataPath + "/AssetBundle", 0, EditorUserBuildSettings.activeBuildTarget);
        //刷新
        AssetDatabase.Refresh();
    }

    [MenuItem("ITools/MarkAssetBundle")]
    public static void MarkAssetBundle()
    {
        AssetDatabase.RemoveUnusedAssetBundleNames();
        string path = Application.dataPath + "/Art/Scenes";
        DirectoryInfo dir = new DirectoryInfo(path);
        FileSystemInfo[] fileInfo = dir.GetFileSystemInfos();
        for (int i = 0; i < fileInfo.Length; i++)
        {
            FileSystemInfo tmpFile = fileInfo[i];
            if (tmpFile is DirectoryInfo)
            {
                string tmpPath = Path.Combine(path, tmpFile.Name);
                ScenesOverView(tmpPath);
            }
        }

        //刷新
        AssetDatabase.Refresh();
    }
    
    /// <summary>
    /// 遍历文件并生成记录文件,文件名:文件夹名称+Record.txt
    /// </summary>
    /// <param name="path">文件路径</param>
    private static void ScenesOverView(string path)
    {
        string textFileName = "Record.txt";
        string tmpPath = path + textFileName;
        FileStream fs = new FileStream(tmpPath, FileMode.OpenOrCreate);
        StreamWriter bw = new StreamWriter(fs);

        //存储对应关系
        Dictionary<string, string> theWriter = new Dictionary<string, string>();
        GetRelativePath(path, theWriter);
        foreach (var item in theWriter)
        {
            bw.WriteLine(item.Key + " | " + item.Value);
        }
        bw.Close();
        fs.Close();
        
    }

    // D:/XXXX/Assets/Art/Scenes\\XXX/XXX/...
    // SceneOne/load

    /// <summary>
    /// 截取相对路径
    /// </summary>
    /// <param name="fullPath">完整路径</param>
    /// <param name="theWriter">文本记录</param>
    public static void GetRelativePath(string fullPath, Dictionary<string, string> theWriter)
    {
        // 得到 D:/XXXX/ 总长度
        int tmpCount = fullPath.IndexOf("Assets");
        int tmpLength = fullPath.Length;

        //得到 Assets/Art/Scenes\\XXX/XXX/... 相对路径
        string relativePath = fullPath.Substring(tmpCount, tmpLength - tmpCount);
        DirectoryInfo dir = new DirectoryInfo(fullPath);
        if (dir != null)
        {
            ListFiles(dir, relativePath, theWriter);
        }
        else
        {
            Debug.LogError("Path " + fullPath + " is not exit");
        }
    }
    /// <summary>
    /// 递归法遍历文件夹下每一个文件
    /// </summary>
    public static void ListFiles(FileSystemInfo info, string relativePath, Dictionary<string, string> theWriter)
    {
        if (!info.Exists)
        {
            Debug.LogError("info is not exit");
            return;
        }
        DirectoryInfo dir = info as DirectoryInfo;
        FileSystemInfo[] files = dir.GetFileSystemInfos();
        for (int i = 0; i < files.Length; i++)
        {
            FileInfo file = files[i] as FileInfo;
            //对文件的操作,如果是文件夹就继续递归循环,如果是文件就更变其Mark值
            if (file != null)
            {
                ChangeMark(file, relativePath, theWriter);
            }
            //对目录的操作,递归
            else
            {
                ListFiles(files[i], relativePath, theWriter);
            }
        }
    }

    /// <summary>
    /// 改变文件标记
    /// </summary>
    public static void ChangeMark(FileInfo tmpFile, string relativePath, Dictionary<string, string> theWriter)
    {
        if (tmpFile.Extension == ".meta")
        {
            return;
        }
        Debug.Log("ChangeFile>>=======================================================");
        string markStr = GetBundlePath(tmpFile, relativePath);
        Debug.Log("markStr ==" + markStr);
        ChangeAssetMark(tmpFile, markStr, theWriter);
    }

    
    /// <summary>
    /// 计算mark 标记值 
    /// </summary>
    public static string GetBundlePath(FileInfo file, string relativePath)
    {
        // D:\\XXX\\XXX此方法获得的路径斜杠方向不同
        string tmpPath = file.FullName;
              
        tmpPath = FixedWindowsPath(tmpPath);
        relativePath = FixedWindowsPath(relativePath);

        Debug.Log("fullPath ==" + tmpPath);
        Debug.Log("relativePath ==" + relativePath);
        //Assets/Art/Scenes/SceneOne/load

        //Assets/Art/Scenes/SceneOne/
        int assetCount = tmpPath.IndexOf(relativePath);
        assetCount += relativePath.Length + 1;;
        int nameCount = tmpPath.LastIndexOf(file.Name);
        int tmpLength = nameCount - assetCount;
        int tmpCount = relativePath.LastIndexOf("/");
        string sceneHead = relativePath.Substring(tmpCount + 1, relativePath.Length - tmpCount - 1);
        //sceneHead == SceneOne

        //Load\TestThree\
        if (tmpLength > 0)
        {
            //Load/TestThree/TestThree.prefab
            string subString = tmpPath.Substring(assetCount, tmpPath.Length - assetCount);
            string[] result = subString.Split("/".ToCharArray());
            //SceneOne + Load
            return sceneHead + "/" + result[0];
        }
        else
        {
            //场景文件 所标记
            return sceneHead;
        }
    }

    
    public static void ChangeAssetMark(FileInfo tmpFile, string markStr, Dictionary<string, string> theWriter)
    {
        string fullPath = tmpFile.FullName;
        int assetCount = fullPath.IndexOf("Assets");
        string assetPath = fullPath.Substring(assetCount, fullPath.Length - assetCount);
        // assets/sceneone/load/test.prefab
        AssetImporter importer = AssetImporter.GetAtPath(assetPath);

        //改变标记
        importer.assetBundleName = markStr;
        if (tmpFile.Extension == ".unity")
        {
            importer.assetBundleVariant = "u3d";
        }
        else
        {
            importer.assetBundleVariant = "ld";
        }

        //Load -- SceneOne/load
        string modleName = "";
        string[] subMark = markStr.Split("/".ToCharArray());
        if (subMark.Length > 1)
        {
            modleName = subMark[1];
        }
        else
        {
            //Load -- SceneOne
            modleName = markStr;
        }

        //sceneone/load.ld
        string modlePath = markStr.ToLower() + "." + importer.assetBundleVariant;
        if (!theWriter.ContainsKey(modleName))
            theWriter.Add(modleName, modlePath);
    }


    public static string FixedWindowsPath(string path)
    {
        path = path.Replace("\\", "/");
        return path;
    }
}


ITools/MarkAssetBundle运行结果



ITools/BuildeAssetBundle 运行结果



Record.txt内容


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本课程总体分为两大部分:理论篇与架构篇AssetBundle架构篇:     1: 讲解Unity原生AssetBundle不适用工程化实战开发的原因与解决方案。     2: 详述AssetBundle框架设计原理图,以及核心设计理念。     3: Untiy编辑器界面全自动化创建AssetBundle与打包理念与代码实现。     4: 关于单一AssetBundle包的综合加载与管理以及相应测试实现。     5: AssetBundle整体管理。          本模块包含*.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、付费专栏及课程。

余额充值