栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

查找空函数体

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

查找空函数体

主要用于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函数体为空的类

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901463.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号