Introduction and use of the TouchInputModule component of Unity UGUI
1. What is the TouchInputModule component?
TouchInputModule is a UGUI component in Unity that handles touch input events. It allows your game to enable touch operations on mobile devices, such as clicking, swiping, zooming, etc.
2. How the TouchInputModule component works
The TouchInputModule component listens for touch events on a mobile device and converts them into input events required by the event system in Unity. It will trigger corresponding events based on the touch position and actions, such as clicking, dragging, etc.
3. Common properties of TouchInputModule component
- ForceModuleActive: Whether to force activation of the TouchInputModule component. If set to true, the TouchInputModule takes effect regardless of whether or not another input module is activated.
- allowActivationOnStandalone: Whether to allow simulation of touch events on the PC side. If set to true, you can use the mouse to simulate touch events.
- forceModuleActive: Whether to force activation of the TouchInputModule component. If set to true, the TouchInputModule takes effect regardless of whether or not another input module is activated.
4. Common functions of TouchInputModule component
- Process: Functions that handle touch events. Called in each frame, used to process the touch event and trigger the corresponding Unity event.
- IsModuleSupported: Check whether the current device supports the TouchInputModule component.
5. Complete example code
Example 1: Click Event
using UnityEngine; using ; public class ClickExample : MonoBehaviour, IPointerClickHandler { public void OnPointerClick(PointerEventData eventData) { ("Clicked!"); } }
Operation steps:
- Create an empty object and mount the script to that object.
- Add a Button component in the scene and bind the Button's OnClick event to the OnPointerClick function of the ClickExample script.
- When you run the game, the console will output "Clicked!" when clicking Button.
Example 2: Drag and drop event
using UnityEngine; using ; public class DragExample : MonoBehaviour, IDragHandler { public void OnDrag(PointerEventData eventData) { = ; } }
Operation steps:
- Create an object and mount the script to that object.
- Run the game, and when you touch and drag the object on your mobile device, the object moves with your fingers.
Example 3: Sliding Event
using UnityEngine; using ; public class ScrollExample : MonoBehaviour, IScrollHandler { public void OnScroll(PointerEventData eventData) { float scrollDelta = ; // Perform corresponding processing according to the sliding direction } }
Operation steps:
- Create an object and mount the script to that object.
- When running the game, when touching and sliding the object on the mobile device, the corresponding processing is performed according to the sliding direction.
Example 4: Scaling Events
using UnityEngine; using ; public class ZoomExample : MonoBehaviour, IPointerClickHandler, IScrollHandler { private float scale = 1f; public void OnPointerClick(PointerEventData eventData) { scale += 0.1f; = new Vector3(scale, scale, scale); } public void OnScroll(PointerEventData eventData) { float scrollDelta = ; scale += scrollDelta * 0.1f; = new Vector3(scale, scale, scale); } }
Operation steps:
- Create an object and mount the script to that object.
- Run the game and the object will zoom in when you click on the object on your mobile device.
- When sliding the object on a mobile device, the object will zoom in according to the sliding direction.
Example 5: Long press event
using UnityEngine; using ; public class LongPressExample : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { private bool isPressed = false; private float pressTime = 0f; public void OnPointerDown(PointerEventData eventData) { isPressed = true; pressTime = ; } public void OnPointerUp(PointerEventData eventData) { isPressed = false; if ( - pressTime >= 1f) { ("Long Pressed!"); } } }
Operation steps:
- Create an object and mount the script to that object.
- Run the game and when you hold the object on your mobile device for more than 1 second, the console will output "Long Pressed!".
Things to note
- When using the TouchInputModule component, you need to make sure there is an EventSystem object in the scene.
- If other input modules (such as StandaloneInputModule) are used at the same time, you need to pay attention to their priority settings to avoid conflicts.
References
- Unity Documentation - TouchInputModule
- Unity Manual - Event Systems
The above is the detailed content of the use examples of the Unity UGUI TouchInputModule TouchInputModule component introduction. For more information about the Unity UGUI TouchInputModule component, please follow my other related articles!