# Runtime Persistence

In some cases, it is necessary to allow changes to properties (e.g., **interface settings**) during runtime in a **built application**. **Runtime Persistence** enables automatic storage and retrieval of values to maintain settings across sessions.

## **Introduction**

### **How It Works**

* **Persistent Storage**: All defined values are **loaded before `OnEnable` and `Awake`** methods are executed.
* **Automatic Saving**: Values are **saved automatically** when the scene is closed, ensuring that changes persist.
* **Uses PlayerPrefs**: The data is stored using Unity’s **PlayerPrefs** system, which varies based on the platform.

### **Where Data is Stored?**

The **PlayerPrefs** storage location varies by platform:

* **Windows**: Stored in the **Registry** under:

  ```
  HKEY_CURRENT_USER\Software\[Company Name]\[Product Name]
  ```
* **MacOS**: Stored in a **.plist file** under:

  ```
  ~/Library/Preferences/unity.[Company Name].[Product Name].plist
  ```
* **Linux**: Saved in the `~/.config/unity3d/` directory.

For more details, refer to the **Unity PlayerPrefs documentation**.

## Saving Data via Runtime Persistence Component

The **Runtime Persistence** component allows saving and restoring **component properties** in Unity **without custom programming**. It enables automatic persistence of values such as **interface settings, network configurations, and UI preferences** across sessions. Besides the persistence itself properties can be configured to be editable via the Options window during Runtime.

<figure><img src="https://260262196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpYxFg97YnJX96UzNNTSd%2Fuploads%2Fgit-blob-f561f95b96ec4407855bdae4ae3b09007de47dc9%2Fruntimepersistance-options.png?alt=media" alt=""><figcaption><p>Options window with properties to be chanded during runtime</p></figcaption></figure>

<figure><img src="https://260262196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpYxFg97YnJX96UzNNTSd%2Fuploads%2Fgit-blob-40c4665ebffc093d6956fd9546f701b16b2c4df4%2Fruntimepersistance.png?alt=media" alt=""><figcaption><p>Runtime Persistence Component</p></figcaption></figure>

### **How to Use the Runtime Persistence Component**

1. **Attach the Component**
   * Add the **Runtime Persistence** script to the GameObject containing the properties you want to save.
2. **Assign Properties to Save**
   * Under **Properties to Save**, click **+** to add a new property.
   * **Component**: Select the script containing the property.
   * **Name**: Enter the exact **property name** from the script.
3. **Enable Automatic UI Integration**
   * **Show in Options (`✔`)**: The property will be automatically added to the **realvirtual.io Options Window** for user configuration.
4. **Handling Events**
   * **OnValueChanged**: Triggered when the property is modified in the Options Window (after the user exits the field).
   * **OnOptionsWindowClosing**: Triggered when the Options Window is closed.
   * These events can be used to execute **custom logic** when values change.

## Example: Mouse Sensitivity Control

One key feature using Runtime Persistence is **Mouse Sensitivity** control for 3D navigation. For detailed information, see the dedicated [Mouse Sensitivity](https://doc.realvirtual.io/basics/runtime-ui/mouse-sensitivity) page.

### **Example Use Case (Interface Configuration)**

**Scenario:** Saving the IP address of a **Denso Interface** (`DensoCobotta1300`).

* **Component:** `DensoCobotta1300 (Denso Interface)`
* **Property Name:** `ipAdress`
* **Show in Options:** `✔` (IP field will appear in the UI).
* **OnValueChanged Event:** Calls`OnValueChangedReconnectInterface.OnValueChanged`, which **deactivates and reactivates the interface** if the IP changes.
