Advertisement
Zidinjo

Load soundfiles from directory

Oct 21st, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System.IO;
  2.  
  3. public class LoadFiles : MonoBehaviour {
  4.  
  5.     // Use this for initialization
  6.     void Start () {
  7.         Debug.Log ((RestoreSoundFiles (SOUND_PATH)).Length);
  8.     }
  9.  
  10.     /// <summary>
  11.     /// final soundpath of the assetsfolder
  12.     /// </summary>
  13.     public readonly string SOUND_PATH =  "/_sounds";
  14.  
  15.     /// <summary>
  16.     /// The file extension.
  17.     /// </summary>
  18.     private readonly string fileExtension = "*.mp3";
  19.  
  20.     /// <summary>
  21.     /// Restores the sound files from the given filePath
  22.     /// </summary>
  23.     /// <returns>The sound files.</returns>
  24.     /// <param name="filePath">File path.</param>
  25.     private AudioClip[] RestoreSoundFiles(string filePath)
  26.     {
  27.         //Declare the direction of the passed filepath
  28.         DirectoryInfo dir = new DirectoryInfo(Assets.ASSETS_FOLDER + filePath);
  29.  
  30.         //Get all files from folder - files will be filtered by the filExtension
  31.         FileInfo[] info = dir.GetFiles(fileExtension);
  32.  
  33.         //Declare a array of audioclips with the given length of the info array
  34.         AudioClip[] soundClips = new AudioClip[info.Length];
  35.         for (int i = 0; i < soundClips.Length; i++)
  36.             soundClips[i] = (AudioClip)Resources.Load(info[i].FullName, typeof(AudioClip));
  37.         return soundClips;
  38.     }
  39. }
  40.  
  41. ///utilities
  42. using UnityEngine;
  43. using System.Collections;
  44. using System.IO;
  45.  
  46. public class Assets {
  47.     public static readonly string ASSETS_FOLDER = Application.dataPath;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement