Unity 检查文件命名是否规范
问题由来
- 非程序人员经常上传一些命名不规范的文件到Unity中
解决方式
- 命名不规范文件不能上传到库中(以后做)
- Unity加载到命名不规范文件的时候弹框提示(以后做)
- 定期扫描项目中所有文件
如果扫描项目中所有文件
- 技术点
- 如何获取Unity Asset 中所有文件命名
- Application.dataPath API可获取本地Asset目录
- DirectoryInfo 目录信息类 可获取该目录下所有目录DirectoryInfo、文件FileSystemInfo
- 使用迭代方式获取Asset目录所有文件
- 如何判断命名是否规范
- 规范定义
- 遍历文件命名字符串每个char是否符合规范
使用方式
- 把源码放入工程
- 在Unity顶部的菜单栏点击 Tool->文件命名是否规范
- 查看Console面版日志
源码
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class Folders
{
[MenuItem("Tools/文件命名是否规范")]
static void CheckFolderName()
{
Folders folder = new Folders(Application.dataPath);
List<string> paths = new List<string>();
CheckFolderName(folder, paths);
foreach (var item in paths)
{
Debug.LogError(item);
}
Debug.Log("检查完成");
}
static void CheckFolderName(Folders folder, List<string> paths)
{
foreach (var item in folder.dicFileSystemInfo)
{
string name = item.Key;
FileSystemInfo fileSystemInfo = item.Value;
if (fileSystemInfo.Extension == ".meta" || fileSystemInfo.Extension == ".DS_Store")
{
continue;
}
bool isIllegal = IsIllegal(name, true, false);
if (isIllegal == false)
{
paths.Add(fileSystemInfo.FullName);
}
}
foreach (var item in folder.listFolder)
{
CheckFolderName(item, paths);
}
}
static bool IsIllegal(string str, bool isDigit = true, bool isSpace = true)
{
for (int i = 0; i < str.Length; i++)
{
char c = str[i];
if (char.IsLower(c) || char.IsUpper(c))
{
return true;
}
if (char.IsDigit(c) && isDigit == true)
{
return true;
}
if (char.IsWhiteSpace(c) && isSpace == true)
{
return true;
}
}
return false;
}
public DirectoryInfo currentDirectoryInfo;
public List<Folders> listFolder;
public List<FileInfo> listFileInfo;
public Dictionary<string, FileSystemInfo> dicFileSystemInfo;
public Folders(string path)
{
currentDirectoryInfo = new DirectoryInfo(path);
Init();
}
Folders(DirectoryInfo directoryInfo)
{
currentDirectoryInfo = directoryInfo;
Init();
}
void Init()
{
listFileInfo = new List<FileInfo>();
foreach (var item in currentDirectoryInfo.GetFiles())
{
listFileInfo.Add(item);
}
listFolder = new List<Folders>();
foreach (var item in currentDirectoryInfo.GetDirectories())
{
listFolder.Add(new Folders(item));
}
dicFileSystemInfo = new Dictionary<string, FileSystemInfo>();
foreach (var item in listFileInfo)
{
dicFileSystemInfo.Add(item.Name, item);
}
foreach (var item in listFolder)
{
dicFileSystemInfo.Add(item.currentDirectoryInfo.Name, item.currentDirectoryInfo);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本