CountLines

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
(Added a EditorWindow derived version of the original script. Updated Usage.)
 
Line 9: Line 9:
 
Place it inside the Editor folder. Then, you can click "Custom/Stats/Count Lines"
 
Place it inside the Editor folder. Then, you can click "Custom/Stats/Count Lines"
  
Note: this was developed to be used on Unity iPhone. So the GUI is very basic (just a DisplayDialog).  
+
== C# - CountLines.cs ==
 
+
EditorWindow-derived version for Unity 3.5.
With Unity 2.5 out, it would be easy to add more advanced looking windows...
+
<syntaxhighlight lang="csharp">
 +
using UnityEngine;
 +
using UnityEditor;
 +
using System.Collections;
 +
 +
public class CountLines: EditorWindow
 +
{
 +
System.Text.StringBuilder strStats;
 +
Vector2 scrollPosition = new Vector2(0,0);
 +
struct File
 +
{
 +
public string name;
 +
public int nbLines;
 +
 +
public File(string name, int nbLines)
 +
{
 +
this.name = name;
 +
this.nbLines = nbLines;
 +
}
 +
}
 +
 +
void OnGUI()
 +
{
 +
if (GUILayout.Button("Refresh"))
 +
{
 +
DoCountLines();
 +
}
 +
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
 +
EditorGUILayout.HelpBox(strStats.ToString(),MessageType.None);
 +
EditorGUILayout.EndScrollView();
 +
}
 +
 +
 +
[MenuItem("Custom/Stats/Count Lines")]
 +
public static void Init()
 +
{
 +
CountLines window = EditorWindow.GetWindow<CountLines>("Count Lines");
 +
window.Show();
 +
window.Focus();
 +
window.DoCountLines();
 +
}
 +
 +
void DoCountLines()
 +
{
 +
string strDir = System.IO.Directory.GetCurrentDirectory();
 +
strDir += @"/Assets";
 +
int iLengthOfRootPath = strDir.Length;
 +
ArrayList stats = new ArrayList();
 +
ProcessDirectory(stats, strDir);
 +
 +
int iTotalNbLines = 0;
 +
foreach(File f in stats)
 +
{
 +
iTotalNbLines += f.nbLines;
 +
}
 +
 +
strStats = new System.Text.StringBuilder();
 +
strStats.Append("Number of Files: " + stats.Count + "\n");
 +
strStats.Append("Number of Lines: " + iTotalNbLines + "\n");
 +
strStats.Append("================\n");
 +
 +
foreach(File f in stats)
 +
{
 +
strStats.Append(f.name.Substring(iLengthOfRootPath+1, f.name.Length-iLengthOfRootPath-1) + " --> " + f.nbLines + "\n");
 +
}
 +
}
 +
 +
static void ProcessDirectory(ArrayList stats, string dir)
 +
{
 +
        string[] strArrFiles = System.IO.Directory.GetFiles(dir, "*.cs");
 +
        foreach (string strFileName in strArrFiles)
 +
            ProcessFile(stats, strFileName);
 +
 +
        strArrFiles = System.IO.Directory.GetFiles(dir, "*.js");
 +
        foreach (string strFileName in strArrFiles)
 +
            ProcessFile(stats, strFileName);
 +
 +
        string[] strArrSubDir = System.IO.Directory.GetDirectories(dir);
 +
        foreach (string strSubDir in strArrSubDir)
 +
            ProcessDirectory(stats, strSubDir);
 +
}
 +
 +
static void ProcessFile(ArrayList stats, string filename)
 +
{
 +
        System.IO.StreamReader reader = System.IO.File.OpenText(filename);
 +
        int iLineCount = 0;
 +
        while (reader.Peek() >= 0)
 +
        {
 +
            reader.ReadLine();
 +
            ++iLineCount;
 +
        }
 +
stats.Add(new File(filename, iLineCount));
 +
        reader.Close();
 +
}
 +
}
 +
</syntaxhighlight>
  
 
== C# - CountLines.cs ==
 
== C# - CountLines.cs ==
 
+
Original version for Unity 2.5. Uses a DisplayDialog.
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;

Latest revision as of 19:28, 27 October 2012

Author: Scrat

Contents

[edit] Description

Count all the files and lines in your project.

[edit] Usage

Place it inside the Editor folder. Then, you can click "Custom/Stats/Count Lines"

[edit] C# - CountLines.cs

