Loading and replacing assets dynamically at runtime
.riv
files tiny without potential bloat of larger assets.riv
is running on a mobile device vs. an image of a larger resolution for desktop devices.riv
.riv
s.riv
file, by choosing the “Embedded” export type. As stated in the beginning of this page, when the Rive file gets loaded, the runtime will implicitly attempt to load in the assets embedded in the .riv
as well, and you don’t need to concern yourself with loading any assets manually.
Caveat: Embedded assets may bulk up the file size, especially when it comes to fonts when using Rive Text (Text Overview).
.riv
file, the asset will not be embedded in the file binary, but will be hosted on Rive’s CDN. This means that at runtime when loading in the file, the runtime will see the asset is marked as “Hosted” and load the asset in from the Rive CDN, so that you don’t need need to concern yourself with loading anything yourself, and the file can still remain tiny.
Caveat: The app will make an extra call to a Rive CDN to retrieve your asset
.riv
file, the asset will not be embedded in the file binary, and the responsibility of loading the asset will be handled by your application at runtime. This option enables you to dynamically load in assets via a handler API when the runtime begins loading in the .riv
file. This option is preferable if you have a need to dynamically load in a specific asset based on any kind of app/game logic, and especially if you want to keep file size small.
All referenced assets, including the .riv
, will be bundled as a zip file when you export your animation.
Caveat: You will need to provide an asset handler API when loading in Rive which should do the work of loading in an asset yourself. See Handling Assets below.
assetLoader
callback property to the list of parameters. This callback will be called for every asset the runtime detects from the .riv
file on load, and will be responsible for either handling the load of an asset at runtime or passing on the responsibility and giving the runtime a chance to load it otherwise.An instance where you may want to handle loading an asset is if an asset in the file is marked as Referenced, and you need to provide an actual asset to render for the graphic, as Rive does not embed it in the .riv
and thus cannot load it.An instance where you may want to give the runtime a chance to load the asset is if the asset in the file is marked as Hosted, and want to pass the responsibility of loading it to the runtime (which will call into a Rive CDN to do so).asset
and bytes
.asset
- Reference to a FileAsset
object from WASM. You can grab a number of properties from this object, such as the name, asset type, and more. You’ll also use this to set a new Rive-specific asset for the dynamically loaded in asset you want to set (i.e. RenderImage
for an image, Font
for a font, or Audio
for audio).bytes
- Array of bytes for the asset (if possible, such as if it’s an embedded asset)boolean
, which is where you need to return true
if you intend on handling and loading in an asset yourself, or false
if you do not want to handle asset loading for that given asset yourself, and attempt to have the runtime try to load the asset.When decoding an asset be sure to call unref
once it is no longer needed - to avoid memory leaks. This allows the engine to clean it up when it is not used by any more animations.