Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
3
Question by crasyboy42 · Sep 04, 2010 at 03:32 PM · convertdirectoryreadmp3datapath

How can unity read a directory?

Hallo

I am making a sound list where the player of the game can put his/her own music in.
But for that unity must read a directory outside the _data folder.
How can unity do that?

Does anybody know the answer?

I have something like it but it only reads the string.

var path = Application.dataPath+"/music/";
function Update ()
{
var files = (path+"*.mp3");
print(files);
}
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

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Ony · Sep 05, 2010 at 12:46 AM

Try this:

import System.IO;

var filesList = new Array();

function ReadDirectory() {

 var path = Application.dataPath+"\\music\\";    
 var info = new DirectoryInfo(path);
 var fileInfo = info.GetFiles();

 for (file in fileInfo)
 {

     var filename = file.ToString();    
     filename = filename.Replace(Application.dataPath+"\\music\\", "");          
     filesList.Push(filename);

 }

 print(filesList);

}

Comment
Add comment · Show 4 · 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 crasyboy42 · Sep 05, 2010 at 10:06 AM 0
Share

i get this error: Assets/music player!.js(14,9): BCE0005: $$anonymous$$ identifier: 'filesList'. do you mean with filelist file name?

avatar image Ony · Sep 05, 2010 at 04:44 PM 0
Share

filesList is an array. I figure you have some array you want to add the filenames to and I just chose filesList as the name out of the blue. I've edited my code to add the declaration for the array.

avatar image crasyboy42 · Sep 06, 2010 at 05:39 AM 0
Share

you helpt me a lot but how do i play the music?? i know how to do that if the .mp3 files are in unity but how to do that with this script?

avatar image Ony · Sep 06, 2010 at 05:08 PM 1
Share

That's a totally different question you'll want to ask in a different post as you're more likely to get an answer that way. $$anonymous$$y code is simply to read a directory, but playing the music files in it is a whole other thing. If my answer helped it would be great if you checked it as the correct answer to this particular question.

avatar image
1

Answer by Cyclops · Sep 04, 2010 at 06:00 PM

Here's another example, Desktop game?, showing how to read a file. Note that only standalone executables can read files on the player's machine. The web-browser version can not, for security reasons.

Unity will not automatically expand wildcards for you, like "*.mp3", that is something you need to using .Net directory functions.

Also - the Update() function runs every frame - you probably want to move your file-reading to a function that runs once, called either in Start(), or via button click or something.

On second reading - it's not clear from your question, if you mean to read files on the user's machine, or if you're allowing the user to upload files to your server. Can you clarify what you're asking for?

Comment
Add comment · Show 2 · 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 crasyboy42 · Sep 05, 2010 at 09:43 AM 0
Share

i just wanna use the .mp3 files as music :) nothing else

avatar image crasyboy42 · Sep 05, 2010 at 10:23 AM 0
Share

it is for a standalone game

avatar image
0

Answer by 3px · Sep 04, 2010 at 05:46 PM

Use the System.IO library from .NET

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Is it possible to choose your own save location? The Filestream File.Create Directory. 1 Answer

Read text file from other directory 1 Answer

Getting Directory not Found error with Binary Formatting attempt 2 Answers

convert wav to compressed format like ogg 1 Answer

Unity doesn't detect txt file after build 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges