Skip to content

Referencing Scene Objects

DDevilISL edited this page Jan 19, 2018 · 1 revision

Referencing Scene Objects

So far so good, but let's say we want to clone a particle system that is in a scene? Unity is kind of a conservative so it doesn't like assets like ScriptableObjects (which is our cutscene) to hang around with scene objects, hence when you try to assign a particle system in the scene to our variable, it nopes you out.

However, it is 2018, and Unity has added a new feature called ExposedReference, which basically saves the scene object's ID and then resolves it at runtime in order to find our object. It is specially used in the Unity Timeline by Playables in order to find and modify scene objects when being executed.

They work in the same way in ShiroiCutscenes, you can add an ExposedReference to your token and resolve it when executing to find the object you want. So let's modify our previously created token to clone a ParticleSystem instead.

public class MyToken : IToken {
    public ExposedReference<ParticleSystem> System;
    public Vector3 Position;

    public IEnumerator Execute(CutscenePlayer player) {
        var systemInScene = System.Resolve(player);
        Object.Instantiate(systemInScene, Position, Quaternion.identity);
        yield break;
    }
}

IMPORTANT NOTE BELOW, DON'T EVEN THINK ABOUT SKIPPING IT!

Yay, but how does it actually work? Well, through every object in a scene would be awfully slow, so instead a cache of every used variable is stored inside a CutscenePlayer and then we use the CutscenePlayer to find the objects.

But which player is used to store these values? If you check the inspector, there is a "Bound Player" field on the top. Whichever player is currently selected will be used.

So what happens if we don't have a CutscenePlayer? Well, we won't be able to save ExposedReferences, so you can't edit them until you bind one.

And what if I change the bound player in the middle of editing a cutscene? All of the previously stored values will be present in the former player, but your cutscene won't be able to find them in the new player, so you'll have to assign these values again. We will include a tool to copy values from one player to another in the future.