0

I am struggeling to come up with a smart way to deal with cutscenes in my 2D game. I think I have decided to Create/Control the cutscenes using the Animator, and moving the actual GameObjects around.

What I am struggeling with is:

This approach means each GameObject needs it's own Animator. So how do I then trigger the correct Cutscene? I am going to use game stages to control the game and its progress, so for example:

if(gameStage == 34 && entering scene castle)
{
    playCorrectCutscene();
}

How and where do I keep references to all my cutscenes? Say I have 22 NPCs in a scene, and 15 of those have their own Animator since they have a cutscene, how do I play the Animation from NPC_11?

I guess what I am looking for is some sort of "Cutscene Manager". The best possible solution would be to have a List of every cutscene (animation) with an ID. Then I could just use the ID to play the correct animation. But since each Animation is stored in the actual GameObject I have no idea if that is possible.

Hope Im making sense.

Green_qaue
  • 3,561
  • 11
  • 47
  • 89
  • 1
    You could create each cutscene as prefab giving it some reference name and then instantiate prefab you need - you would save plenty of memory if you have many cutscenes. –  Jul 03 '17 at 07:58
  • 1
    @RafalZiolkowski I think you should put that as an answer. Making all the gameobjects a prefab and [loading](https://stackoverflow.com/a/44832347/3785314) them with resources or assetbundles should do it. – Programmer Jul 03 '17 at 08:14
  • @Programmer - I'll do it when get back home, including code examples - I am not exactly unity developer at my daily job :) –  Jul 03 '17 at 08:16
  • No problem. Will check back when you do. – Programmer Jul 03 '17 at 08:19
  • @RafalZiolkowski Would the Animations for the cutscene then be inside the prefab you mean? Yeah a code example would be great! – Green_qaue Jul 03 '17 at 17:26
  • @Green_qaue Yes, this is what I mean –  Jul 03 '17 at 18:13

1 Answers1

1

Once you create complete prefab with 1 Animator per prefab, then you can use bellow method to create instances on the fly:

Let say you have CutsceneManager script like this:

class CutsceneManager : MonoBehaviour {
    public GameObject InstantiateResource(string folder, string name)
    {
        GameObject resource = Instantiate<GameObject>(Resources.Load<GameObject>(folder + "/" + name));
        if (resource == null)
        {
            Debug.LogErrorFormat("Cannot find resource {0} in {1}", name, folder);
            return null;
        }
        resource.name = name;
        return resource;
    }        
}

The easiest way to use it is to attach it to Empty object. Then you can create public variable for manager in other scripts, where you would need to show cutscene, like this:

class SomeOtherScript : MonoBehaviour {
    public CutsceneManager CutsceneManager;
}

This will let you drag&drop CutsceneManager empty into each script where you need it. This way you have 1 instance of cutscenemanager for all classes.

Now, in place where you would need to play custscene, you instantiate:

class SomeOtherScript : MonoBehaviour {
    public CutsceneManager CutsceneManager;

    public void PlayMyCutscene() {
        GameObject cutscene = CutsceneManager.InstantiateResource("Cutscenes", "SomeOtherCutscene");
        cutscene.setActive(true); // Or whetever other method to fire it off
    }
}

On folder structure you would need to have: Assets\Resources\Cutscenes

Cutscenes would be called: SomeOtherCutscene.prefab

Notice there is no need to include .prefab when you are instantiating one - Unity framework will "know" and add it for you.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Where do I play the actual animation? Say NPC1 walk around in my world. But he also has 2 cutscenes. Would I instantiate a new NPC1 then just to play out the cutscene, and then remove it? (PS: Should all prefabs be kept in resources folder? I just have them in a prefab-folder atm, and if I need one in a Script I attach it in the editor.). – Green_qaue Jul 03 '17 at 18:34
  • It depends how your cutscene animation is done. Can it get existing NPC1 as parameter to re-use it? –  Jul 03 '17 at 18:39
  • At the moment I just attach an Animator to NPC1 and create an animation for each cutscene. Would it then be NPC1 I Instantiate and trigger the correct anim from? – Green_qaue Jul 03 '17 at 18:41
  • Yes, I think you could do it this way. –  Jul 03 '17 at 18:44
  • 1
    I decided to make a more generic manager that I attach to the actual trigger for the cutscene. It's a bit more code and I need to loop through all cutscenes and find the correct Id and the corresponding prefab. But this way The prefab doesnt have to be in the Scene to trigger the cutScene :) Wouldnt recommend it if you have > 20 different type of prefabs that store cutScene tho, too much code and the switch-statement quickly gets messy – Green_qaue Jul 11 '17 at 19:24
  • classes should be public – derHugo Nov 29 '17 at 16:50