EditorWindow-derived version for Unity 3.5.

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class CountLines: EditorWindow 
{
	System.Text.StringBuilder strStats;
	Vector2 scrollPosition = new Vector2(0,0);
	struct File
	{
		public string 	name;
		public int 		nbLines;
 
		public File(string name, int nbLines)
		{
			this.name 		= name;
			this.nbLines 	= nbLines;
		}
	}	
 
	void OnGUI()
	{
		if (GUILayout.Button("Refresh"))
		{
			DoCountLines();
		}
		scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
		EditorGUILayout.HelpBox(strStats.ToString(),MessageType.None);
		EditorGUILayout.EndScrollView();
	}
 
 
	[MenuItem("Custom/Stats/Count Lines")]
	public static void Init()
	{
		CountLines window = EditorWindow.GetWindow<CountLines>("Count Lines");
		window.Show();
		window.Focus();
		window.DoCountLines();
	}
 
	void DoCountLines()
	{		
		string strDir = System.IO.Directory.GetCurrentDirectory();
		strDir += @"/Assets";
		int iLengthOfRootPath = strDir.Length;
		ArrayList stats = new ArrayList();	
		ProcessDirectory(stats, strDir);	
 
		int iTotalNbLines = 0;
		foreach(File f in stats)
		{
			iTotalNbLines += f.nbLines;
		}
 
		strStats = new System.Text.StringBuilder();
		strStats.Append("Number of Files: " + stats.Count + "\n");		
		strStats.Append("Number of Lines: " + iTotalNbLines + "\n");	
		strStats.Append("================\n");	
 
		foreach(File f in stats)
		{
			strStats.Append(f.name.Substring(iLengthOfRootPath+1, f.name.Length-iLengthOfRootPath-1) + " --> " + f.nbLines + "\n");
		}		
	}
 
	static void ProcessDirectory(ArrayList stats, string dir)
	{	
        string[] strArrFiles = System.IO.Directory.GetFiles(dir, "*.cs");
        foreach (string strFileName in strArrFiles)
            ProcessFile(stats, strFileName);
 
        strArrFiles = System.IO.Directory.GetFiles(dir, "*.js");
        foreach (string strFileName in strArrFiles)
            ProcessFile(stats, strFileName);
 
        string[] strArrSubDir = System.IO.Directory.GetDirectories(dir);
        foreach (string strSubDir in strArrSubDir)
            ProcessDirectory(stats, strSubDir);
	}
 
	static void ProcessFile(ArrayList stats, string filename)
	{
        System.IO.StreamReader reader = System.IO.File.OpenText(filename);
        int iLineCount = 0;
        while (reader.Peek() >= 0)
        {
            reader.ReadLine();
            ++iLineCount;
        }
		stats.Add(new File(filename, iLineCount));
        reader.Close();			
	}	
}

[edit] C# - CountLines.cs

Original version for Unity 2.5. Uses a DisplayDialog.

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class CountLines: Editor{
 
	struct File
	{
		public string 	name;
		public int 		nbLines;
 
		public File(string name, int nbLines)
		{
			this.name 		= name;
			this.nbLines 	= nbLines;
		}
	}	
 
	[MenuItem("Custom/Stats/Count Lines")]
	static void CountLines()
	{		
		string strDir = System.IO.Directory.GetCurrentDirectory();
		strDir += @"/Assets";
		int iLengthOfRootPath = strDir.Length;
		ArrayList stats = new ArrayList();	
		ProcessDirectory(stats, strDir);	
 
		int iTotalNbLines = 0;
		foreach(File f in stats)
		{
			iTotalNbLines += f.nbLines;
		}
 
		System.Text.StringBuilder strStats = new System.Text.StringBuilder();
		strStats.Append("Number of Files: " + stats.Count + "\n");		
		strStats.Append("Number of Lines: " + iTotalNbLines + "\n");	
		strStats.Append("================\n");	
 
		foreach(File f in stats)
		{
			strStats.Append(f.name.Substring(iLengthOfRootPath+1, f.name.Length-iLengthOfRootPath-1) + " --> " + f.nbLines + "\n");
		}		
 
		EditorUtility.DisplayDialog("Statistics", strStats.ToString(), "Ok");
	}
 
	static void ProcessDirectory(ArrayList stats, string dir)
	{	
        string[] strArrFiles = System.IO.Directory.GetFiles(dir, "*.cs");
        foreach (string strFileName in strArrFiles)
            ProcessFile(stats, strFileName);
 
        strArrFiles = System.IO.Directory.GetFiles(dir, "*.js");
        foreach (string strFileName in strArrFiles)
            ProcessFile(stats, strFileName);
 
        string[] strArrSubDir = System.IO.Directory.GetDirectories(dir);
        foreach (string strSubDir in strArrSubDir)
            ProcessDirectory(stats, strSubDir);
	}
 
	static void ProcessFile(ArrayList stats, string filename)
	{
        System.IO.StreamReader reader = System.IO.File.OpenText(filename);
        int iLineCount = 0;
        while (reader.Peek() >= 0)
        {
            reader.ReadLine();
            ++iLineCount;
        }
		stats.Add(new File(filename, iLineCount));
        reader.Close();			
	}	
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox