Unity多触控开发中testTouch类的触控回调无法被触发
故障原因与修复方案
1. 射线检测类型错配(根本原因)
你参考的教程是基于3D物体实现的触控演示,对应使用3D物理检测逻辑;但你实际项目用的是2D圆形Sprite物体,仅挂载SpriteRenderer、没有3D碰撞体,代码里的Physics.Raycast永远无法命中目标,自然不会发送任何触控消息。2D场景必须替换为Physics2D.Raycast接口完成射线检测。
2. SendMessage方法签名不匹配
你触发目标方法时给SendMessage传递了hit.point参数,但testTouch类里所有触控回调都是无参定义。加上你设置了SendMessageOptions.DontRequireReceiver,Unity会静默跳过签名不匹配的调用,全程不抛任何报错,这就是你看不到任何日志输出的直接原因。
3. 编辑器下无模拟逻辑
你预留的UNITY_EDITOR条件编译块是空的,编辑器下点击鼠标不会触发任何等效触控,完全没法在编辑器环境测试功能。
4. 边界逻辑存在疏漏
遍历旧触控对象发送OnTouchExit时,你传递的hit.point是整轮Update最后一次射线检测的结果,和每个被移出的触控对象没有对应关系,运行后会出现触控位置错位的问题。
修复后的完整代码
touchInput类修复版本
using System.Collections; using System.Collections.Generic; using UnityEngine; public class touchInput : MonoBehaviour { public LayerMask touchInputMask; private List<GameObject> touchList = new List<GameObject>(); private GameObject[] touchesOld; private RaycastHit2D hit; private Camera cam; void Start() { cam = GetComponent<Camera>(); } void Update() { #if UNITY_EDITOR // 编辑器下用鼠标模拟单触控逻辑 if (Input.GetMouseButtonDown(0)) { SimulateTouch(Input.mousePosition, TouchPhase.Began); } else if (Input.GetMouseButton(0)) { SimulateTouch(Input.mousePosition, TouchPhase.Stationary); } else if (Input.GetMouseButtonUp(0)) { SimulateTouch(Input.mousePosition, TouchPhase.Ended); } #endif // 真机环境处理真实多点触控 if (Input.touchCount > 0) { touchesOld = new GameObject[touchList.Count]; touchList.CopyTo(touchesOld); touchList.Clear(); foreach (Touch touch in Input.touches) { SimulateTouch(touch.position, touch.phase); } // 处理触控移出的对象 foreach (GameObject g in touchesOld) { if (!touchList.Contains(g)) g.SendMessage("OnTouchExit", SendMessageOptions.DontRequireReceiver); } } } // 抽公共触控处理逻辑,复用编辑器/真机流程 void SimulateTouch(Vector2 screenPos, TouchPhase phase) { Ray ray = cam.ScreenPointToRay(screenPos); // 替换为2D物理射线检测逻辑 if (Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, touchInputMask)) { hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity, touchInputMask); GameObject recipient = hit.transform.gameObject; touchList.Add(recipient); if (phase == TouchPhase.Began) { // 去掉多余参数,匹配testTouch里的无参方法签名 recipient.SendMessage("OnTouchDown", SendMessageOptions.DontRequireReceiver); } if (phase == TouchPhase.Ended) { recipient.SendMessage("OnTouchUp", SendMessageOptions.DontRequireReceiver); } if (phase == TouchPhase.Stationary) { recipient.SendMessage("OnTouchStay", SendMessageOptions.DontRequireReceiver); } if (phase == TouchPhase.Canceled) { recipient.SendMessage("OnTouchExit", SendMessageOptions.DontRequireReceiver); } } } }
testTouch类基础配置调整即可
using System.Collections; using System.Collections.Generic; using UnityEngine; public class testTouch : MonoBehaviour { public Color defaultColor; public Color selectedColor; private Material mat; void Start() { mat = GetComponent<SpriteRenderer>().material; } void OnTouchDown() { mat.color = selectedColor; Debug.Log("hello"); } void OnTouchUp() { mat.color = defaultColor; Debug.Log("hello"); } void OnTouchStay() { mat.color = selectedColor; Debug.Log("hello"); } void OnTouchExit() { mat.color = defaultColor; Debug.Log("hello"); } }
必做场景配置检查
- 给2D圆形物体挂载对应2D碰撞组件(如
CircleCollider2D),无碰撞体的2D物体无法被射线检测命中 - 把2D圆形所在的图层勾选进
touchInputMask的层级过滤范围 - 不要直接手动拖拽公共材质引用赋值,避免修改项目共享材质资源,建议直接用
SpriteRenderer.color属性控制颜色
内容的提问来源于stack exchange,提问作者ReduX Completedy
相关产品推荐
相关产品推荐

