Data binding allows you to establish connections between your Unity code and your Rive graphics. This creates a two-way reactive system where changes in your code affect the Rive design, and changes in the design can trigger responses in your code.
The simplest way to use data binding is through the Unity Inspector. The Rive Widget component includes a Data Binding Mode setting that offers three options:
Auto Bind Default: Automatically binds the default view model instance (marked as “Default” in the Rive editor)
Auto Bind Selected: Lets you select a specific instance from a dropdown
Once your Rive Widget is bound to a view model instance, you can access and modify its properties from your Unity scripts. The following example shows how to get, set, and observe different types of properties:
Copy
Ask AI
using UnityEngine;using Rive;using Rive.Components;public class DataBindingExample : MonoBehaviour{ [SerializeField] private RiveWidget riveWidget; private ViewModelInstanceNumberProperty numberProperty; private ViewModelInstanceStringProperty stringProperty; private ViewModelInstanceBooleanProperty boolProperty; private ViewModelInstanceColorProperty colorProperty; private ViewModelInstanceEnumProperty enumProperty; private ViewModelInstanceTriggerProperty triggerProperty; private void OnEnable() { riveWidget.OnWidgetStatusChanged += HandleWidgetStatusChanged; } private void OnDisable() { riveWidget.OnWidgetStatusChanged -= HandleWidgetStatusChanged; } private void HandleWidgetStatusChanged() { // Check if the widget is loaded before accessing the view model instance if (riveWidget.Status == WidgetStatus.Loaded) { ViewModelInstance viewModelInstance = riveWidget.StateMachine.ViewModelInstance; //========================================================================== // STRING PROPERTIES //========================================================================== stringProperty = viewModelInstance.GetStringProperty("title"); Debug.Log($"String value: {stringProperty.Value}"); stringProperty.Value = "New Text"; stringProperty.OnValueChanged += OnStringPropertyChanged; //========================================================================== // NUMBER PROPERTIES //========================================================================== numberProperty = viewModelInstance.GetNumberProperty("count"); Debug.Log($"Number value: {numberProperty.Value}"); numberProperty.Value = 42.5f; numberProperty.OnValueChanged += OnNumberPropertyChanged; //========================================================================== // BOOLEAN PROPERTIES //========================================================================== boolProperty = viewModelInstance.GetBooleanProperty("isActive"); Debug.Log($"Boolean value: {boolProperty.Value}"); boolProperty.Value = true; boolProperty.OnValueChanged += OnBoolPropertyChanged; //========================================================================== // COLOR PROPERTIES //========================================================================== colorProperty = viewModelInstance.GetColorProperty("backgroundColor"); // Using Unity Color (float values 0-1) Color currentColor = colorProperty.Value; colorProperty.Value = UnityEngine.Color.red; // Or using Color32 (byte values 0-255) Color32 currentColor32 = colorProperty.Value32; colorProperty.Value32 = new Color32(0, 255, 0, 255); // Green color colorProperty.OnValueChanged += OnColorPropertyChanged; //========================================================================== // ENUM PROPERTIES //========================================================================== enumProperty = viewModelInstance.GetEnumProperty("category"); Debug.Log($"Enum current value: {enumProperty.Value}"); Debug.Log($"Enum available values: {string.Join(", ", enumProperty.EnumValues)}"); enumProperty.Value = "option_name"; enumProperty.OnValueChanged += OnEnumPropertyChanged; //========================================================================== // TRIGGER PROPERTIES //========================================================================== triggerProperty = viewModelInstance.GetTriggerProperty("onSubmit"); triggerProperty.Trigger(); // Fire the trigger triggerProperty.OnTriggered += OnTriggerPropertyFired; } } private void OnNumberPropertyChanged(float newValue) { Debug.Log($"Number changed to: {newValue}"); } private void OnStringPropertyChanged(string newValue) { Debug.Log($"String changed to: {newValue}"); } private void OnBoolPropertyChanged(bool newValue) { Debug.Log($"Boolean changed to: {newValue}"); } private void OnColorPropertyChanged(UnityEngine.Color newValue) { Debug.Log($"Color changed to: {ColorUtility.ToHtmlStringRGBA(newValue)}"); } private void OnEnumPropertyChanged(string newValue) { Debug.Log($"Enum changed to: {newValue}"); } private void OnTriggerPropertyFired() { Debug.Log("Trigger fired!"); } private void OnDestroy() { // Remove property listeners if (numberProperty != null) numberProperty.OnValueChanged -= OnNumberPropertyChanged; if (stringProperty != null) stringProperty.OnValueChanged -= OnStringPropertyChanged; if (boolProperty != null) boolProperty.OnValueChanged -= OnBoolPropertyChanged; if (colorProperty != null) colorProperty.OnValueChanged -= OnColorPropertyChanged; if (enumProperty != null) enumProperty.OnValueChanged -= OnEnumPropertyChanged; if (triggerProperty != null) triggerProperty.OnTriggered -= OnTriggerPropertyFired; }}
The simplest way to use data binding is through the Unity Inspector. The Rive Widget component includes a Data Binding Mode setting that offers three options:
Auto Bind Default: Automatically binds the default view model instance (marked as “Default” in the Rive editor)
Auto Bind Selected: Lets you select a specific instance from a dropdown
Once your Rive Widget is bound to a view model instance, you can access and modify its properties from your Unity scripts. The following example shows how to get, set, and observe different types of properties:
Copy
Ask AI
using UnityEngine;using Rive;using Rive.Components;public class DataBindingExample : MonoBehaviour{ [SerializeField] private RiveWidget riveWidget; private ViewModelInstanceNumberProperty numberProperty; private ViewModelInstanceStringProperty stringProperty; private ViewModelInstanceBooleanProperty boolProperty; private ViewModelInstanceColorProperty colorProperty; private ViewModelInstanceEnumProperty enumProperty; private ViewModelInstanceTriggerProperty triggerProperty; private void OnEnable() { riveWidget.OnWidgetStatusChanged += HandleWidgetStatusChanged; } private void OnDisable() { riveWidget.OnWidgetStatusChanged -= HandleWidgetStatusChanged; } private void HandleWidgetStatusChanged() { // Check if the widget is loaded before accessing the view model instance if (riveWidget.Status == WidgetStatus.Loaded) { ViewModelInstance viewModelInstance = riveWidget.StateMachine.ViewModelInstance; //========================================================================== // STRING PROPERTIES //========================================================================== stringProperty = viewModelInstance.GetStringProperty("title"); Debug.Log($"String value: {stringProperty.Value}"); stringProperty.Value = "New Text"; stringProperty.OnValueChanged += OnStringPropertyChanged; //========================================================================== // NUMBER PROPERTIES //========================================================================== numberProperty = viewModelInstance.GetNumberProperty("count"); Debug.Log($"Number value: {numberProperty.Value}"); numberProperty.Value = 42.5f; numberProperty.OnValueChanged += OnNumberPropertyChanged; //========================================================================== // BOOLEAN PROPERTIES //========================================================================== boolProperty = viewModelInstance.GetBooleanProperty("isActive"); Debug.Log($"Boolean value: {boolProperty.Value}"); boolProperty.Value = true; boolProperty.OnValueChanged += OnBoolPropertyChanged; //========================================================================== // COLOR PROPERTIES //========================================================================== colorProperty = viewModelInstance.GetColorProperty("backgroundColor"); // Using Unity Color (float values 0-1) Color currentColor = colorProperty.Value; colorProperty.Value = UnityEngine.Color.red; // Or using Color32 (byte values 0-255) Color32 currentColor32 = colorProperty.Value32; colorProperty.Value32 = new Color32(0, 255, 0, 255); // Green color colorProperty.OnValueChanged += OnColorPropertyChanged; //========================================================================== // ENUM PROPERTIES //========================================================================== enumProperty = viewModelInstance.GetEnumProperty("category"); Debug.Log($"Enum current value: {enumProperty.Value}"); Debug.Log($"Enum available values: {string.Join(", ", enumProperty.EnumValues)}"); enumProperty.Value = "option_name"; enumProperty.OnValueChanged += OnEnumPropertyChanged; //========================================================================== // TRIGGER PROPERTIES //========================================================================== triggerProperty = viewModelInstance.GetTriggerProperty("onSubmit"); triggerProperty.Trigger(); // Fire the trigger triggerProperty.OnTriggered += OnTriggerPropertyFired; } } private void OnNumberPropertyChanged(float newValue) { Debug.Log($"Number changed to: {newValue}"); } private void OnStringPropertyChanged(string newValue) { Debug.Log($"String changed to: {newValue}"); } private void OnBoolPropertyChanged(bool newValue) { Debug.Log($"Boolean changed to: {newValue}"); } private void OnColorPropertyChanged(UnityEngine.Color newValue) { Debug.Log($"Color changed to: {ColorUtility.ToHtmlStringRGBA(newValue)}"); } private void OnEnumPropertyChanged(string newValue) { Debug.Log($"Enum changed to: {newValue}"); } private void OnTriggerPropertyFired() { Debug.Log("Trigger fired!"); } private void OnDestroy() { // Remove property listeners if (numberProperty != null) numberProperty.OnValueChanged -= OnNumberPropertyChanged; if (stringProperty != null) stringProperty.OnValueChanged -= OnStringPropertyChanged; if (boolProperty != null) boolProperty.OnValueChanged -= OnBoolPropertyChanged; if (colorProperty != null) colorProperty.OnValueChanged -= OnColorPropertyChanged; if (enumProperty != null) enumProperty.OnValueChanged -= OnEnumPropertyChanged; if (triggerProperty != null) triggerProperty.OnTriggered -= OnTriggerPropertyFired; }}
If you choose to use the low-level API to control the render loop, you’ll need to manually set up data binding in your scripts.
For reference, check this RiveScreen example that demonstrates one way to implement auto binding in a custom render loop.
Using the low-level API requires additional implementation effort and understanding of the Rive runtime. Unless you have a specific need to setup a custom render loop, we recommend using the Component API.
For a deeper understanding of data binding concepts and more advanced usage, see the Data Binding documentation.