How to manage audio assets
Fabric provides a number of strategies that help manage audio memory and performance in games that contain large amount of audio assets.
Audio Component
The Audio Component is responsible for loading the audio clip into the audio memory and it provides a number of loading strategies.
Default Behavior
By default the audio component will load its audio clip into memory during initialization and will keep it into memory for as long as the component is present in the scene. This is a simple and good strategy for small project because you know exactly the total audio memory used by the game and also reduces the runtime memory allocations which can cause memory fregmentation.
Dynamic AudioClip Loading
By Enabling this option Fabric will manage the loading of the audio data into memory automatically depending the audio clip options (for Unity 5+). For example if an audio component ir requested to play an instance for the first time the audio data will be loaded into memory and then will only get unloaded if all instances have stopped playing.
It is also possible to post LoadAudio/UnloadAudio event actions to any component in the Fabric hierarchy causing all audio components located underneath to load/unload their audio data into/from the audio memory.
Using AudioClip Path
When the dynamic audio clip option is selected for the first time the "Use AudioClip Path" behavior is enabled which uses Unity's Resource.Load function to load the audio clip into memory. This is the first implementation of this feature and was design with Unity 4 in mind. The main reason enabled by default mainly to support existing games.
AudioClip Loading options
In Unity 5+ the audio clip now provides two different options for loading the audio data,
- Load In Background: Loads the audio data Asynchronously without stalling the game
- Preload Audio Data: The audio data will load into memory the moment it is referenced by an audio component
Custom AudioClip Loader (Dialog Audio Component)
With the Dialog Audio Component you can choose the location that will look and load the audio clip with the dialog line.
Resources:
This method will look into the Resources folder (taking into account the active language folder path) for the audio clip that matches the name of the audio clip reference property. The downside of this solution is that all the dialog audio clips have to be located inside a Resources folder which will be included in the application package.
Custom AudioClip Loader:
The custom package option makes it possible to load the audio clip using a custom audio clip loader that is defined by the game. This method allows games to support loading audio clips from asset bundles. Please refer to the manual or look into the Tutorials/Script folder on the way in which it is possible to load audio clips from an AssetBundle using Unity's AssetBundleManager.
External Group Components
When a Group Component is moved outside of the main Fabric hierarchy (i.e. undernath the fabric manager) it will start behaving like an external group component and it will try to automatically register with the main hierarchy via a proxy component. This makes it possible to create self contained Fabric component hierarchies and include them with game assets (i.e. characters, vehicles etc.) that will automatically get registered with the fabric hierarchy and load their audio clips into audio memory.
When an external group component is destroyed it will automatically remove the proxy component and unload all audio clips from memory.
External group components are best used to group level specific audio components such as front end, levels etc.... that are loaded during the loading sequence. If you require to load audio dynamically as you traverse a level (i.e. specific regions etc) then it is best to have the components in the main hierarchy using the LoadAudio/UnloadAudio event actions on the appropriate group components that refer to audio clips set to load their audio data in the background.
You can find more details about external group components in the manual.