# API

Magic Lightmap Switcher provides a simple set of APIs for controlling scene lighting and loading and unloading in multi-scene mode.&#x20;

## Scene Loading/Unloading

{% hint style="warning" %}
Please note that loading and unloading scenes requires special mechanics, which are implemented in the plugin API.&#x20;

You can use your own scene manager and your own logic, but you should definitely call methods from the plugin API for loading and unloading scenes if you need the plugin mechanics to work correctly.
{% endhint %}

### LoadSceneAdditive()

To load the scene additively, call the **SceneManagment.LoadSceneAdditive()** method.

| Parameter                      | Description                                      |
| ------------------------------ | ------------------------------------------------ |
| **MonoBehaviour** callerObject | The object from which the method is called.      |
| **string** sceneName           | Scene name to load.                              |
| **bool** setActiveOnLoad       | If true, the loaded scene will be set as active. |

In the example below, the method is called on the **OnTriggerEnter()** event.

```
public class CallAdditiveSceneLoading : MonoBehaviour
{
    public string sceneName;
    public bool setActiveOnLoad;
    
    .
    .
    .
    
    private void OnTriggerEnter(Collider other)
    {
        SceneManagment.LoadSceneAdditive(this, sceneName, setActiveOnLoad);
    }
    
    .
    .
    .
}
```

### UnloadScene()

To unload the scene, call the **SceneManagment.UnloadScene()** method.

| Parameter                       | Description                                 |
| ------------------------------- | ------------------------------------------- |
| **MonoBehaviour** callerObject  | The object from which the method is called. |
| **string** sceneName            | Scene name to unload.                       |

In the example below, the method is called on the **OnTriggerEnter()** event.

```
public class CallUnloadScene : MonoBehaviour
{
    public string sceneName;
    
    .
    .
    .
    
    private void OnTriggerEnter(Collider other)
    {
        SceneManagment.UnloadScene(this, sceneName);
    }
}
```

## Switching/Blending

The **RuntimeAPI** class contains methods for switching and blending lightmaps. Each scene must have its own instance of this class.&#x20;

### BlendLightmapsCyclic()

Call **Blending.BlendLightmapsCyclic()** to start blending lightmaps cyclic in real time. This triggers a sequential transition between all lightmaps in the Lighting Scenario from the first to the last and then jumps back to the first.

| Parameter                           | Description                              |
| ----------------------------------- | ---------------------------------------- |
| **float** cycleLength               | Cycle time in seconds.                   |
| **StoredLightingScenario** scenario | The Lighting Scenario used for blending. |

Below is an example of a simple method call that starts the cyclic blending of lightmaps.

```
public class CallLightmapBlending : MonoBehaviour
{
    public StoredLightingScenario lightingScenario;
    public float blendingLength;

    private RuntimeAPI runtimeAPI;

    void Start()
    {
        runtimeAPI = new RuntimeAPI();
    }

    void Update()
    {
        runtimeAPI.BlendLightmapsCyclic(blendingLength, lightingScenario);
    }
}
```

### BlendLightmapsPingPong()

This method blend lightmaps in Lighting Scenario in a ping-pong style, i.e. from first to last and in reverse order. The parameters and calling code are exactly the same as those for **BlendLightmapsCyclic()**.

### SwitchLightmap()

To instantly switch lightmaps to a specific index in a scenario, call the **RuntimeAPI.SwitchLightmap()** method.

| Parameter                           | Description                                                                |
| ----------------------------------- | -------------------------------------------------------------------------- |
| **int** lightmapIndex               | The index of the lightmap in the blend queue of the scenario to switch to. |
| **StoredLightingScenario** scenario | The Lighting Scenario used for blending.                                   |

In the example below, the method is called on the **OnTriggerEnter()** event.

```
public class CallLightmapsSwitching : MonoBehaviour
{
    public StoredLightingScenario lightingScenario;
    public int lightmapIndex;

    private RuntimeAPI runtimeAPI;

    void Start()
    {
        runtimeAPI = new RuntimeAPI();
    }

    private void OnTriggerEnter(Collider other)
    {
        runtimeAPI.SwitchLightmap(lightmapIndex, lightingScenario);
    }
}
```
