Custom input on UI Buttons
I recently did a project where they wanted the players to press a UI button, then slide across, dragging to another button. The button you drag to is not interactable otherwise. There are many ways to implement this technique.
I chose to work with Unity’s IEventSystemHandler. This works with mouse click and mobile finger touch. In the Unity interface, you can create an OnClick() event. But with IEventSystemHandler, you can create scripts based on when you hover, drag, select, click, hold click, release click, submit, update, drag, drop, scroll, etc.
You can see how much more in depth you can go with just interacting with a simple button. In my example, I use OnPointerDown to detect when the user is holding down a specific button, I also use OnPointerUp to detect if the user has let go. On the other button, I am detecting if the user is hovering with OnPointerEnter. Together we can utilize other code to create a simple two button interaction.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Button_PressDown : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[SerializeField]
private Button_Hover _buttonHover;
//Detect if held down
public void OnPointerDown(PointerEventData pointerEventData)
{
//Tells the hoverable button that we are holding this a dragging to it
_buttonHover.canActivate = true;
}
//Detect if released
public void OnPointerUp(PointerEventData pointerEventData)
{
//Tells the hoverable button that we let go and can no longer activate the draggable action
_buttonHover.canActivate = false;
}
}
Note how we are using IPointer Handlers after MonoBehavior.
MonoBehaviour itself is a Unity-specific class that provides functionality for script components attached to GameObjects in the Unity scene. By inheriting from MonoBehaviour, you gain access to Unity’s lifecycle methods, such as Start() and Update(), and can attach your script to GameObjects to interact with the Unity engine.
When working with UI elements and user input through EventSystem, you need to handle various pointer events like pointer down and pointer up. To do this, you implement the appropriate interfaces provided by UnityEngine.EventSystems. These interfaces include IPointerDownHandler, IPointerUpHandler, and others, which define methods that get invoked when specific pointer events occur on UI elements.
By implementing these interfaces, you are essentially registering your script as a listener for the corresponding pointer events. Unity’s EventSystem will then invoke the appropriate methods in your script when those events occur, allowing you to respond to user input.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Button_Hover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public bool canActivate = false;
//Detect if the Cursor/touch starts to pass over the GameObject
public void OnPointerEnter(PointerEventData pointerEventData)
{
if(canActivate == true)
{
///Do action
}
}
//Detect when the cursor/touch leaves this GameObject
public void OnPointerExit(PointerEventData pointerEventData)
{
if(canActivate == true)
{
///Release action
}
}
}
Notice how we are using a different IPointer Handler in this. If you implement these handlers, you need to implement the methods as well, and vice versa.
You can explore more of Unity’s event system handlers at https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.IPointerClickHandler.html
On the left hand side, you will see the other event handlers under UnityEngine > UnityEngine.EventSystems > Interfaces