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类函数发射射线我始终没有成功过!!!!

相关推荐
csd7986 小时前
从原理到实操:ddraw.dll是什么?为何游戏启动时频繁提示“找不到ddraw.dll”?解决思路全解析
游戏·dll修复工具·dll修复·dll丢失·dll缺失
星释6 小时前
Rust 练习册 :Pig Latin与语言游戏
游戏·rust·c#
Nebula_g7 小时前
C语言应用实例:硕鼠游戏,田忌赛马,搬桌子,活动选择(贪心算法)
c语言·开发语言·学习·算法·游戏·贪心算法·初学者
棉猴8 小时前
《pygame中Sprite类实现多帧动画》注-通过多张序列帧显示动画2-1
python·游戏·pygame·游戏编程
mm-q291522272910 小时前
【天野学院5期】 第5期易语言半内存辅助培训班,主讲游戏——手游:仙剑奇侠传4,端游:神魔大陆2
人工智能·算法·游戏
MoRanzhi120310 小时前
Python 实现:从数学模型到完整控制台版《2048》游戏
数据结构·python·算法·游戏·数学建模·矩阵·2048
wanhengidc11 小时前
海外云手机是指什么
运维·服务器·游戏·智能手机·云计算
武陵悭臾13 小时前
Python应用开发学习: Pygame 中实现数字水平靠右对齐和垂直靠底对齐
python·学习·程序人生·游戏·个人开发·学习方法·pygame
顾安r13 小时前
11.8 脚本网页 塔防游戏
服务器·前端·javascript·游戏·html
星释15 小时前
Rust 练习册 :Poker与扑克牌游戏
开发语言·游戏·rust