因为当前使用的是lua所以直接上lua代码了
3d音效需要设计到 listener和3d sound两部分
处理如下
首先是有一个听者
local listner_node = scene_:CreateChild("listenerNode");
local listener = listner_node :CreateComponent("SoundListener");
node.position = Vector3(100, 0, 30);
之后是一个发声者
local sound_node = scene_:CreateChild("soundNode");
local soundSource = sound_node:CreateComponent("SoundSource3D");
// 下面3个参数需要注意在设置的时候自己要测试,不同数字参数的效果
// 具体说明看文档
soundSource:SetDistanceAttenuation(10, 500, 15)
sound_node.position = Vector3(10, 0, 10);
local sound = cache:GetResource("Sound", "Sounds/river.wav");
soundSource:Play(sound );
除此之外还需要进行一些设置才可以
audio:SetListener(listener);
这里就可以听到声音了
我遇到的问题是刚开始
- audio 没有设置
- SetDistanceAttenuation没有设置正确的参数
所以就没有声音