主要用于Unity项目里删除空的Awake()、Start()、Update()、FixedUpdate()和LateUpdate()等
方法1:直接在编辑器里模式匹配查找替换,表达式:voids*Updates*?(s*?)s*?n*?{n*?s*?}
方法2:免得忘记匹配模式,写几行代码来改
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
public class HandleEmptyFunction
{
private static readonly string defaultPath = @"H:UnityProjectsTestAssetsScripts";
private static readonly string pattern = @"voids*Updates*?(s*?)s*?n*?{n*?s*?}";
private static readonly bool chooseFolder = true;
private static readonly bool removeEmptyFunction = true;
[MenuItem("MyTools/HandleEmptyFunc")]
public static void Check()
{
string selectedPath = "";
if (chooseFolder)
{
selectedPath = EditorUtility.OpenFolderPanel("Select Folder", Application.dataPath, "");
}
else
{
selectedPath = defaultPath;
}
if (selectedPath.Length == 0)
{
Debug.Log("取 消");
return;
}
DirectoryInfo directoryInfo = new DirectoryInfo(selectedPath);
//如果使用 GetFiles 出现内存不够的情况(基本不会的啦),可改用 Directory.EnumerateFiles()
string[] files = Directory.GetFiles(selectedPath, "*.cs", SearchOption.AllDirectories);
foreach (string file in files)
{
string s = File.ReadAllText(file);
Match match = Regex.Match(s, pattern);
if (match.Success)
{
Debug.Log($"{file} contains empty Update function");
if (removeEmptyFunction)
{
s = Regex.Replace(s, pattern, "");
File.WriteAllText(file, s, Encoding.UTF8);
}
}
}
}
}
方法3:Mono.Cecil读取dll来查找——Unity3D 查找Update函数体为空的类



