> For the complete documentation index, see [llms.txt](https://motiongamesstudio.gitbook.io/magic-lightmap-switcher/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://motiongamesstudio.gitbook.io/magic-lightmap-switcher/using/api.md).

# API

Magic Lightmap Switcher provides a simple set of APIs for controlling scene lighting at runtime.&#x20;

## GetInstance()

Retrieves the active MagicLightmapSwitcher instance in the specified scene. If no scene is specified, the currently active scene is used. Searches for the component inside a GameObject named "Magic Tools" (and its children).

It has asynchronous overload, which returns a guaranteed fully initialized instance for the specified scene.

```
_magicLightmapSwitcher = MagicLightmapSwitcher.GetInstance();

StartCoroutine(MagicLightmapSwitcher.GetInstance("", (mls) =>
{
    _magicLightmapSwitcher = mls;
}));
```

## 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 **RuntimeAPI.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.

```
using MLS = MagicLightmapSwitcher.MagicLightmapSwitcher;

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

    private RuntimeAPI _runtimeAPI;

    void Start()
    {
        StartCoroutine(MagicLightmapSwitcher.GetInstance("", (mls) =>
        {
            _runtimeAPI = mls.RuntimeAPI;
        }));
    }

    void Update()
    {
        if (_runtimeAPI != null)
        {
            _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.

```
using MLS = MagicLightmapSwitcher.MagicLightmapSwitcher;

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

    private RuntimeAPI _runtimeAPI;

    void Start()
    {
        StartCoroutine(MLS.GetInstance("", (mls) =>
        {
            _runtimeAPI = mls.RuntimeAPI;
        }));
    }

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://motiongamesstudio.gitbook.io/magic-lightmap-switcher/using/api.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
