Runtime Asset Swapping
You can dynamically swap assets in your Rive animations at runtime using the CustomAssetLoaderCallback
. This lets you change fonts, images, or audio files while your Rive file is playing, making it possible to create dynamic visuals.
Setting Up Asset Loading
To swap assets at runtime, provide a callback when loading your Rive file:
Your callback will be invoked whenever the runtime needs to load an asset. The callback should return true
if you’ve handled the asset loading, or false
to let the runtime handle it using the default loading behavior.
Here’s an example showing how you might swap a font at runtime:
Asset Types
You can swap these types of assets at runtime:
FontOutOfBandAsset
: For font filesImageOutOfBandAsset
: For image filesAudioOutOfBandAsset
: For audio files
Asset Reference Types
When handling assets in your callback, you’ll need to check the type of the EmbeddedAssetReference
before setting the asset. Each asset type has its own reference class with specific setter methods:
Memory Management
When working with runtime asset swapping, we recommend following these best practices:
- Always call
Load()
on your Out-Of-Band asset before using it in the callback - Dispose of resources properly:
- Call
Unload()
on your assets when they’re no longer needed - Call
Dispose()
on the Rive file when you’re done with it
- Call
Was this page helpful?