Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 - a# _9 R5 s( f 引入UnityEngine.EventSystems,用函数判断一下即可 , k) K- l/ Y+ R9 M9 w ~( A 5 c! X3 s' D" I. ^: g
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& m2 ?" Y6 ^ `* A0 n ) ]* v2 Q4 a/ [8 K1 h. aunity点击UI跟场景不冲突的方法:) w* n! c1 ^ _5 {6 V
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;+ ]( K# c* n7 U9 F
( I4 ?( z, g) T$ {5 ~2 l
* U1 R3 c' Z3 B" N1 j