Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 7 ^0 k. f( l2 x" B 引入UnityEngine.EventSystems,用函数判断一下即可 ) W: |8 ^; V7 s* Q
1 R. Q- Z% b% I, S6 J8 N
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 取消勾选可以避免。 ! L4 D8 b' f; d 2 U* O7 d8 F0 ~" x( I4 Tunity点击UI跟场景不冲突的方法: 6 D% A* H& h% { 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;1 U6 B' t4 x* G4 f; c2 r
+ O% m3 u9 c( D