• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nezabyte · May 31, 2014 at 12:44 PM · recursionfolders

Recursive search project folder hierarchy?

I want to instantiate a prefab from a particular project folder with subfolders. Is it possible to do a recursive search so that it checks the parent folder and any child folders? Trying to find something in the API that will give me access to the folder structure.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by neoRiley · Jun 10, 2015 at 09:02 PM

I know this is over a year old question, but thought I'd answer for archive sake.

You could use Directory.GetFiles() with the SearchOption.AllDirectories and have it return a list of all files with filter. Here's a snip I used for finding all audio files I wanted that existed in Resources:

 // imports...(I hate it when people don't post imports)
 using UnityEngine;
 using UnityEditor;
 using UnityEditorInternal;
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using UnityEngine.Audio;
 
 // ... later in code
 protected List<string> GetFiles()
     {
         List<string> fileList = new List<string>();
 
         try
         {
             string path = Application.dataPath;
 
             IEnumerable<string> files = Directory.GetFiles(path + "/Resources", "*.*", SearchOption.AllDirectories)
                 .Where(s => s.EndsWith(".mp3") || s.EndsWith(".wav") || s.EndsWith(".aif") || s.EndsWith(".ogg"));
 
             foreach (string f in files)
             {
                 fileList.Add(f);
             }
 
 
             return fileList;
         }
         catch (UnauthorizedAccessException UAEx)
         {
             Console.WriteLine(UAEx.Message);
         }
         catch (PathTooLongException PathEx)
         {
             Console.WriteLine(PathEx.Message);
         }
 
         return null;
     }

Hope that helps,

John

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Dec 22, 2016 at 09:01 PM 0
Share

Thank you, exactly what i needed. And extra + for neat linq

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Call a function from within itself 3 Answers

Hot to generate recursive shapes like geokone.net using GL.Begin? 0 Answers

Correct Folder for Resources.Load() ? 1 Answer

for wat folder save the input manager ? 1 Answer

Longest Common Subsequence Recursive Function Error. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges