Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 , s. T" E3 _. K. I1 t3 r! k 引入UnityEngine.EventSystems,用函数判断一下即可: h3 s' w- _" b/ w3 ~3 @5 W2 u
" B6 S8 `6 d M$ L
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 \% Z; L9 t* ~* D
7 |% _" n2 U9 [3 l: n
unity点击UI跟场景不冲突的方法:2 A: J, W C9 `) F/ t' j" [! Z( n
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; / w0 l! u: W- H( ^8 I
8 E7 f8 t; S' P% ^" M0 T
0 A& B+ S% }5 R6 G% q