Rive Events
Access Rive Events in Unity.
For more information on Rive Events see the respective runtime and editor documentation.
Accessing Events
The following code demonstrates accessing all Rive events reported from an active state machine.
With a reference to the Rive Widget, you can subscribe to the OnRiveEventReported
event in your scripts:
... private void OnEnable() { m_riveWidget.OnRiveEventReported += HandleRiveEventReported; } private void OnDisable() { m_riveWidget.OnRiveEventReported -= HandleRiveEventReported; }
... foreach (var reportedEvent in m_stateMachine?.ReportedEvents() ?? Enumerable.Empty<ReportedEvent>()) { Debug.Log($"Event received, name: \"{reportedEvent.Name}\", secondsDelay: {reportedEvent.SecondsDelay}"); } // Important! Call `advance` after accessing events. m_stateMachine?.Advance(Time.deltaTime);
The method ReportedEvents()
returns a list of ReportedEvents
Let's look at a code snippet for a star-rating Rive file. If a reported event's name is Star, the rating property of type float
and a message of type string
are retrieved from the event data (custom properties).
private void HandleRiveEventReported(ReportedEvent reportedEvent) { Debug.Log($"Event received, name: \"{reportedEvent.Name}\", secondsDelay: {reportedEvent.SecondsDelay}"); if (reportedEvent.Name == "Star") { // You can access properties directly and cast them if (evt.Properties.TryGetValue("rating", out object rating)) { float ratingValue = (float)rating; Debug.Log($"Rating: {ratingValue}"); } if (evt.Properties.TryGetValue("message", out object message)) { string messageValue = message as string; Debug.Log($"Message: {messageValue}"); } /* // For Typesafe access to properties, use the TryGet* methods for (uint i = 0; i < evt.PropertyCount; i++) { ReportedEvent.Property property = evt.GetProperty(i); if (property.Name == "rating" && property.TryGetNumber(out string ratingValue)) { Debug.Log($"Rating: {ratingValue}"); } else if (property.Name == "message" && property.TryGetString(out string messageValue)) { Debug.Log($"Message: {messageValue}"); } } */ } }
Properties that can be read are bool, string, and float.
Access a dictionary of all properties with:
reportedEvent.Properties
.
Additional Resources
For a complete example see the getting-started project in the examples repository and open the EventsScene scene.