QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

查看: 5770|回复: 0

[C#/.NET] Unity点击UI与点击屏幕冲突的解决方案

[复制链接]

等级头衔

积分成就    金币 : 2861
   泡泡 : 1516
   精华 : 6
   在线时间 : 1328 小时
   最后登录 : 2026-7-17

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2021-4-10 22:26:14 | 显示全部楼层 |阅读模式
       Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。& ]" Y3 G2 C3 }4 g' r% ^9 T* R9 u
       引入UnityEngine.EventSystems,用函数判断一下即可
8 u) `/ D+ f( Y3 x0 o3 R" L! F0 f 1.jpg 8 v, w  a  M9 j5 T  @. }! i: D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.EventSystems;
public class PlayerController : MonoBehaviour
{
    private void Update()
    {
        if (EventSystem.current.IsPointerOverGameObject()) return;
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("点击屏幕");
        }
    }
}
      这个方法会将点击Text的时候也会当作点击UI,将raycast target 取消勾选可以避免。7 v9 y7 }; S7 O9 M- a' ^, l7 f
2.jpg % S- c. ]4 @4 m/ D2 ]/ a3 \% ]; Q
unity点击UI跟场景不冲突的方法:
* g3 d7 u' M: `$ @( a2 S7 C9 E       在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;
' A5 y4 q% @; Y: ^$ R! N% d9 O 3.jpg 4 M6 I; k6 T  \! y/ H8 |4 y  c1 g
  ^; f$ y) F$ R; O7 _' [/ J6 L" A% Z
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2026-9-15 19:02

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表