Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。3 ]9 _# O; G. V. m
引入UnityEngine.EventSystems,用函数判断一下即可 ! p3 K: m0 K6 w3 \7 B1 a
6 c' R6 | o# u& c! {$ S
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 取消勾选可以避免。 ' V6 b7 Q- J& O' P3 F
" }/ r- e+ x; }) c8 l
unity点击UI跟场景不冲突的方法: " p/ t6 q( D$ l) ` 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; / ~$ n9 U# Y+ U/ E! _* j
# Y) w) X2 y" d& A6 N
) {4 L7 u9 a A, G2 z