Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 " O' w# D+ A. B" m# b+ I 引入UnityEngine.EventSystems,用函数判断一下即可 2 B6 ?/ I# g8 i( p) {% a ; [% n+ U- S j9 d: x
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 x G- [! q, e# }- F4 U , B% D7 L9 O- n6 runity点击UI跟场景不冲突的方法:/ I4 u2 i, h. }+ K2 q# d( t
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;; E6 x/ u! R: n$ ]
$ r& M! \& M5 c6 N