Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。$ I2 H+ @3 \9 g
引入UnityEngine.EventSystems,用函数判断一下即可 ( \ U7 t1 s9 H7 _+ b' }) |! M8 ]. ?. k
& e, u6 b5 s) w |7 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 取消勾选可以避免。$ o7 u5 T$ K4 ?4 ~ / z/ T- T" q* ]0 Y* r. n$ Cunity点击UI跟场景不冲突的方法:& n5 |- G- S) R7 E
在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems; 2 k& Y- |! V v8 N8 P : p( M: \8 m+ q y: L, B8 t) t" O, }) v