HEADER

Custom Extensions and Triggers

Overview

Although we tried to cover as many use cases as possible with the wide range of features, extensions and triggers we packed into ProCamera2D, you might reach a time when you need something tailored specifically to your game. Gladly we’ve got you covered! 🙂

There are 2 primary ways to modify the behaviour of ProCamera2D. You can either do it through Extensions or through Triggers. The only difference between them is that Extensions don’t have a “physical” representation in the game and Triggers do. Therefore, Extensions are components that are usually placed on the same GameObject as the ProCamera2D component (probably your Main Camera) and Triggers are components that are placed on GameObjects that live in the scene.

When building your own Extension or Trigger there are classes you can extend to make your life easier:

Custom Extension

On this example we’re going to create a extension that moves the camera down when the user presses the down arrow, to let him see a little bit further what’s beneath him.

using UnityEngine;
using Com.LuisPedroFonseca.ProCamera2D;

public class ExtensionExample : BasePC2D
{
    public float ViewDistance = 2f;

    void Update()
    {
        if(Input.GetKey(KeyCode.DownArrow))
        {
            ProCamera2D.ApplyInfluence(new Vector2(0, -ViewDistance));
        }
    }
}

Which results in something like this:

Extension example

Custom Trigger

Now we’re going to create a trigger which changes the background color once the camera enters it. Notice the EnteredTrigger and the ExitedTrigger methods which are part of the BaseTrigger class.

It’s a pretty silly example but the truth is that the triggers that are part of ProCamera2D already cover pretty much every useful cases we could think of. 🙂

using UnityEngine;
using Com.LuisPedroFonseca.ProCamera2D;

public class TriggerExample : BaseTrigger
{
    public Color EnterColor;
    public Color ExitColor;

    protected override void EnteredTrigger()
    {
        base.EnteredTrigger();

        ProCamera2D.GameCamera.backgroundColor = EnterColor;
    }

    protected override void ExitedTrigger()
    {
        base.ExitedTrigger();

        ProCamera2D.GameCamera.backgroundColor = ExitColor;
    }
}

Which in turn results in this funny thing:

Trigger example