目录
Unity 进阶 之 资源文件夹下资源名的重名检查,并简单生产资源表的方法整理
一、简单介绍
二、简单实现过程
三、关键代码
Unity中的一些知识点整理。
本节简单介绍在Unity开发中的,在资源管理的时候,为了更好的管理资源,在大的资源文件夹下,可能并不希望不同文件夹下有重名的资源名,这里代码自动检查,并且给出重名提示,最后生成一个资源表,如果你有新的方式也可以留言,多谢。
1、新建一个Unity工程,创建资源文件夹Res 并把相关资源添加到Res 文件夹下
2、在 Editor 文件夹下编写资源检查脚本
3、菜单栏出现资源检查的工具菜单
4、点击执行 Check Res And Generate MapFile,控制台给出重名提示,并且生成一个简单资源表
5、为了避免忘记做资源检查和表生成,每次在Build 的时候,自动做一次资源检查和生成工作,代码如下
6、每次Build的时候就会自动做一次资源检查和生成工作
1、CheckResAndGenertateResConfig
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
///
/// 将Res文件夹中所有的资源 以 名称(如img 无扩展)=路径(Res中的路径如:a/img 无扩展名)的实行导出txt
/// 如果名称重复,则爆出出重复路径, 重复的名称以路径代替,则无法使用名称来映射路径(建议不要使用相同的名称命名资源)
///
public class CheckResAndGenertateResConfig : Editor
{[MenuItem("Tools/Res/ Check Res And Generate MapFile")]public static void Generate(){string targetDir = "Assets/StreamingAssets";string targetFile = targetDir+ "/ResMap.txt";if (!Directory.Exists(targetDir)){Directory.CreateDirectory(targetDir);}string resourcesPath = Application.dataPath + "/Res";//resources文件文件的路径if (!Directory.Exists(resourcesPath)){Debug.Log($"不存在路径{resourcesPath}");if (File.Exists(targetFile)){File.Delete(targetFile);}return;}Dictionary> dic = new Dictionary>();DirectoryList(resourcesPath, dic);//已经获得了Resources文件夹中所有的资源,开始操作if (dic.Count == 0){if (File.Exists(targetFile)){File.Delete(targetFile);}}else{//更新之前的表List info = new List();List result;string warn = "";foreach (string key in new List(dic.Keys)){result = dic[key];if (result.Count == 1){info.Add($"{key}={result[0]}");continue;}foreach (string value in result)//如果存在相同的文件名称在不同的路径当中{info.Add($"{value}={value}");if (warn == string.Empty){warn += value;}else{warn += " ---- " + value;}}}if (warn != string.Empty){warn = "Assets/Res中存在相同名称的资源,这些资源将可能使用路径来获取 : \n" + warn;Debug.LogWarning(warn);}File.WriteAllLines(targetFile, info.ToArray());}AssetDatabase.Refresh();//刷新}private static void GetFiles(string path, Dictionary> dic){DirectoryInfo theFolder = new DirectoryInfo(path);string name;string filePath;string suffix;foreach (FileInfo file in theFolder.GetFiles()){name = file.Name;suffix = name.Substring(name.LastIndexOf("."));if (name.Length > 5 && name.Substring(name.Length - 5) == ".meta") continue;//过滤掉Unity的.meta配置文件filePath = file.FullName;name = Path.GetFileNameWithoutExtension(filePath);filePath = filePath.Substring(filePath.IndexOf(@"Assets\Res\") + @"Assets\Res\".Length);filePath = filePath.Replace(@"\", @"/").Replace(suffix, string.Empty);if (dic.ContainsKey(name)){dic[name].Add(filePath);}else{dic.Add(name, new List() { filePath });}}}private static void DirectoryList(string path, Dictionary> dic){DirectoryInfo theFolder = new DirectoryInfo(path);//遍历文件GetFiles(path, dic);foreach (DirectoryInfo directory in theFolder.GetDirectories()){DirectoryList(directory.FullName, dic);}}
}
2、CheckResPreBuildHandler
using UnityEditor.Build;
#if UNITY_EDITORpublic class CheckResPreBuildHandler : IPreprocessBuildWithReport
{public int callbackOrder => 0;//回调顺序public void OnPreprocessBuild(UnityEditor.Build.Reporting.BuildReport report){CheckResAndGenertateResConfig.Generate();//每次在打包前,处理Resources文件夹中的资源映射}
}
#endif
上一篇:数据结构---set篇
下一篇:C++引用