unity透过UI去点击物体

透过UI去点击物体

1,演示
请添加图片描述
2,代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ClickUIDownObj : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (isOnClickUI)
    {
        ClickUIObjTween();
    }   
}
Ray ray;
RaycastHit hit;
public bool isOnClickUI = false;
/// <summary>
/// 透过UI去点击物体
/// </summary>
void ClickUIObjTween()
{
    if (EventSystem.current.IsPointerOverGameObject())
    {
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            if (Input.GetMouseButtonDown(0) && hit.collider.tag == "UIDownObj")
            {
                Debug.Log("点击到UI下物体:"+hit.transform.name);
                isOnClickUI = false;
            }
        }
    }
}

}

猜你喜欢

转载自blog.csdn.net/hyt0626/article/details/127464887