Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Load from folder

Discussion in 'Scripting' started by imDanOush, May 30, 2016.

  1. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    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?
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    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...
    }
     
  3. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    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.
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Ok, that is much easier to get working.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4.  
    5. public class ReadFolder : MonoBehaviour {
    6.  
    7.     public string myPath;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.        
    12.         DirectoryInfo dir = new DirectoryInfo(Application.dataPath + myPath);
    13.         FileInfo[] info = dir.GetFiles("*.*");
    14.         foreach (FileInfo f in info)
    15.         {
    16.             print(f.Name);
    17.         }
    18.  
    19.     }
    20. }
     
    tudta, Gekigengar and imDanOush like this.
  5. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    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.
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    yep, you got it. change it to .mp3 and then load then in a list<>, do what ever you want.