Hello.
How to read (or scan) the resources folder in order to check if there is a new object added to the game (for example a new MP3 file as a music), and if it is added then lets say the game automatically plays it?
Hello.
How to read (or scan) the resources folder in order to check if there is a new object added to the game (for example a new MP3 file as a music), and if it is added then lets say the game automatically plays it?
well lets see, you could setup a repeating function to check the folder. I would have 2 lists<> one list would be the content on the folder from the last time it checked, and the second list would the active check. if the folder has something new then run some other function to check if it was a .mp3. if it was then send it to the audio source in unity.
but this leave out all the details of how to check the folder and how often to check it. does it need to check the date and time of when the file was added?
You need to give a little more info. do you have any of this working yet? do you have unity playing sound files yet?
these are some simple code snip its for checking folders and file I use. but you would need some sort of foreach loop.
if (!Directory.Exists(Application.dataPath + “/Resources/music/”))
{
Directory.CreateDirectory(Application.dataPath + “/Resources/music/”);
}
if (File.Exists(Application.dataPath + “/Resources/music/music.mp3”))
{
do something…
}
Okay, first of all thanks for the reply. And let me explain more.
I actually need such script to actually check new contents (or mods) for my game. this could be a new mp3 file a new car mod or something else.
And yes, I did made a script to play music and I even made a small media player everything is fine here except I don’t know how to make my game to search for new mods (or contents) when the game first starts.
I guess I explained clearly but please if you need more info ask me.
Ok, that is much easier to get working.
using UnityEngine;
using System.Collections;
using System.IO;
public class ReadFolder : MonoBehaviour {
public string myPath;
// Use this for initialization
void Start () {
DirectoryInfo dir = new DirectoryInfo(Application.dataPath + myPath);
FileInfo[] info = dir.GetFiles("*.*");
foreach (FileInfo f in info)
{
print(f.Name);
}
}
}
Aaa thank you!!, So if I replace the “.” to for example “*.mp3” it gets all the music files so that I can mess around with them in the foreach loop, that is a good way thanks.
yep, you got it. change it to .mp3 and then load then in a list<>, do what ever you want.