Skip to content

Instantly share code, notes, and snippets.

@zedarus
Last active November 27, 2018 17:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zedarus/aeb36c59c5269df4bc991aebc70db95f to your computer and use it in GitHub Desktop.
Save zedarus/aeb36c59c5269df4bc991aebc70db95f to your computer and use it in GitHub Desktop.
// WARNING: Only works in Editor scripts folder
AkWwiseProjectData data = AkWwiseProjectInfo.GetData();
for(int i = 0; i < data.EventWwu.Count; i++)
{
for (int e = 0; e < data.EventWwu[i].List.Count; e++)
{
audioClipIDs.Add(data.EventWwu[i].List[e].ID.ToString());
audioClipNames.Add(data.EventWwu[i].List[e].Name);
}
}

Wwise in editor mode changes:

IMPORTANT: Don't forget to add WWISE_EDITOR_PREVIEW in Build Settings -> Player Settings -> Scripting Define Symbols to enable Wwise support.

  • Add this code to AkBank.cs:
public void LoadInEditor()
{
		Awake();
		Start();
		HandleEvent(null);
}

public void UnloadInEditor()
{
		OnDestroy();
}
  • In AkInitializer.cs make these methods public:
    • Awake()
    • OnDestroy()
    • OnEnable()
    • LateUpdate()
    • OnDisable()
  • In AkTerminator.cs make these methods public
    • Awake()
    • OnApplicationQuit()
    • OnDestroy()
  • Make sure to wrap all DontDestroyOnLoad() methods in if (Application.isPlaying) in AkInitializer.cs and AkTerminator.cs
  • Get Wwise events IDs using code from GetWwiseEventIDs.cs (see above) and then just send one of the IDs to Play() method in WwiseEditorPreview.cs (note that in this case it uses string for id, but you can simply change that by removing type cast)
using System;
using UnityEngine;
using UnityEditor;
public class WwiseEditorPreview
{
#if WWISE_EDITOR_PREVIEW
private AkInitializer _wwiseInitializer = null;
//private AkAudioListener _listener = null;
#endif
public WwiseEditorPreview()
{
}
public void Init()
{
#if WWISE_EDITOR_PREVIEW
InitWwiseInEditor();
EditorApplication.playmodeStateChanged += OnEditorPlaymodeStateChanged;
#endif
}
public void Dispose()
{
#if WWISE_EDITOR_PREVIEW
RemoveWwiseInEditor();
EditorApplication.playmodeStateChanged -= OnEditorPlaymodeStateChanged;
#endif
}
public void Play(string clipID)
{
#if WWISE_EDITOR_PREVIEW
int id;
if (int.TryParse(clipID, out id))
{
GameObject go = CreatePreviewGO();
AkSoundEngine.PostEvent((uint)id, go);
RemovePreviewGO(go);
}
#endif
}
private void InitWwiseInEditor()
{
#if WWISE_EDITOR_PREVIEW
if (!EditorApplication.isPlaying)
{
//Debug.Log("InitWwiseInEditor");
AkTerminator term = GameObject.FindObjectOfType<AkTerminator>();
if (term != null)
{
term.Awake();
}
_wwiseInitializer = GameObject.FindObjectOfType<AkInitializer>();
_wwiseInitializer.Awake();
_wwiseInitializer.OnEnable();
AkBank[] banks = GameObject.FindObjectsOfType<AkBank>();
foreach (AkBank bank in banks)
{
bank.LoadInEditor();
}
}
EditorApplication.update += OnEditorUpdate;
#endif
}
private void RemoveWwiseInEditor()
{
#if WWISE_EDITOR_PREVIEW
if (_wwiseInitializer != null)
{
//Debug.Log("RemoveWwiseInEditor");
AkBank[] banks = GameObject.FindObjectsOfType<AkBank>();
foreach (AkBank bank in banks)
{
bank.UnloadInEditor();
}
AkTerminator term = GameObject.FindObjectOfType<AkTerminator>();
if (term != null)
{
term.OnApplicationQuit();
term.OnDestroy();
}
if (_wwiseInitializer != null)
{
_wwiseInitializer.OnDisable();
_wwiseInitializer.OnDestroy();
_wwiseInitializer = null;
}
}
EditorApplication.update -= OnEditorUpdate;
#endif
}
#region Helpers
private GameObject CreatePreviewGO()
{
#if WWISE_EDITOR_PREVIEW
GameObject preview = new GameObject("Wwise Editor Preview");
AkSoundEngine.RegisterGameObj(preview);
return preview;
#else
return null;
#endif
}
private void RemovePreviewGO(GameObject go)
{
#if WWISE_EDITOR_PREVIEW
AkSoundEngine.UnregisterGameObj(go);
GameObject.DestroyImmediate(go);
#endif
}
#endregion
#region Event Handlers
private void OnEditorUpdate()
{
#if WWISE_EDITOR_PREVIEW
if (_wwiseInitializer != null)
{
_wwiseInitializer.LateUpdate();
}
#endif
}
private void OnEditorPlaymodeStateChanged()
{
#if WWISE_EDITOR_PREVIEW
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
RemoveWwiseInEditor();
}
else if (!EditorApplication.isPaused)
{
InitWwiseInEditor();
}
#endif
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment