Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。 * d- h `) O- C$ U% r* ?8 y& | 引入UnityEngine.EventSystems,用函数判断一下即可9 r6 M) T3 h. i/ t z' m
3 V w8 y7 `* K" f% `5 r
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 D* j2 }1 j1 K$ ]1 ^! n2 J, g
/ \/ p( |. n4 d3 l
unity点击UI跟场景不冲突的方法: k8 d5 P- e3 j' D1 Y8 D6 `
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; 6 m& X: J& `1 G0 S3 O9 X0 c Q . c, P, j6 @4 F* E* Y/ j/ j. l: L + F& b9 u |( R8 u W( q1 ?+ ]3 b