You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity:如何为任意控件绑定触摸事件并获取面板内触摸坐标

How to Handle Panel Touches and Get Local Position in Unity

Hey there! Don’t stress—you absolutely don’t need to write tons of low-level touch event code or manual collision checks for this. Unity’s UGUI system has built-in tools that make handling UI interactions like this totally straightforward. Here’s how to set it up step by step:

Step 1: Make Sure You Have an EventSystem

First, double-check that your scene has an EventSystem component. If you don’t see one in the Hierarchy, right-click in the Hierarchy > UI > EventSystem to create it. This is required for all UI interaction events to work.

Step 2: Add an EventTrigger to Your Panel

Select your Panel in the Hierarchy, then go to the Inspector > Add Component > EventTrigger. This component lets you bind specific UI events (like touches/clicks) to functions in your script.

Step 3: Create a Touch Handler Script

Create a new C# script (let’s call it PanelTouchHandler) with the following code. This script will hold the function you want to call when the Panel is touched, and handle converting the screen touch position to the Panel’s local coordinate system:

using UnityEngine;
using UnityEngine.EventSystems;

public class PanelTouchHandler : MonoBehaviour
{
    private RectTransform _panelRectTransform;

    void Awake()
    {
        // Get the Panel's RectTransform component once at startup
        _panelRectTransform = GetComponent<RectTransform>();
    }

    // This is the function we'll bind to the EventTrigger
    public void OnPanelTouched(PointerEventData eventData)
    {
        // Convert the screen touch position to the Panel's local coordinates
        Vector2 localTouchPosition;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(
            _panelRectTransform,
            eventData.position,
            eventData.pressEventCamera, // Use null if your Canvas is in Screen Space - Overlay mode
            out localTouchPosition
        );

        // Now you have the touch position relative to the Panel's origin
        Debug.Log($"Touch position in Panel local space: X = {localTouchPosition.x}, Y = {localTouchPosition.y}");
        
        // Add your custom logic here (e.g., spawn an object, update UI, etc.)
    }
}

Attach this script to your Panel in the Hierarchy.

Step 4: Bind the Event to Your Function

Go back to the EventTrigger component on your Panel:

  • Click the + button to add a new event.
  • From the dropdown menu, select the event type you want (e.g., Pointer Down for when the touch first starts, or Pointer Click for when the touch is released).
  • Drag your Panel object from the Hierarchy into the empty object field in the event entry.
  • Click the dropdown next to the object field, navigate to PanelTouchHandler, and select the OnPanelTouched function.

A Quick Note About Camera Settings

If your Canvas is set to Screen Space - Overlay (the default), you can pass null instead of eventData.pressEventCamera in the ScreenPointToLocalPointInRectangle method—since overlay UI doesn’t use a camera. If you’re using Screen Space - Camera, leave it as eventData.pressEventCamera to use the Canvas’s assigned camera.

That’s it! Now when the user touches the Panel, your OnPanelTouched function will run, and you’ll have the exact position of the touch relative to the Panel’s own coordinate system. No messy native code required 😊

内容的提问来源于stack exchange,提问作者Eric Jorgensen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:47:37