Unity3D在2D游戏中获取触屏物体的方法

我们的需求是:

假如屏幕中一个棋盘,每个棋子是button构成的,我们希望手指或者鼠标在哪里,就显示那个位置的button信息。

网上有很多获取触屏物体信息的信息的方法如下面代码所示:

cs 复制代码
Camera cam = Camera.main; // pre-defined
...

if (touch.phase == TouchPhase.Bagan)){ // 如果触控点状态为按下
   Ray ray = cam.ScreenPointToRay(Input.position);
   RaycastHit hit;
   if (Physics.Raycast(ray, out hit))
   {
       Debug.Log(hit.transform.name); // 输出点击物体名称
       startTouchPos = touch.position;
   } 
}

但是在实际测试时,发现可以进入到if (touch.phase == TouchPhase.Bagan))这个分支中,但是Physics.Raycast(ray, out hit)始终返回false。

错误1:Physics.Raycast方法只适用于3D场景

翻阅资料发现:Physics.Raycast方法适用于3D场景下,在2D场景下是不会起作用的。

于是我们找到了2D场景下的替代方法:Physics2D.Raycast()

该方法最简单的定义如下:

public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction)

需要传入两个参数:origin是射线发射的起始位置,direction是射线的方向。

为了测试效果,我们制作了demo如下:

RayHandler对象对应的脚本RayHandler.cs如下:

cs 复制代码
public class RayHandler : MonoBehaviour
{
    // Start is called before the first frame update
    private Camera _camera;

    void Start()
    {
        _camera = Camera.main;  
    }

    // Update is called once per frame
    void Update()
    {

        Vector2 direction = Input.mousePosition - Camera.main.transform.position;
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.transform.position, direction);
        Debug.DrawRay(Camera.main.transform.position, direction, Color.red, 30f);
        if (hit.collider != null)
        {
            Debug.Log("true");
        }
      
    }
}

加入Debug.DrawRay(Camera.main.transform.position, direction, Color.red, 30f);是为了更方便地观察到射线的情况。

我们发现射线(红色线段)穿透button的次数大大多于true日志的次数。

这个方案还有个缺陷:如果从一个点发出射线,当出发点和终点之间有其他物体时,射线检测到的是另一个物体,而不是当前鼠标(触屏点)对应的物体。

错误2:普通UI如Image、Button等一般用射线是不起作用的。

EventSystem.current.RaycastAll()可以将当前屏幕上的所有可检测的物体全部检测到,该方法需要自己构造一个PointerEventData参数传入检测的点坐标。

参考代码如下:

cs 复制代码
 void Update()
 {
     PointerEventData pointerData = new PointerEventData(EventSystem.current);
     pointerData.position = Input.mousePosition;
     List<RaycastResult> results = new List<RaycastResult>();
     EventSystem.current.RaycastAll(pointerData, results);

     if (results.Count > 0) {
         foreach (RaycastResult r in results) {
             if (r.gameObject.layer == LayerMask.NameToLayer("button")) {
                 Debug.Log(r.gameObject.name);
             }
         }
     }
     
 }

针对Button1和Button2添加了button的layer,这样就可以过滤出我们想要的物体。

另外我做了测试,创建了Empty、2D Object,使用Physics2D.RaycastAll也没有生效

测试的代码如下:

cs 复制代码
    void Update()
    {
        Vector3 pos = GetTouchPosition();
        Ray myRay = Camera.main.ScreenPointToRay(pos);
        RaycastHit2D[] hits = Physics2D.RaycastAll(new Vector2(myRay.origin.x, myRay.origin.y), Vector2.zero);
        if(hits.Length > 0)
        {
            Debug.Log("true");
        }


    }

    public static Vector3 GetTouchPosition()
    {
        return Input.mousePosition;
    }

可能Physics2D.RaycastAll适用于一个游戏Player向另一个物体发射射线,而不是从触屏点发射射线。反正从触屏点通过Physics.Raycast类函数发射射线我始终没有成功过!!!!

相关推荐
xiezhr18 小时前
米哈游36岁程序员被曝复工当晚猝死出租屋内
游戏·程序员·游戏开发
爱搞虚幻的阿恺4 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
智算菩萨4 天前
儿童游乐空间的双维建构:室内淘气堡与室外亲子乐园的发展学理、功能分野与协同育人机制研究
游戏·游戏策划
marteker5 天前
房地产市场平台Zillow与《魔兽世界》合作展示游戏内房屋
游戏
wanhengidc5 天前
云手机 打造云端算力
运维·服务器·网络·游戏·智能手机
henry1010105 天前
DeepSeek生成的HTML5小游戏 -- 黑8台球
前端·javascript·css·游戏·html
yuanmenghao5 天前
从零开始:使用 Claude Code 打造字母消除游戏
游戏·glm·claudecode
阿甘编程点滴6 天前
2026年推荐以下5款游戏直播提词器
游戏
PieroPc6 天前
HTML5 Canvas 平台跳跃游戏
前端·游戏·html5
Swift社区6 天前
LeetCode 390 消除游戏 - Swift 题解
leetcode·游戏·swift