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

UnityMobilePaint在安卓真机无法工作:射线检测始终返回false

Unity MobilePaint库安卓真机Raycast失败解决方案

问题根源

安卓设备上Input.mousePosition仅适配模拟鼠标输入场景,真实触摸操作下需通过Input.GetTouch()获取触摸位置,原代码依赖鼠标位置导致射线无法正确命中目标物体。

修复步骤

1. 替换触摸输入逻辑

修改MobilePaint.cs第532行附近的射线生成代码,同时兼容触摸与鼠标输入:

Ray ray;
// 优先处理安卓触摸输入
if (Input.touchCount > 0)
{
    Touch touch = Input.GetTouch(0);
    // 响应触摸开始与移动阶段
    if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
    {
        ray = cam.ScreenPointToRay(touch.position);
    }
    else
    {
        wentOutside = true;
        return;
    }
}
else
{
    // 编辑器下保留鼠标输入逻辑
    ray = cam.ScreenPointToRay(Input.mousePosition);
}

// 执行射线检测
if (!Physics.Raycast(ray, out hit, Mathf.Infinity, paintLayerMask)) 
{ 
    wentOutside = true; 
    return; 
}

2. 验证图层掩码配置

  • 确认目标绘图物体的图层与paintLayerMask指定的图层完全匹配
  • 打印paintLayerMask的数值,对比编辑器与安卓真机的输出,排除掩码配置差异

3. 检查碰撞体与相机设置

  • 确保目标物体挂载了有效Collider组件(如MeshCollider),且未勾选Is Trigger选项
  • 验证相机的Culling Mask包含目标图层,避免因相机无法渲染目标物体导致射线检测失败
  • 检查安卓设备分辨率适配设置,确保碰撞体区域未因屏幕缩放出现偏移

内容的提问来源于stack exchange,提问作者Kishore Suthar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 20:41:01