使用 Wwise Unity 集成

该集成提供了一些组件,这些组件可以直接满足最常见的应用场景下的使用需求,而不需要编程。

参见:

Wwise Types

这个集成还提供了一些类,适用于常见场景以外的大多数应用场景,需要极少量的编程:

参见:

Wwise Authoring API (WAAPI) client

A native WAAPI client with a C# API allows you to connect to WAAPI from within Unity. It currently is available for Windows and macOS. The Wwise Authoring API sends messages via JSON objects. In Unity, the client was implemented using strings. You may use your preferred method to construct valid JSON strings to then give to the WAAPI client.

参见:

如何将 Wwise 声音添加到游戏对象上

共有四种方法可以将声音添加到您的游戏中:

如何与 inspector 一起使用 AkAmbient

Using Wwise with Unity Timeline

For Unity's Timeline feature, there are custom Wwise tracks for triggering Wwise events and setting Wwise RTPC values.

参见:

Using the Unity WAAPI client

The Unity integration includes a simple WAAPI client that can be used to interface with the Wwise Authoring tool.

参见:

如何通过 inspector来使用 AkEvironment 和 AkEvironmentPortal(混响区域)

在 Wwise 中,Reverb Zone(混响区域)被称为 Environment(环境)或者 Auxiliary Sends(辅助发送)。Reverb Zone 并不局限于混响效果,而且它是在 Wwise 工程中定义的。

AkEnvironment 组件包含一个非常简单的环境区域。您可以在任何类型的碰撞体上附加一个 AkEnvironment。如果想将一个 AkEnvironment 添加到您的场景:

我们还有一些门户(portal)可以用来合并两个环境的影响。这两个环境中每个环境对效果的贡献都是和它们与游戏对象间距离相关的。
如果游戏对象站在两个房间之间,或者站在连接两个环境的隧道中,那么这种功能是很有用的。

如果想使用环境和环境门户,您需要一个游戏对象和能觉察到环境的(environment aware) AkGameObj 组件。
AkEnvironmentPortal 会自动探测到和它重叠的 AkEnvironment 对象。重叠的环境将出现在门户 inspector 的两个选择列表中。如果有太多的环境与门户重叠, 您则可以选择让门户和哪些环境混合在一起。

在 Wwise 中,只有 4 个环境可以同时处于活跃状态。这样 4 个环境按照如下方式选择:

参见:

使用 C# 代码来控制声音引擎

大多数 Wwise SDK 函数可以通过 AkSoundEngine 类在 Unity 中使用。将它想象为 C++ 命名空间 AK::SoundEngine、AK::MusicEngine 等等的替代。想了解与原始 SDK 相比,API 绑定中所做的更改,请参考 API 变化和限制。 对于更复杂的情况,您需要从代码中调用 Wwise 函数。在 API 中,所有函数中的 GameObjectID 被Unity 风格的 GameObject 所替代。在运行时,这个 GameObject 中将自动添加一个 AkGameObj 组件,除非您之前已经手动添加过了。

对于 Event 和 Bank 使用数字 ID,而不是字符串。

原生 Wwise API 使用字符串或 ID 来触发 Wwise 工程中的事件和其它有名字的对象。您仍然可以在 C# 环境下这样做,方法是将文件 Wwise_IDs.h 转码为 Wwise_IDs.cs。点击 Assets > Wwise > Convert Wwise SoundBank IDs。为了完成上述操作,您需要安装 Python。

Sending MIDI to Wwise.

MIDI can be sent to Wwise by filling the AkMIDIPost members of AkMIDIPostArray class and calling any of the following methods:

The following is a basic script that sends MIDI messages to the sound engine:

public class MyMIDIBehaviour : UnityEngine.MonoBehaviour
{
    public AK.Wwise.Event SynthEvent;

    private void Start()
    {
        AkMIDIPostArray MIDIPostArrayBuffer = new AkMIDIPostArray(6);
        AkMIDIPost midiEvent = new AkMIDIPost();

        midiEvent.byType = AkMIDIEventTypes.NOTE_ON;
        midiEvent.byChan = 0;
        midiEvent.byOnOffNote = 56;
        midiEvent.byVelocity = 127;
        midiEvent.uOffset = 0;
        MIDIPostArrayBuffer[0] = midiEvent;

        midiEvent.byOnOffNote = 60;
        MIDIPostArrayBuffer[1] = midiEvent;

        midiEvent.byOnOffNote = 64;
        MIDIPostArrayBuffer[2] = midiEvent;

        midiEvent.byType = AkMIDIEventTypes.NOTE_OFF;
        midiEvent.byOnOffNote = 56;
        midiEvent.byVelocity = 0;
        midiEvent.uOffset = 48000 * 8;
        MIDIPostArrayBuffer[3] = midiEvent;

        midiEvent.byOnOffNote = 60;
        MIDIPostArrayBuffer[4] = midiEvent;

        midiEvent.byOnOffNote = 64;
        MIDIPostArrayBuffer[5] = midiEvent;

        SynthEvent.PostMIDI(gameObject, MIDIPostArrayBuffer);
    }
}

