Class BaseInputModule
A base module that raises events and sends them to GameObjects.
Inherited Members
Namespace: UnityEngine.EventSystems
Assembly: UnityEngine.UI.dll
Syntax
[RequireComponent(typeof(EventSystem))]
public abstract class BaseInputModule : UIBehaviour
Remarks
An Input Module is a component of the EventSystem that is responsible for raising events and sending them to GameObjects for handling. The BaseInputModule is a class that all Input Modules in the EventSystem inherit from. Examples of provided modules are TouchInputModule and StandaloneInputModule, if these are inadequate for your project you can create your own by extending from the BaseInputModule.
Examples
using UnityEngine;
using UnityEngine.EventSystems;
/**
* Create a module that every tick sends a 'Move' event to
* the target object
*/
public class MyInputModule : BaseInputModule
{
public GameObject m_TargetObject;
public override void Process()
{
if (m_TargetObject == null)
return;
ExecuteEvents.Execute (m_TargetObject, new BaseEventData (eventSystem), ExecuteEvents.moveHandler);
}
}
Fields
m_InputOverride
The BaseInput that replaces the default one, if any.
Declaration
protected BaseInput m_InputOverride
Field Value
| Type | Description |
|---|---|
| BaseInput |
m_RaycastResultCache
The reusable list that raycast results are collected into.
Declaration
[NonSerialized]
protected List<RaycastResult> m_RaycastResultCache
Field Value
| Type | Description |
|---|---|
| List<RaycastResult> |
Properties
eventSystem
The EventSystem that this input module belongs to.
Declaration
protected EventSystem eventSystem { get; }
Property Value
| Type | Description |
|---|---|
| EventSystem |
input
The current BaseInput being used by the input module.
Declaration
public BaseInput input { get; }
Property Value
| Type | Description |
|---|---|
| BaseInput |
inputOverride
Used to override the default BaseInput for the input module.
Declaration
public BaseInput inputOverride { get; set; }
Property Value
| Type | Description |
|---|---|
| BaseInput |
Remarks
With this it is possible to bypass the Input system with your own but still use the same InputModule. For example this can be used to feed fake input into the UI or interface with a different input system.
sendPointerHoverToParent
True if pointer hover events will be sent to the parent
Declaration
protected bool sendPointerHoverToParent { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
ActivateModule()
Called when the module is activated. Override this if you want custom code to execute when you activate your module.
Declaration
public virtual void ActivateModule()
ConvertPointerEventScrollDeltaToTicks(Vector2)
Converts PointerEventData.scrollDelta to corresponding number of ticks of the scroll wheel.
Declaration
public virtual Vector2 ConvertPointerEventScrollDeltaToTicks(Vector2 scrollDelta)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | scrollDelta | The scroll delta to convert. |
Returns
| Type | Description |
|---|---|
| Vector2 | The number of scroll wheel ticks that the scroll delta corresponds to. |
Remarks
Input Module implementations are free to apply a scaling factor to their PointerEventData's scrollDelta. This method can be used when a system needs to treat scaling-independent input values.
ConvertUIToolkitPointerId(PointerEventData)
Returns Id of the pointer following PointerId convention.
Declaration
public virtual int ConvertUIToolkitPointerId(PointerEventData sourcePointerData)
Parameters
| Type | Name | Description |
|---|---|---|
| PointerEventData | sourcePointerData | PointerEventData whose pointerId will be converted to UI Toolkit pointer convention. |
Returns
| Type | Description |
|---|---|
| int | The equivalent UI Toolkit pointer ID, or |
See Also
DeactivateModule()
Called when the module is deactivated. Override this if you want custom code to execute when you deactivate your module.
Declaration
public virtual void DeactivateModule()
DetermineMoveDirection(float, float)
Given an input movement, determine the best MoveDirection.
Declaration
protected static MoveDirection DetermineMoveDirection(float x, float y)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | X movement. |
| float | y | Y movement. |
Returns
| Type | Description |
|---|---|
| MoveDirection | The move direction that best matches the movement, or |
DetermineMoveDirection(float, float, float)
Given an input movement, determine the best MoveDirection.
Declaration
protected static MoveDirection DetermineMoveDirection(float x, float y, float deadZone)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | X movement. |
| float | y | Y movement. |
| float | deadZone | Dead zone. |
Returns
| Type | Description |
|---|---|
| MoveDirection | The move direction that best matches the movement, or |
FindCommonRoot(GameObject, GameObject)
Finds and returns a common root GameObject for two given GameObject instances.
Declaration
protected static GameObject FindCommonRoot(GameObject g1, GameObject g2)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | g1 | The first GameObject to use in the search for a common root. |
| GameObject | g2 | The second GameObject to use in the search for a common root. |
Returns
| Type | Description |
|---|---|
| GameObject | The common root GameObject, or null if none found. |
Remarks
Walks up the transform hierarchy of both objects to find the first common ancestor. Used by the event system to determine exit/enter boundaries when the pointer moves between UI elements that share a common root.
Examples
Use this to find a common ancestor of two UI objects for event boundaries. The following gets the common root of a button and a slider under a panel.
GameObject a = transform.Find("Panel/Button").gameObject;
GameObject b = transform.Find("Panel/Slider").gameObject;
GameObject root = FindCommonRoot(a, b); // Returns the Panel's GameObject.
if (root != null) Debug.Log("Common root: " + root.name);
FindFirstRaycast(List<RaycastResult>)
Return the first valid RaycastResult.
Declaration
protected static RaycastResult FindFirstRaycast(List<RaycastResult> candidates)
Parameters
| Type | Name | Description |
|---|---|---|
| List<RaycastResult> | candidates | The raycast results to search, in priority order. |
Returns
| Type | Description |
|---|---|
| RaycastResult | The first result that hit a GameObject, or an empty RaycastResult if there is none. |
GetAxisEventData(float, float, float)
Given some input data generate an AxisEventData that can be used by the event system.
Declaration
protected virtual AxisEventData GetAxisEventData(float x, float y, float moveDeadZone)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | X movement. |
| float | y | Y movement. |
| float | moveDeadZone | Move dead zone. |
Returns
| Type | Description |
|---|---|
| AxisEventData | The reusable axis event data of this module, updated with the given movement. |
GetBaseEventData()
Generate a BaseEventData that can be used by the EventSystem.
Declaration
protected virtual BaseEventData GetBaseEventData()
Returns
| Type | Description |
|---|---|
| BaseEventData | The reusable base event data of this module, reset to its default state. |
GetNavigationEventDeviceType(BaseEventData)
Returns the type of device that generated this event.
Declaration
public virtual NavigationDeviceType GetNavigationEventDeviceType(BaseEventData eventData)
Parameters
| Type | Name | Description |
|---|---|---|
| BaseEventData | eventData | An event generated by this input module. |
Returns
| Type | Description |
|---|---|
| NavigationDeviceType | The type of device that generated this event. |
Remarks
This method is used by UI Toolkit's TextField to allow navigation in and out of the TextField when using a device other than a keyboard.
If we don't know what the source of the event is, this method should return Unknown. Input Modules with more information on the event source should override this method to reflect that information.
This method's behavior is undefined for events other than Move, Submit, or Cancel.
HandlePointerExitAndEnter(PointerEventData, GameObject)
Sends pointer enter and exit events when moving from the currently entered GameObject to a new one.
Declaration
protected void HandlePointerExitAndEnter(PointerEventData currentPointerData, GameObject newEnterTarget)
Parameters
| Type | Name | Description |
|---|---|---|
| PointerEventData | currentPointerData | The pointer event data to send with each event, and to update with the new entered GameObject. |
| GameObject | newEnterTarget | The GameObject that the pointer entered, or |
Remarks
This method walks up the hierarchy until it finds a common root between the currently entered GameObject and the new one. It sends exit events up to (but not including) the common root, and then sends enter events up to (but not including) the common root. It also sends move events before the exit events, after the enter events, and on the hovered GameObjects when the pointer data changed.
IsModuleSupported()
Check to see if the module is supported. Override this if you have a platform specific module (eg. TouchInputModule that you do not want to activate on standalone.)
Declaration
public virtual bool IsModuleSupported()
Returns
| Type | Description |
|---|---|
| bool | Is the module supported. |
IsPointerOverGameObject(int)
If the module is pointer based, then override this to return true if the pointer is over an event system object.
Declaration
public virtual bool IsPointerOverGameObject(int pointerId)
Parameters
| Type | Name | Description |
|---|---|---|
| int | pointerId | The ID of the pointer. |
Returns
| Type | Description |
|---|---|
| bool | True if the pointer is over an EventSystem object. |
OnDisable()
Removes this module from the list of modules of the EventSystem.
Declaration
protected override void OnDisable()
Overrides
OnEnable()
Caches the EventSystem on the same GameObject and adds this module to its list of modules.
Declaration
protected override void OnEnable()
Overrides
Process()
Process the current tick for the module.
Declaration
public abstract void Process()
ShouldActivateModule()
Should the module be activated.
Declaration
public virtual bool ShouldActivateModule()
Returns
| Type | Description |
|---|---|
| bool | True if the EventSystem can make this module the current one. |
UpdateModule()
Update the internal state of the Module.
Declaration
public virtual void UpdateModule()