Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 9 d. x; x5 I7 W( k 引入UnityEngine.EventSystems,用函数判断一下即可) g7 n) ]: R4 H# p3 Z% O
/ n5 E/ F0 m u
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 取消勾选可以避免。! l7 D {6 J% j# k1 N8 {# R . l% a& O* N8 b a) o1 Junity点击UI跟场景不冲突的方法: 1 z* X4 {% e' G& s& |! Z! J8 k. x4 f 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;. v9 E- y; X% U/ {
4 k( B5 c) I/ p9 a" N
M" [$ v$ {* @3 s1 e. o% W6 }5 b