As of version 2018.1, support for the Wwise Authoring API (WAAPI) is built into the Unity integration (for more information refer to Using the Wwise Authoring API (WAAPI)).
A class called AkWaapiClient
is provided, which can be used to establish a WAAPI connection and start making WAAPI calls. The following line will connect to WAAPI using localhost, on port 8080.
In order to make WAAPI calls and receive responses, JSON arguments must be constructed and parsed. The Unity JsonUtility
class makes it easy to translate between C# classes and JSON strings. The following class provides functionality to create JSON strings from classes, and vice-versa.
public class WaapiJsonClassBase<T>
{
public override string ToString()
{
return JsonUtility.ToJson(this);
}
public static implicit operator string(WaapiJsonClassBase<T> obj)
{
return obj.ToString();
}
public static T Create(string FromJson)
{
return JsonUtility.FromJson<T>(FromJson);
}
}
This class can then be extended to provide C# wrappers for specific WAAPI schemas. For example, the following classes can be used to construct the arg and option JSON strings for calls to ak.wwise.core.object.get
(for more information refer to ak.wwise.core.object.get).
[System.Serializable]
public class ObjectGetArgs : WaapiJsonClassBase<ObjectGetArgs>
{
[System.Serializable]
public class From
{
public string[] id;
}
public From from = new From();
}
[System.Serializable]
public class CallOptions : WaapiJsonClassBase<CallOptions>
{
public string[] @return;
}
The following class can then be used to parse the result.
[System.Serializable]
public class CallReturn : WaapiJsonClassBase<CallReturn>
{
[System.Serializable]
public class ReturnContents
{
public string name;
public string type;
public string path;
}
public ReturnContents[] @return;
}
The following code shows how these examples, along with some similar classes, can be used to create a simple script that connects to WAAPI and registers a callback for when an object is created in Wwise. If this code is added to an existing Unity project with Wwise integrated, a menu option will be added to the editor through which the WAAPI connection can be tested.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class WaapiJsonClassBase<T>
{
public override string ToString()
{
return JsonUtility.ToJson(this);
}
public static implicit operator string(WaapiJsonClassBase<T> obj)
{
return obj.ToString();
}
public static T Create(string FromJson)
{
return JsonUtility.FromJson<T>(FromJson);
}
}
[System.Serializable]
public class ProjectInfo : WaapiJsonClassBase<ProjectInfo>
{
[System.Serializable]
public class ProjectInfoDirectories
{
public string authoring;
public string bin;
public string help;
public string install;
public string log;
public string user;
}
[System.Serializable]
public class VersionInfo
{
public int build;
public string displayName;
public int major;
public int minor;
public string nickname;
public int schema;
public int year;
}
public int apiVersion;
public string branch;
public string configuration;
public string copyright;
public ProjectInfoDirectories directories;
public string displayName;
public bool isCommandLine;
public string platform;
public int processId;
public string processPath;
public VersionInfo version;
}
[System.Serializable]
public class ObjectCreatedJsonObject : WaapiJsonClassBase<ObjectCreatedJsonObject>
{
[System.Serializable]
public class subObject
{
public string id;
}
public subObject @object;
}
[System.Serializable]
public class ObjectGetArgs : WaapiJsonClassBase<ObjectGetArgs>
{
[System.Serializable]
public class From
{
public string[] id;
}
public From from = new From();
}
[System.Serializable]
public class CallOptions : WaapiJsonClassBase<CallOptions>
{
public string[] @return;
}
[System.Serializable]
public class CallReturn : WaapiJsonClassBase<CallReturn>
{
[System.Serializable]
public class ReturnContents
{
public string name;
public string type;
public string path;
}
public ReturnContents[] @return;
}
[System.Serializable]
public class WaapiTest : MonoBehaviour
{
public static void OnObjectCreated(ulong subID, string Json)
{
ObjectCreatedJsonObject CreatedObject = ObjectCreatedJsonObject.Create(Json);
if(CreatedObject != null)
{
string Result;
var Args = new ObjectGetArgs { from = new ObjectGetArgs.From { id = new[] { CreatedObject.@object.id } } };
var Options = new CallOptions { @return = new[]{ "name", "type", "path" } };
var ReturnedObject = CallReturn.Create(Result);
Debug.Log("New object of type " + ReturnedObject.@return[0].type + " named " + ReturnedObject.@return[0].name + " with ID " + CreatedObject.@object.id + " created at path " + ReturnedObject.@return[0].path);
}
}
[MenuItem("WWISE/TEST WAAPI")]
public static void WaapiClientTest()
{
{
Debug.Log("Connect Success!");
string WaapiResult = string.Empty;
if (res)
{
var projectInfo = ProjectInfo.Create(WaapiResult);
Debug.Log(projectInfo);
}
else
{
Debug.Log("Call failed :(");
}
var Options = new CallOptions { @return = new[] { "id" } };
ulong subId = 0;
res =
AkWaapiClient.
Subscribe(
"ak.wwise.core.object.created", Options, OnObjectCreated, out subId, out WaapiResult);
if (res)
{
Debug.Log("Subscribe success! " + WaapiResult);
}
else
{
Debug.Log("Subscribe failed :(");
}
}
else
{
Debug.Log("Connect fail :(");
}
}
}