Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。3 E0 u- `! T; R& D5 h
引入UnityEngine.EventSystems,用函数判断一下即可 $ N I% C) d8 N6 p7 M9 L
^* m4 x9 ~0 l- 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 取消勾选可以避免。 6 x% O( P% B% r7 c7 g
7 m( h! ~ V: p. ?2 N
unity点击UI跟场景不冲突的方法:/ R+ J7 {0 \1 l$ d: e E
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; " n' o$ S+ ~) j 8 D3 M% m# d( o# [+ \: x) N; _+ t! e* }8 P/ Q