Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。
2 L+ y5 e0 H9 f! X3 X 引入UnityEngine.EventSystems,用函数判断一下即可/ ~+ N7 j* \+ ]* y8 k2 M1 I! a
* |' O$ c( O7 W3 | l+ Q) 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 取消勾选可以避免。4 n7 m% z% u9 H6 G; v2 \1 j1 y
& R7 A/ w' k' p9 u& \5 h, ounity点击UI跟场景不冲突的方法:
2 O$ _; a7 z- S& I 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;0 a# _1 A# E9 ]1 b' _
$ e6 [9 U7 q: j$ k* P' `, ]
2 i$ A: e5 L: o. B( Z- Y
|