Skip to content

Instantly share code, notes, and snippets.

@blazejhanzel
Last active February 12, 2024 19:03
Show Gist options
  • Save blazejhanzel/70b61927dab217d18018c7dfff289cc2 to your computer and use it in GitHub Desktop.
Save blazejhanzel/70b61927dab217d18018c7dfff289cc2 to your computer and use it in GitHub Desktop.
Tool for displaying current number of lines in every script hold in "Assets/Scripts/" directory.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Tool for displaying current number of lines in every script hold in "Assets/Scripts/" directory.
/// </summary>
public class LineCounter : EditorWindow
{
[SerializeField]
private bool autoRefresh = true;
private Vector2 scrollPosition = Vector2.zero;
private List<(string, int)> filesAndLines = new();
private int totalLines;
private int totalFiles;
#region Unity events
[MenuItem("Tools/Line Counter")]
private static void ShowWindow()
{
var window = GetWindow<LineCounter>();
window.titleContent = new GUIContent("Line Counter");
window.Show();
}
private void Awake()
{
if (autoRefresh)
{
UpdateStats();
}
}
private void OnProjectChange()
{
if (autoRefresh)
{
UpdateStats();
}
}
private void OnGUI()
{
if (GUILayout.Button("Refresh"))
{
UpdateStats();
}
autoRefresh = EditorGUILayout.ToggleLeft("Auto refresh (can impact performance)", autoRefresh);
GUILayout.Label($"Total: {totalLines} lines in {totalFiles} files", new GUIStyle
{
fontStyle = FontStyle.Bold,
margin = new RectOffset(5, 5, 5, 5)
});
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition,
GUILayout.ExpandHeight(true), GUILayout.ExpandWidth(true));
foreach (var tuple in filesAndLines)
{
GUILayout.Label($"{tuple.Item1}: {tuple.Item2}");
}
EditorGUILayout.EndScrollView();
}
#endregion
private void UpdateStats()
{
var scriptFiles = new DirectoryInfo(Application.dataPath + "/Scripts")
.GetFiles("*.cs", SearchOption.AllDirectories);
filesAndLines.Clear();
totalLines = 0;
totalFiles = scriptFiles.Length;
foreach (var scriptFile in scriptFiles)
{
var numLines = File.ReadAllLines(scriptFile.FullName).Length;
filesAndLines.Add((scriptFile.Name, numLines));
totalLines += numLines;
}
filesAndLines = filesAndLines.OrderByDescending(tuple => tuple.Item2).ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment