Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 ; K% U4 y; A- A& g9 Q' p 引入UnityEngine.EventSystems,用函数判断一下即可# ]- F+ N( ^' X) i5 B. F
; p. f' g# U: L% T9 J# B% h
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 取消勾选可以避免。: o! a1 W. M8 m T0 A- n$ |7 m- i) t . j% R/ a5 B" u* M4 Munity点击UI跟场景不冲突的方法:: y$ B ~ Q+ G/ L8 C& A7 r
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; N1 R& J$ D/ \9 K! ~* t
( X3 {9 d) o/ j! t! I0 a4 ]