Collider Button

A unified button solution that operates as expected across any and all platforms from mobile to virtual reality.

Collider Button offers a simple solution to capturing user input on any screen with a workflow that just works. Touch screen, in the editor, Mixed Reality, Virtual Reality, Colliders, Unity GUI, it doesn't matter - Collider Button works the same so you only need to code things once.

Working with geometry

Simply add Collider Button on anything with a collider and your button is ready to roll. Collider Button offers easy inspector adjustments for responses in color, size, and if you prefer, easy event hook-ups.

Working with GUI

Collider Button will work on any of Unity's GUI objects as long as a Collider is attached.

Working with collisions

For situations in Virtual Reality and Mixed Reality you can easily create pick-up, or physical buttons by applying a collider to your tracked controller and turning on "Is Trigger".

Working with pointers

Collider Button hooks right into Surge's Chooser system to allow for simple "flashlight" and "pointer" solutions commonly found in Mixed Reality and Virtual Reality.

Making it "click"

From Collider Button's inspector you can choose what input will trigger a pressed event. While this solution covers all of the "default" inputs like mouse, keyboard, and mapped joystick buttons it doesn't cover emerging technologies and everything else out in the wild. However, you can easily handle any input configuration by simply calling a Collider Button's "Pressed()"" method. For instance, this example will trigger a Collider Button on Magic Leap if the button is in contact with a controller that has a Collider on it set to "Is Trigger".

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using Pixelplacement;

[RequireComponent(typeof(ColliderButton))]
public class InputManager : MonoBehaviour
{
    private ColliderButton _colliderButton;

    private void Awake()
    {
        _colliderButton = GetComponent>ColliderButton<();
    }

    private void OnEnable()
    {
        MLInput.OnTriggerDown += HandleOnTriggerDown;
        MLInput.OnTriggerUp += HandleOnTriggerUp;
    }

    private void OnDisable()
    {
        MLInput.OnTriggerDown -= HandleOnTriggerDown;
        MLInput.OnTriggerUp -= HandleOnTriggerUp;
    }

    private void HandleOnTriggerDown(byte b, float f)
    {
        button.Pressed();
    }

    private void HandleOnTriggerUp(byte b, float f)
    {
        button.Released();
    }
}

Or for a more general solution on the same Magic Leap hardware you could use a Surge Chooser attached to the control for pointing and have the trigger select any button it is currently over by calling Chooser's "Pressed()" method in much the same way.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.MagicLeap;
using Pixelplacement;

public class InputManager : MonoBehaviour
{
    public Chooser chooser;

    private void Awake()
    {
        MLInput.OnTriggerDown += HandleOnTriggerDown;
        MLInput.OnTriggerUp += HandleOnTriggerUp;
    }

    private void HandleOnTriggerDown(byte b, float f)
    {
        chooser.Pressed();
    }

    private void HandleOnTriggerUp(byte b, float f)
    {
        chooser.Released();
    }
}

Responding to input

Capturing input from a Collider Button can be done through the inspector's "Unity Events" or through code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pixelplacement;

public class InputManager : MonoBehaviour
{
    public ColliderButton startButton;
    public ColliderButton cancelButton;

    private void Awake()
    {
        startButton.OnClick.AddListener(HandleStartButton);
        cancelButton.OnClick.AddListener(HandleCancelButton);
    }

    private void HandleStartButton(ColliderButton button)
    {
        Debug.Log("Start was clicked!");
    }

    private void HandleCancelButton(ColliderButton button)
    {
        Debug.Log("Cancel was clicked!");
    }
}

In addition, Collider Button offers "Global Events" which allow you to respond to any Colldier Button from a single location. This is useful for situations where you may use Collider Button as a way to capture "hits" on enemies.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pixelplacement;

public class InputManager : MonoBehaviour
{
    private void Awake()
    {
        ColliderButton.OnClickGlobal += HandleClick;
    }

    private void HandleClick(ColliderButton button)
    {
        if (button.tag == "Enemy")
        {
            Debug.Log("You hit an enemy!");
        }
    }
}