# SuperScenes API

## SceneComponent

This is the base abstract class that all scene components should derive from.

<details>

<summary>SceneComponent base class API</summary>

### scene

```csharp
public Scene scene;
```

The scene that the component is attached to. This is only valid at runtime, and only if the scene is currently loaded.

### OnGameStarted

```csharp
public virtual void OnGameStarted() { }
```

If implemented, this method will be executed when the game first starts, and only then. It runs both in the Editor (upon pressing Play) and in a build (when launching the game).

The method will not be invoked when the scene is loaded after the beginning of the game and thus, it will only ever run once.

{% hint style="success" %}
If you want a scene component to respond in the same way both when the game starts and when that scene is reloaded, just let one callback invoke the other, like in the following example.
{% endhint %}

{% code lineNumbers="true" %}

```csharp
[Serializable]
public class MySceneComponent : SceneComponent
{
    public override void OnGameStarted() => OnSceneLoaded();
    
    public override void OnSceneLoaded()
    {
        // Do something on both scene load and game start
    }
}
```

{% endcode %}

### OnSceneLoaded

```csharp
public virtual void OnSceneLoaded() { }
```

This method is invoked when the scene that the component is connected to has fully loaded by Unity's [Scene Manager](https://docs.unity3d.com/6000.1/Documentation/ScriptReference/SceneManagement.SceneManager.html). As such, you can inspect the scene's GameObjects in this method.

### OnSceneUnloaded

```csharp
public virtual void OnSceneLoaded() { }
```

This method is invoked when the scene that the component is connected to has been unloaded by Unity's [Scene Manager](https://docs.unity3d.com/6000.1/Documentation/ScriptReference/SceneManagement.SceneManager.html).

</details>

## SceneExtensions

At runtime, the static class `SceneExtensions` provides some extension methods for the built-in Unity struct `Scene`. This means you can just use an existing `Scene` reference, and invoke on it one of the available methods listed below:

<details>

<summary>SceneExtensions extensions class API</summary>

### GetComponent

{% code fullWidth="false" %}

```csharp
public static T GetComponent<T>(this Scene scene) where T : SceneComponent
```

{% endcode %}

Returns the first component of type `T` attached to the given `Scene` struct, if any is present. If no component of that type is present, it returns `null`.

If you are not sure if a component is present, you can use [TryGetComponent](#trygetcomponent) instead.

### TryGetComponent\<T>

{% code fullWidth="false" %}

```csharp
public static bool TryGetComponent<T>(this Scene scene, out T component)
where T : SceneComponent
```

{% endcode %}

Returns a boolean value representing whether the requested component of type `T` is present or not on the given `Scene` struct.

The output parameter `component` will contain a reference to the component or, if not present, it will contain `null`.

### GetComponents

{% code fullWidth="false" %}

```csharp
public static List<SceneComponent> GetComponents(this Scene scene)
```

{% endcode %}

Returns a list of all scene components present on the provided `Scene`. Returns an empty `List` if the scene has no components.

</details>

## SceneDataRegistry

The `SceneDataRegistry` ScriptableObject holds the index of all the `SceneDataSO` ScriptableObjects – which in turn hold the data for the components. You can use it to access components even before the related scene is loaded, but finding the component in the list is up to you.

<details>

<summary>SceneDataRegistry class API</summary>

### Instance

```csharp
public static SceneDataRegistry Instance
```

Holds a reference to the `SceneDataRegistry` ScriptableObject.

### sceneData

```csharp
public List<SceneDataSO> sceneData;
```

A list of all `SceneDataSO` objects.

### GetSceneData (string)

```csharp
public SceneDataSO GetSceneData(string scenePath)
```

Looks for a `SceneDataSO` where the path property matches the provided one. The path is intended to be project-related (i.e. starting from "Assets").

### GetSceneData (Scene)

```csharp
public SceneDataSO GetSceneData(Scene scene)
```

Looks for a `SceneDataSO` where the path property matches the path of the provided `Scene` struct.

</details>

## SceneDataSO

`SceneDataRegistrySO` ScriptableObjects hold the data for the scene components. You can reference them to access components even before the related scene is loaded.

<details>

<summary>SceneDataSO class API</summary>

### sceneAsset

```csharp
public SceneAsset sceneAsset;
```

The connected `SceneAsset`. This is only available in the editor.

### scenePath

```
public string scenePath;
```

The path of the targeted scene. Can be used at runtime to query for components even before the scene is loaded.

Note: at edit time, this is automatically provided by SuperScenes, no need to fill in manually.

### components

```csharp
[SerializeReference] public List<SceneComponent> components;
```

The list of scene components attached to the related scene.

### GetComponent\<T>

```csharp
public T GetComponent<T>() where T : SceneComponent
```

Tries to find a component in the list of the provided type. Returns `null` if not present.

### TryGetComponent\<T>

```csharp
public bool TryGetComponent<T>(out T component) where T : SceneComponent
```

Tries to find a component in the list of the provided type. The returned component will be null if not present.

</details>
