Unity有点击屏幕进行移动操作,通过Input.GetMouseButtonDown(0)。如果点击到了一些UI上面会触发点击屏幕事件。8 j+ ~4 O' A# N5 p. t; f* G
引入UnityEngine.EventSystems,用函数判断一下即可( N5 T- n' a: `& i6 |0 q
9 a- n. L7 C2 y. \1 l
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 y& I% ~ q. t: U% k: V w- d% z
% s" ^5 ?3 Y. ]
unity点击UI跟场景不冲突的方法: - n% @7 `& I$ l* }0 h 在射线检测后加!EventSystem.current.IsPointerOverGameObject()即可,需要引入命名空间using UnityEngine.EventSystems;# m% B: S: a' O" H3 J; s & t: _, F/ X s. |/ k; T5 C4 c" t$ Y9 q- W9 ]$ {