> ## Documentation Index
> Fetch the complete documentation index at: https://rive-unity-feature-experimental-render-texture-support.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Rive Events

> Access Rive Events in Unity.

export const YouTube = ({id, timestamp}) => {
  const videoSrc = timestamp ? `https://www.youtube.com/embed/${id}?start=${timestamp}` : `https://www.youtube.com/embed/${id}`;
  return <iframe width="100%" height="400" src={videoSrc} title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerPolicy="strict-origin-when-cross-origin" allowFullScreen />;
};

<Warning>
  **DEPRECATION NOTICE:** This entire page documents the legacy Events system.
  **For new projects:** Use <Link href="data-binding">Data Binding</Link> instead.
  **For existing projects:** Plan to migrate from Events to Data Binding as soon
  as possible. **This content is provided for legacy support only.**
</Warning>

<YouTube id="M5DIDVtYI_Y" />

With Rive events, you have the ability to subscribe to meaningful signals that get reported from animations, state machines, and Rive listeners, all created at design time from the Rive editor. These signals can be subscribed to at runtime and have a specific name, type, and various custom metadata that may accompany the event to help inform the context surrounding its meaning.

For more on the Events feature in general, check out the [Events](/editor/events/overview) page in the editor section of the docs. The Event system has also been expanded to support [Audio Events ](/editor/events/audio-events)to trigger audio to play in the editor and at runtime.

For example, in a Rive graphic simulating a loader, there may be an event named `LoadComplete` fired when transitioning from a `complete` timeline animation state to an `idle` state. You can subscribe to Rive events with a callback that the runtime may invoke, and from there, your callback can handle extra functionality at just the right moment when the event fired.

Other practical use cases for events:

* Coordinating audio playback at specific moments in an animation, see [Audio Events](/editor/events/audio-events)
* Opening a URL when specific interactions have occurred
* Adding haptic feedback on meaningful touch interactions
* Implementing functionality on Buttons and other UI elements
* Send semantic information
* Communicate any information your runtime needs at the right moment

## Subscribing to Events

When you subscribe to Rive events at runtime, you subscribe to **all** Rive events that may be emitted from a state machine, and you can parse through each event by name or type to execute conditional logic.

Let's use a 5-star rater Rive example to set any text supplied with events and open a URL if one is given.

## Accessing Events

The following code demonstrates accessing all Rive events reported from an active state machine.

<Tabs>
  <Tab title="Components">
    With a reference to the **Rive Widget,** you can subscribe to the `OnRiveEventReported`  event in your scripts:

    ```csharp theme={null}
    ...

    private void OnEnable()
    {
        m_riveWidget.OnRiveEventReported += HandleRiveEventReported;
    }

    private void OnDisable()
    {
        m_riveWidget.OnRiveEventReported -= HandleRiveEventReported;
    }
    ```
  </Tab>

  <Tab title="Legacy API">
    <Warning>
      Using the low-level API is no longer recommended. Please use the [Component API](/game-runtimes/unity/components) instead for ease of use and maintainability. This content is provided for legacy support only.
    </Warning>

    ```csharp theme={null}
    ...

    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`
  </Tab>
</Tabs>

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).

```csharp theme={null}
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](https://github.com/rive-app/rive-unity-examples) and open the **EventsScene** scene.
