如何发挥装在飞行的声音 [英] How to play sounds loaded on the fly

查看:117
本文介绍了如何发挥装在飞行的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的小组的音响设计师,将让他听到他在Unity播放的声音文件的工具。

I trying to make a tool for the sound designer of my group that would allow him to hear his sound file played in Unity.


  • 检查,如果它是可装载

  • 检查音量是正确

  • 检查循环正常

等等...

我的问题是要找到团结如何管理加载音频文件的文件夹在什么地方铺设。

My issue is to find how Unity can manage loading audio files laying somewhere in a folder.

我发现了很多的话题谈论,但没有真正的解决方案,你如何能够团结动态加载外部文件。

I found a lot of topics speaking about it but no real solutions to how you can make Unity load external files dynamically.

推荐答案

如果您想从同一目录加载文件作为.EXE /的.app <​​/ STRONG>您可以使用此:

If you want to load files from the same directory as the .exe / .app you can use this :


  • 使用System.IO DirectoryInfo的(),以获取所有的文件名

  • 使用 WWW类以流/ LOAD中找到的文件

  • Using the System.IO DirectoryInfo() to get all files names
  • Using the WWW class to stream/load the files found

在这里,code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class SoundPlayer : MonoBehaviour {

    string absolutePath = "./"; // relative path to where the app is running

    AudioSource src;
    List<AudioClip> clips = new List<AudioClip>();
    int soundIndex = 0;

    //compatible file extensions
    string[] fileTypes = {"ogg","wav"};

    FileInfo[] files;

    void Start () {
        //being able to test in unity
        if(Application.isEditor)    absolutePath = "Assets/";
        if(src == null) src = gameObject.AddComponent<AudioSource>();
        reloadSounds();
    }

    void reloadSounds() {
        DirectoryInfo info = new DirectoryInfo(absolutePath);
        files = info.GetFiles();

        //check if the file is valid and load it
        foreach(FileInfo f in files) {
            if(validFileType(f.FullName)) {
                //Debug.Log("Start loading "+f.FullName);
                StartCoroutine(loadFile(f.FullName));
            }
        }
    }

    bool validFileType(string filename) {
        foreach(string ext in fileTypes) {
            if(filename.IndexOf(ext) > -1) return true;
        }
        return false;
    }

    IEnumerator loadFile(string path) {
        WWW www = new WWW("file://"+path);

        AudioClip myAudioClip = www.audioClip;
        while (!myAudioClip.isReadyToPlay)
        yield return www;

        AudioClip clip = www.GetAudioClip(false);
        string[] parts = path.Split('\\');
        clip.name = parts[parts.Length - 1];
        clips.Add(clip);
    }
}

如果人们想改善文件管理,我建议此链接

If people wants to improve on the file management I recommend this link

这篇关于如何发挥装在飞行的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