Search Unity

Audio Patch to fix sluggish editor when using Wwise

Discussion in 'Audio & Video' started by gsylvain, Sep 27, 2021.

  1. gsylvain

    gsylvain

    Joined:
    Aug 6, 2014
    Posts:
    104
    A year and a half after my previous post - https://forum.unity.com/threads/fixed-a-huge-performance-issue-with-wwise-in-editor.891241/

    In our new project, we installed wwise 2019.2.8 Build 7432 on Unity 2020.3.5f1. It did not took a long to notice that unity editor became sluggish. Even worst, it appeared to have some memory leaks - leaving the editor on and coming back the next day often resulted in 10+GB of ram for the unity process alone, increasing over time, 5-10+% of CPU usage in an empty scene.

    AkWwiseXMLWatcher.cs - This will prevent creating a new thread (never disposed!) every frame in case the generated soundbank folder is not found.
    Code (CSharp):
    1.     public void StartWatcher()
    2.     {
    3.         basePath = AkBasePathGetter.GetPlatformBasePath();
    4.         new Thread(CreateWatcher).Start();
    5.         OnEditorUpdate();
    6.     }
    7.     public void CreateWatcher()
    8.     {
    9.         if (string.IsNullOrEmpty(basePath))
    10.         {
    11.             ExceptionOccurred = true;
    12.             return;
    13.         }
    14.        
    15.         try
    16.         {
    17.             XmlWatcher?.Dispose();
    18.             XmlWatcher = new System.IO.FileSystemWatcher(basePath) {Filter = "*.xml", IncludeSubdirectories = true, };
    19.            
    20.             // Event handlers that are watching for specific event
    21.             XmlWatcher.Created += RaisePopulateFlag;
    22.             XmlWatcher.Changed += RaisePopulateFlag;
    23.  
    24.             XmlWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
    25.             XmlWatcher.EnableRaisingEvents = false;
    26.             ExceptionOccurred = false;
    27.         }
    28.         catch
    29.         {
    30.             //ExceptionOccurred = true;
    31.         }
    32.     }
    33.  
    34.     private void OnEditorUpdate()
    35.     {
    36.         var logWarnings = AkBasePathGetter.LogWarnings;
    37.         AkBasePathGetter.LogWarnings = false;
    38.         if(string.IsNullOrEmpty(basePath)) basePath = AkBasePathGetter.GetPlatformBasePath();
    39.         AkBasePathGetter.LogWarnings = logWarnings;
    40.  
    41.         if (ExceptionOccurred || basePath != XmlWatcher?.Path)
    42.             new Thread(CreateWatcher).Start();
    43.  
    44.         if (!fireEvent)
    45.             return;
    46.  
    47.         fireEvent = false;
    48.  
    49.         var populate = PopulateXML;
    50.         if (populate == null || !populate())
    51.             return;
    52.  
    53.         var callback = XMLUpdated;
    54.         if (callback != null)
    55.         {
    56.             callback();
    57.         }
    58.  
    59.  
    60.         AkBankManager.ReloadAllBanks();
    61.     }
    AkBasePathGetter.cs - "try" at line 152
    Code (CSharp):
    1.             try
    2.             {
    3.                 // Verify if there are banks in there
    4.                 var di = new System.IO.DirectoryInfo(SoundBankDest);
    5.                
    6.                 // Super slow code
    7.                 /*
    8.                 var foundBanks = di.GetFiles("*.bnk", System.IO.SearchOption.AllDirectories);
    9.                 if (foundBanks.Length == 0)
    10.                 {
    11.                     return null;
    12.                 }
    13.  
    14.                 if (!SoundBankDest.Contains(platformName))
    15.                 {
    16.                     if (LogWarnings)
    17.                         UnityEngine.Debug.LogWarning("WwiseUnity: The platform SoundBank subfolder does not match your platform name. You will need to create a custom platform name getter for your game. See section \"Using Wwise Custom Platforms in Unity\" of the Wwise Unity integration documentation for more information");
    18.                 }
    19.                 */
    20.                 return SoundBankDest;
    21.             }
    AkWwiseWWUBuilder.cs - Simple patch to launch the wwise populate heavy detection once every second (really good enough) instead of every frame.
    Code (CSharp):
    1.     // Optim
    2.     private const float _updateInterval = 1f;
    3.     private static float _lastUpdateTime = 0f;
    4.  
    5.     private static void Tick()
    6.     {
    7.         isTicking = true;
    8.  
    9.         if (_lastUpdateTime + _updateInterval > Time.unscaledTime)
    10.             return;
    11.  
    12.         _lastUpdateTime = Time.unscaledTime;
    Code (CSharp):
    1.     public static void StartWWUWatcher()
    2.     {
    3.         if (isTicking)
    4.             return;
    5.  
    6.         _lastUpdateTime = 0f;
    7.         Tick();
    8.         UnityEditor.EditorApplication.update += Tick;
    9.     }