一:使用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++;
}
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++;
}
totalLine += nowLine;
}
}
}