Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。5 Y$ f C. M% ?- O, C) X5 |7 s6 y+ ~
引入UnityEngine.EventSystems,用函数判断一下即可) b$ X' V5 T' q, C
7 e X. r& ]7 H2 n+ I9 M
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 取消勾选可以避免。8 G5 I8 L0 [- a. Y0 X9 A 7 a, [8 D# B7 ^' l$ [; lunity点击UI跟场景不冲突的方法:6 }8 ]' u& p' |4 e. M8 J
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;( M8 C8 s/ c, Z( E
+ Z' O* z; l+ \" E# d5 R