Using the Audio Input Source Plug-in in Unity.

The audio input source plug-in can be used via C# scripting. 请参阅 Audio Input Source Plug-in from the Wwise SDK documentation.

The following is a basic script that sends a test tone to the audio input source plug-in:

public class MyAudioInputBehaviour : UnityEngine.MonoBehaviour
{
    public AK.Wwise.Event AudioInputEvent;
    public uint SampleRate = 48000;
    public uint NumberOfChannels = 1;
    public uint SampleIndex = 0;
    public uint Frequency = 880;
    private bool IsPlaying = true;

    // Callback that fills audio samples - This function is called each frame for every channel.
    bool AudioSamplesDelegate(uint playingID, uint channelIndex, float[] samples)
    {
        for (uint i = 0; i < samples.Length; ++i)
            samples[i] = UnityEngine.Mathf.Sin(Frequency * 2 * UnityEngine.Mathf.PI * (i + SampleIndex) / SampleRate);

        if (channelIndex == NumberOfChannels - 1)
            SampleIndex = (uint)(SampleIndex + samples.Length) % SampleRate;

        // Return false to indicate that there is no more data to provide. This will also stop the associated event.
        return IsPlaying;
    }

    // Callback that sets the audio format - This function is called once before samples are requested.
    void AudioFormatDelegate(uint playingID, AkAudioFormat audioFormat)
    {
        // Channel configuration and sample rate are the main parameters that need to be set.
        audioFormat.channelConfig.uNumChannels = NumberOfChannels;
        audioFormat.uSampleRate = SampleRate;
    }

    private void Start()
    {
        // The AudioInputEvent event, that is setup within Wwise to use the Audio Input plug-in, is posted on gameObject.
        // AudioFormatDelegate is called once, and AudioSamplesDelegate is called once per frame until it returns false.
        AkAudioInputManager.PostAudioInputEvent(AudioInputEvent, gameObject, AudioSamplesDelegate, AudioFormatDelegate);
    }

    // This method can be called by other scripts to stop the callback
    public void StopSound()
    {
        IsPlaying = false;
    }

    private void OnDestroy()
    {
        AudioInputEvent.Stop(gameObject);
    }
}

Apply Custom Positioning in Unity

By default, the AkGameObj component is attached to a specific Unity gameObject and uses its transform (with an optional offset) for full positioning. This is usually adequate for many games, such as first-person shooters. However, games with custom camera angles, such as many third-person games, may find it difficult to accommodate the two aspects of positioning (distance attenuation and spatialization) by simply attaching the audio listener to one game object, such as the main camera in Unity. Other games may want players to experience other custom positioning.

To this end, the AkGameObj component class provides overridable positioning to Unity users. Through the three virtual methods GetPosition(), GetForward(), and GetUpward(), users can derive a subclass from AkGameObj and use that subclass component to customize any number of Unity gameObjects' positioning.

Here is a simple example of how to use a custom component to override the default AkAudioListener behavior. With a third-person project integrated with Wwise, remove the existing AkAudioListener and its associated AkGameObj. Then attach the following script to the MainCamera object, attach AkAudioListener, and finally specify the target Unity gameObject (such as the player avatar) that the audio listener's position will follow. After this, the distance attenuation of all the emitters will rely on the selected target Unity gameObject's position as the listener position (an on-screen distance listener), while the orientation of all the emitters is still based on the main camera orientation as the listener orientation (an off-screen orientation listener).

#if !(UNITY_DASHBOARD_WIDGET || UNITY_WEBPLAYER || UNITY_WII || UNITY_WIIU || UNITY_NACL || UNITY_FLASH || UNITY_BLACKBERRY) // Disable under unsupported platforms.

//
// Copyright (c) 2017 Audiokinetic Inc. / All Rights Reserved
//

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


[AddComponentMenu ("Wwise/AkGameObj3rdPersonCam")]
[ExecuteInEditMode] //ExecuteInEditMode necessary to maintain proper state of isStaticObject.
public class AkGameObj3rdPersonCam : AkGameObj
{
    public Transform target;            // The position that this camera will be following. User can specify this to the player character's Unity gameObject in the Inspector.

    
    // Sets the camera position to the player's position to handle distance attenuation.
    public override Vector3 GetPosition ()
    {
        return target.GetComponent<AkGameObj> ().GetPosition ();
    }

}
#endif // #if !(UNITY_DASHBOARD_WIDGET || UNITY_WEBPLAYER || UNITY_WII || UNITY_WIIU || UNITY_NACL || UNITY_FLASH || UNITY_BLACKBERRY) // Disable under unsupported platforms.
Generated at Tue Dec 4 10:52:21 2018 for Wwise Unity Integration by  doxygen 1.6.3