Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 ) Q/ ~8 O! V" e 引入UnityEngine.EventSystems,用函数判断一下即可; h# _, Z o. m6 ] e$ N5 @; g7 b1 B: p' ~' P
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 取消勾选可以避免。 2 d! q* w" R! c
* @% ?/ Q4 a9 ~4 l; A) [
unity点击UI跟场景不冲突的方法: 8 M0 m ~8 k0 a7 C" R" }* X 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; 9 {, l+ V+ j4 s* B2 e- L a! J % a8 H. L# ?# D# @ s6 L* D/ J+ Q# a" F3 l5 s