# realvirtual Init Sequence

Unity's default execution order provides a solid foundation for initialization but often lacks the granular control needed for complex scenarios—particularly when integrating third-party solutions, orchestrating sequence events across different GameObjects, or dealing with additive scene loading. To address these challenges, **realvirtual** introduces an additional initialization sequence managed by the **realvirtualController**.

***

### Overview

By extending Unity’s built-in lifecycle, **realvirtual** ensures a more predictable and step-by-step setup process across complex multi-scene projects. This is especially helpful for coordinating dependencies among third-party plugins, custom systems, and multi-scene workflows.

The extended lifecycle adds five main phases:

1. **InitStart**
2. **InitAwake**
3. **InitEnable**
4. **InitAllScenesLoaded**
5. **InitPostAllScenesLoaded**

Each phase is opt-in; you only need to implement the corresponding interface or override the provided method in your scripts.

> **Debugging Tip**\
> You can enable **DebugMode** on the **realvirtualController** (e.g., via the Inspector) to see detailed console logs showing the exact sequence of calls. This is especially useful if you need to confirm your **InitStart**, **InitAwake**, **InitEnable**, and **InitAllScenesLoaded** methods are running at the expected times.

<figure><img src="/files/Lvrg4R0YUswlSXUUHGK7" alt=""><figcaption></figcaption></figure>

***

### Sequence Flow

1. **Scene Load (Unity)**
2. **realvirtualController Recognition**
3. **InitStart → InitAwake → InitEnable → (Additive Scenes Load) → InitAllScenesLoaded → InitPostAllScenesLoaded**

Below is a closer look at each phase.

***

### 1. InitStart

* **Triggered When**: The **realvirtualController** is first recognized.
* **Key Tasks**:
  * Reads and applies any saved settings (e.g., `DebugMode`, `ConnectionMode` from `PlayerPrefs`).
  * Invokes any script that implements the `IInitStart` interface.

#### Code Example

```csharp
using realvirtual;
using UnityEngine;

public class MyInitStartExample : MonoBehaviour, IInitStart
{
    public void OnInitStart()
    {
        // Perform essential setup, e.g., loading configuration from PlayerPrefs
        Debug.Log("InitStart: Essential setup complete.");
    }
}
```

***

### 2. InitAwake

* **Triggered When**: **realvirtualController**'s standard Awake method is called.
* **Key Tasks**:
  * Unity’s standard `Awake()` can happen before or after this, depending on Unity’s internal ordering.
  * Use `IInitAwake` for additional initialization logic that should pair with Unity’s Awake but be orchestrated by **realvirtualController**.

#### Code Example

```csharp
using realvirtual;
using UnityEngine;

public class MyInitAwakeExample : MonoBehaviour, IInitAwake
{
    public void OnInitAwake()
    {
        // Perform Awake-like setup, e.g., references or data initialization
        Debug.Log("InitAwake: More initialization logic here.");
    }
}
```

***

### 3. InitEnable

* **Triggered When**: **realvirtualController** is enabled (similar to Unity’s `OnEnable()`).
* **Key Tasks**:
  * Standard `OnEnable()` may occur before or after this, again depending on Unity’s internal ordering.
  * Regardless of the active status of your script derived from `realvirtualBehavior`, this will still be called.

#### Code Example

```csharp
using realvirtual;
using UnityEngine;

public class MyInitEnableExample : MonoBehaviour, IInitEnable
{
    public void OnInitEnable()
    {
        // Setup activation-related logic, e.g., attaching event listeners
        Debug.Log("InitEnable: Components are ready to be enabled.");
    }
}
```

***

### 4. InitAllScenesLoaded

* **Triggered When**: All scenes (including additive scenes) have been loaded.
* **Key Tasks**:
  * If no additional scenes are loaded, this runs immediately after `InitEnable`.
  * Typically used for final cross-scene references or enabling/disabling systems that depend on every scene being ready.
  * Implement `IAllScenesLoaded` or override `OnAllScenesLoaded` in your `realvirtualBehavior`.

#### Code Example

```csharp
using realvirtual;
using UnityEngine;

public class MyAllScenesLoadedExample : realvirtualBehavior, IAllScenesLoaded
{
    public void OnAllScenesLoaded()
    {
        // Access objects across multiple scenes, finalize cross-scene references, etc.
        Debug.Log("All Scenes Loaded: Multi-scene references can now be set up.");
    }

    // If overriding in realvirtualBehavior, you can also do:
    // protected override void OnAllScenesLoaded() { ... }
}
```

***

### 5. InitPostAllScenesLoaded

* **Triggered When**: Immediately after `InitAllScenesLoaded`.
* **Key Tasks**:
  * Allows “late-late” initialization logic—when you want to ensure everything in `InitAllScenesLoaded` (including other scripts) has completed.

#### Multi-Phase Example

Below is an example of a script that handles multiple phases by implementing multiple interfaces:

```csharp
using UnityEngine;
using realvirtual;

public class MyMultiStageScript : MonoBehaviour,
    IInitStart,
    IInitAwake,
    IInitEnable,
    IAllScenesLoaded
{
    public void OnInitStart()
    {
        Debug.Log("OnInitStart: Reading configuration...");
    }

    public void OnInitAwake()
    {
        Debug.Log("OnInitAwake: Setting up references...");
    }

    public void OnInitEnable()
    {
        Debug.Log("OnInitEnable: Attaching event listeners...");
    }

    public void OnAllScenesLoaded()
    {
        Debug.Log("OnAllScenesLoaded: Final cross-scene linking...");
    }
}
```

***

### Summary of Customizable Methods

| Method / Interface                        | Description                                                                                                            |
| ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| **InitStart**`IInitStart`                 | Earliest moment after the **realvirtualController** is recognized. Useful for reading config or setting global states. |
| **InitAwake**`IInitAwake`                 | Pairs with Unity’s `Awake()` for orchestrated initialization.                                                          |
| **InitEnable**`IInitEnable`               | Pairs with Unity’s `OnEnable()`, for activation-related logic.                                                         |
| **InitAllScenesLoaded**`IAllScenesLoaded` | Ensures all scenes (including additive scenes) are loaded before proceeding. Ideal for cross-scene references.         |
| **InitPostAllScenesLoaded**               | Late-late initialization after all scenes and logic in `InitAllScenesLoaded`.                                          |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.realvirtual.io/advanced-topics/for-developers/realvirtual-init-sequence.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
