Unity C#-动态更改音频剪辑

时间:2018-09-23 15:27:11

标签: c# unity3d

在我的游戏中,我正在进行设置。这些设置将在以后用于更改音乐或精灵,以及保持音乐静音与否。该列表中唯一不起作用的是音乐。启动时,该游戏应该从JSON文件中读取有关设置的信息,形式为两个字符串和一个布尔值。问题来自加载用作资源的音频。我的代码如下:

    Settings data = new Settings();
    string readData = File.ReadAllText(Path.Combine(Application.persistentDataPath, "settings.json"));
    JsonUtility.FromJsonOverwrite(readData, data);
    isMusicPlaying = data.musicPlaying;
    currentMusicPiece = data.songIndex;
    Debug.Log(currentMusicPiece); //This logs exactly what I'm trying to log (song1)
    currentTileSet = data.tileset;
    this.gameObject.GetComponent<AudioSource>().clip = Resources.Load<AudioClip>(currentMusicPiece); //This line doesn't load the audio
    this.gameObject.GetComponent<AudioSource>().Play(); //This line doesn't play the audio

除了最后两行无效外,其他所有内容均有效。我已经尝试通过在加载部分的字符串中添加“ .wav”来加载“ song1.wav”,但这也不起作用。我尝试弄乱了代码试图更改的音频源(在动态和检查器窗口中)。还有什么要做?

0 个答案:

没有答案
相关问题