Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。5 b& z- R. k( c f% i
引入UnityEngine.EventSystems,用函数判断一下即可 ( U' j3 T4 |+ j% x3 l' G" ] ' U/ J: y2 f1 E5 A- J0 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 取消勾选可以避免。- [: b0 X' t3 ^& k
! y9 |8 [3 g1 `4 [, M) t2 T6 }
unity点击UI跟场景不冲突的方法: / Q. K, o' W! l 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;" Z$ b' p) Y8 K; ^0 J , a0 [4 m( Z: O+ d2 Y2 t * t, a& N1 q: A- J5 h