Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 3 {) x% [8 p+ M. x! j5 N5 s* v 引入UnityEngine.EventSystems,用函数判断一下即可 ' m3 E: s l: _& ]) s& U! n ; {$ G4 m6 P' O
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 取消勾选可以避免。 + U% E; x/ Q) N# E& G
! N& c9 p1 n* |4 Z
unity点击UI跟场景不冲突的方法: 6 t# H# t1 j9 q- @) f 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;+ R$ R7 v( ?/ W6 X1 R; M7 w" r+ ]$ U* k 9 L: L0 I$ G( `' ]6 \7 o- B- Y7 L 8 M8 B% }& O0 f2 w s& D$ C1 Z