#if UNITY_EDITOR [UnityEditor.InitializeOnLoad] ///@brief The full Wwise Authoring API is exposed to Unity as a native plugin for the Windows and macOS platforms. All features, including subscriptions, are supported through the \c AkWAAPIClient class. public class AkWaapiClient { #region WAAPI Client Interface /// /// Connects to a Wwise Authoring application /// /// IP to connect to /// Port to connect to /// Connection success public static bool Connect(string Uri, uint Port) { try { if (AkWaapiClient_PINVOKE.IsConnected()) return true; return AkWaapiClient_PINVOKE.Connect(Uri, Port); } catch { return false; } } /// /// Is the client connected /// public static bool IsConnected { get { try { return AkWaapiClient_PINVOKE.IsConnected(); } catch { return false; } } } /// /// Disconnects from the connected Wwise Authoring application /// public static void Disconnect() { foreach (var Callback in WaapiEventCallbacks) { string Result; UnsubscribeInternal(Callback.Key, -1, out Result); } WaapiEventCallbacks.Clear(); try { AkWaapiClient_PINVOKE.Disconnect(); } catch {} } /// /// Subscribes to a topic. See Subscribing to Topics in the Wwise Authoring API for more information. /// /// Topic to subscribe to /// Elements of the Wwise objects to return /// C# delegate to execute when subscription is triggered /// Unique ID representing the subscription /// Result of the subscription attempt /// Subscription success public static bool Subscribe(string Uri, string Options, WaapiEventCallback Callback, out ulong SubscriptionID, out string Result) { try { int ResultLength; bool res = AkWaapiClient_PINVOKE.Subscribe(Uri, Options, out SubscriptionID, out ResultLength); if (res) { WaapiEventCallbacks.Add(SubscriptionID, Callback); } res &= GetLastString(ResultLength, out Result); return res; } catch { SubscriptionID = 0; Result = string.Empty; return false; } } /// /// Subscribes to a topic. See Subscribing to Topics in the Wwise Authoring API for more information. /// /// Topic to subscribe to /// Elements of the Wwise objects to return /// C# delegate to execute when subscription is triggered /// Timeout in milliseconds to wait on the Wwise Authoring application to respond /// Unique ID representing the subscription /// Result of the subscription attempt /// Subscription success public static bool Subscribe(string Uri, string Options, WaapiEventCallback Callback, int TimeoutMs, out ulong SubscriptionID, out string Result) { try { int ResultLength; bool res = AkWaapiClient_PINVOKE.Subscribe(Uri, Options, out SubscriptionID, TimeoutMs, out ResultLength); if (res) { WaapiEventCallbacks.Add(SubscriptionID, Callback); } res &= GetLastString(ResultLength, out Result); return res; } catch { SubscriptionID = 0; Result = string.Empty; return false; } } private static bool UnsubscribeInternal(ulong SubscriptionID, int TimeoutMs, out string Result) { try { int ResultLength; bool res = AkWaapiClient_PINVOKE.Unsubscribe(SubscriptionID, TimeoutMs, out ResultLength); res &= GetLastString(ResultLength, out Result); return res; } catch { Result = string.Empty; return false; } } /// /// Unsubscribes from a topic. /// /// Unique ID representing the subscription /// Result of the unsubscription attempt /// Unsubscription success public static bool Unsubscribe(ulong SubscriptionID, out string Result) { bool res = UnsubscribeInternal(SubscriptionID, -1, out Result); if (res) { WaapiEventCallbacks.Remove(SubscriptionID); } return res; } /// /// Unsubscribes from a topic. /// /// Unique ID representing the subscription /// Timeout in milliseconds to wait on the Wwise Authoring application to respond /// Result of the unsubscription attempt /// Unsubscription success public static bool Unsubscribe(ulong SubscriptionID, int TimeoutMs, out string Result) { bool res = UnsubscribeInternal(SubscriptionID, TimeoutMs, out Result); if (res) { WaapiEventCallbacks.Remove(SubscriptionID); } return res; } /// /// Perform a call to the Wwise Authoring. See Querying the Wwise Project for more information. /// /// Function to call /// Arguments required by the function /// Elements of the Wwise objects to return /// Result of the subscription attempt /// Call success public static bool Call(string Uri, string Args, string Options, out string Result) { try { int ResultLength; bool res = AkWaapiClient_PINVOKE.Call(Uri, Args, Options, out ResultLength); res &= GetLastString(ResultLength, out Result); return res; } catch { Result = string.Empty; return false; } } /// /// Perform a call to the Wwise Authoring. See Querying the Wwise Project for more information. /// /// Function to call /// Arguments required by the function /// Elements of the Wwise objects to return /// Timeout in milliseconds to wait on the Wwise Authoring application to respond /// Result of the subscription attempt /// Call success public static bool Call(string Uri, string Args, string Options, int TimeoutMs, out string Result) { try { int ResultLength; bool res = AkWaapiClient_PINVOKE.Call(Uri, Args, Options, TimeoutMs, out ResultLength); res &= GetLastString(ResultLength, out Result); return res; } catch { Result = string.Empty; return false; } } #endregion #region Callback Management /// /// Delegate ran when a subscription is triggered /// /// Unique ID representing the subscription /// Information requested in the subscription [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)] public delegate void WaapiEventCallback(ulong SubscriptionID, string Contents); private static void ProcessCallbacks() { if (UnityEditor.EditorApplication.isCompiling) { UnityEditor.EditorApplication.update -= ProcessCallbacks; } else if (IsConnected) { AkWaapiClient_PINVOKE.ProcessCallbacks(); } } [AOT.MonoPInvokeCallback(typeof(WaapiEventCallback))] private static void InternalWampEventCallback(ulong SubscriptionID, string Contents) { if (WaapiEventCallbacks.ContainsKey(SubscriptionID)) { WaapiEventCallbacks[SubscriptionID](SubscriptionID, Contents); } else { string Result; UnityEngine.Debug.Log("SubscriptionID " + SubscriptionID + " not found"); UnsubscribeInternal(SubscriptionID, -1, out Result); } } private static System.Collections.Generic.Dictionary WaapiEventCallbacks = new System.Collections.Generic.Dictionary(); #endregion static AkWaapiClient() { try { AkWaapiClient_PINVOKE.SetWampEventCallback(InternalWampEventCallback); UnityEditor.EditorApplication.update += ProcessCallbacks; } catch { } } ~AkWaapiClient() { UnityEditor.EditorApplication.update -= ProcessCallbacks; } private static bool GetLastString(int StringLength, out string Result) { var ResultBuilder = new System.Text.StringBuilder(StringLength); bool res = AkWaapiClient_PINVOKE.GetLastString(ResultBuilder, ResultBuilder.Capacity); Result = ResultBuilder.ToString(); return res; } } #endif