Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。- H2 x1 U" ]* O; t' J" V' H$ \# Q
引入UnityEngine.EventSystems,用函数判断一下即可
% r/ }; [0 c* ^4 q2 X) }: @
: W8 i7 F7 O2 g4 ?
- 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 取消勾选可以避免。
6 S2 P+ o9 n- T: d2 x8 H* v8 p
, }# V. \) a! |' F! ]
unity点击UI跟场景不冲突的方法:: v) n6 d+ ?& Y
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;
( _7 `8 M2 e7 K: b# ^/ a9 H! X5 t
* r& \2 \: ^0 M" g7 x; W4 I6 U3 Q. R9 U4 {( ^" V
|