音频源 play/PlayOneShot 不工作
AudioSource play/PlayOneShot not working
我在 OnStart 中有这段代码
void Start () {
Camera.main.GetComponent<AudioListener> ().enabled = false;
GameObject sfx_go = new GameObject();
sfx_go.name = "sfx_go";
sfx_player = sfx_go.AddComponent<AudioSource> ();
sfx_go.AddComponent<AudioListener> ();
clip = (AudioClip)Resources.Load("Sounds/wind", typeof(AudioClip));
sfx_player.clip = clip;
}
然后
void Update () {
if (Input.GetMouseButtonDown (0)) {
//TODO play sound
sfx_player.PlayOneShot(clip, 0.7F);
}
我的文件是一个 wav 文件并且可以统一播放,但不是来自代码。这里有什么问题?看起来 AudioClip 在 unity editor 中是 Nothing
谢谢
您的声音文件需要位于资产文件夹内名为 "Resources" 的文件夹中。目前,Unity 正在寻找 "Assets/Resources/Sounds/wind" 作为文件的文件路径。如果您的文件只是在 "Assets/Sounds/wind" 中,那么它在错误的位置。
我在 OnStart 中有这段代码
void Start () {
Camera.main.GetComponent<AudioListener> ().enabled = false;
GameObject sfx_go = new GameObject();
sfx_go.name = "sfx_go";
sfx_player = sfx_go.AddComponent<AudioSource> ();
sfx_go.AddComponent<AudioListener> ();
clip = (AudioClip)Resources.Load("Sounds/wind", typeof(AudioClip));
sfx_player.clip = clip;
}
然后
void Update () {
if (Input.GetMouseButtonDown (0)) {
//TODO play sound
sfx_player.PlayOneShot(clip, 0.7F);
}
我的文件是一个 wav 文件并且可以统一播放,但不是来自代码。这里有什么问题?看起来 AudioClip 在 unity editor 中是 Nothing
谢谢
您的声音文件需要位于资产文件夹内名为 "Resources" 的文件夹中。目前,Unity 正在寻找 "Assets/Resources/Sounds/wind" 作为文件的文件路径。如果您的文件只是在 "Assets/Sounds/wind" 中,那么它在错误的位置。