unity how to tell how many objets are in a folder code example

Example: unity how to tell how many objets are in a folder

void Start()
    {
        //I needed to get a count of my all level prefabs from my
        //Resources folder so I hashed together this frankenstein code:

        //Get the path of the Levels folder from Resources
        string AssetsFolderPath = Application.dataPath;
        string levelFolder = AssetsFolderPath + "/Resources/Levels";

        Debug.Log("dataPath : " + levelFolder);

        //////////////////////

        //DirectoryInfo needs "using System.IO"
        DirectoryInfo dir = new DirectoryInfo(levelFolder);
        FileInfo[] info = dir.GetFiles("*.prefab");
        int fileCount = info.Length;

        Debug.Log("Level Count: " + fileCount);

        //Clear array to save space, needs "using System"
        Array.Clear(info, 0, info.Length);
    }

Tags:

Misc Example