Unity中获取整个项目的代码行数总和

507 阅读1分钟

一:使用Directory.GetFiles

using System;
using System.IO;
using UnityEngine;
using UnityEditor;

public class StatisticLine
{
    [MenuItem("输出总代码行数/输出")]
    private static void PrintTotalLine()
    {
        string[] fileName = Directory.GetFiles("Assets/Scripts", "*.cs", SearchOption.AllDirectories);

        int totalLine = 0;
        foreach (var temp in fileName)
        {
            int nowLine = 0;
            StreamReader sr = new StreamReader(temp);
            while (sr.ReadLine() != null)
            {
                nowLine++;
            }

            //文件名+文件行数
            //Debug.Log(String.Format("{0}——{1}", temp, nowLine));

            totalLine += nowLine;
        }

        Debug.Log(String.Format("总代码行数:{0}", totalLine));
    }
}

​

二:使用AssetDatabase.FindAssets

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public class StatisticLine 
{
    [MenuItem("输出总代码行数/输出")]
    private static void PrintTotalLine()
    {
        string[] fileName = AssetDatabase.FindAssets("t:Script", new string[] { "Assets/Scripts" });

        int totalLine = 0;
        foreach (var temp in fileName)
        {
            int nowLine = 0;
            string path = AssetDatabase.GUIDToAssetPath(temp);
            StreamReader sr = new StreamReader(path);
            while (sr.ReadLine() != null)
            {
                nowLine++;
            }

            //文件名+文件行数
            //Debug.Log(String.Format("{0}——{1}", path, nowLine));

            totalLine += nowLine;
        }

        //Debug.Log(String.Format("总代码行数:{0}", totalLine));
    }
}