Example #1
0
    /// <summary>
    /// Get an AudioClip at the given full path. Attempts to retrieve it from the AudioClipRegistry first by using folderRoot to extract the clip's name, otherwise attempts to load from disk.
    /// </summary>
    /// <param name="musicFilePath">Full path to a file.</param>
    /// <returns>AudioClip object on successful load, otherwise null.</returns>
    public static AudioClip getAudioClip(string folderRoot, string musicFilePath)
    {
        string clipName = FileLoader.getRelativePathWithoutExtension(folderRoot, musicFilePath);
        //AudioClip music = AudioClipRegistry.Get(clipName);
        //if (music != null)
        //    return music;
        WWW www = new WWW(new Uri(musicFilePath).AbsoluteUri.Replace("+", "%2B"));

        while (!www.isDone)
        {
        }                       // hold up a bit while it's loading; delay isn't noticeable and loading will fail otherwise
        AudioType type = AudioType.UNKNOWN;

        if (musicFilePath.EndsWith(".ogg"))
        {
            type = AudioType.OGGVORBIS;
        }
        else if (musicFilePath.EndsWith(".wav"))
        {
            type = AudioType.WAV;
        }
        else
        {
            return(null);
        }

        AudioClip music;

        music      = www.GetAudioClip(false, false, type);
        music.name = "File at " + musicFilePath;
        music.LoadAudioData();

        AudioClipRegistry.Set(clipName, music);
        return(music);
    }