Skip to content

Instantly share code, notes, and snippets.

@talecrafter
Created February 15, 2017 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talecrafter/71fcaa222b986cd2bd40d2eaf6dec366 to your computer and use it in GitHub Desktop.
Save talecrafter/71fcaa222b986cd2bd40d2eaf6dec366 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public static class AssetFilesCounter
{
[MenuItem("Utilities/Count Asset Files")]
public static void CountAssetFiles()
{
EditorUtility.DisplayProgressBar("Counting Asset Files", "", 0);
try
{
// using our own counting here, AssetDatabase.GetAllAssetPaths() would include directories
int assetFilesCount = GetAllFilesExcludingExtensions(Application.dataPath, "*.*", "meta").Length;
Debug.Log("Asset Files: " + assetFilesCount);
}
finally
{
EditorUtility.ClearProgressBar();
}
}
public static string[] GetAllFilesExcludingExtensions(string path, string searchPattern, params string[] extensions)
{
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException("Directory " + path + " not found.");
}
// make sure extension has the right format
for (int i = 0; i < extensions.Length; i++)
{
if (!extensions[i].StartsWith("."))
{
extensions[i] = "." + extensions[i];
}
}
var allFiles = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
List<string> validFiles = new List<string>();
foreach (var file in allFiles)
{
if (!PathHasExtension(file, extensions))
{
validFiles.Add(file);
}
}
return validFiles.ToArray();
}
public static bool PathHasExtension(string path, params string[] extensions)
{
for (int i = 0; i < extensions.Length; i++)
{
if (path.EndsWith(extensions[i]))
{
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment