[检测用户点击屏幕时Canvas中是否有按钮

问题描述 投票:-1回答:1

我正在构建一个AR应用程序,当用户在地面上点击屏幕时会生成3D模型。

但是我面临的问题是,我在屏幕底部放置了一个按钮以进行屏幕截图。单击该按钮时,它确实会截取屏幕截图,但也会在该按钮下方生成一个图像。

如何避免这种情况?我可以使用raycast来检测按钮,而不在那里生成对象吗?

下面是我用来放置对象的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.Experimental.XR;
using System;

public class ArTapToPlace : MonoBehaviour
{
    public GameObject objectToPlace;
    public GameObject placementIndicator;

    private ARSessionOrigin arOrigin;
    private Pose placementPose;
    private bool placementPoseIsValid = false;

    void Start()
    {
        arOrigin = FindObjectOfType<ARSessionOrigin>();
    }

    void Update()
    {
        UpdatePlacementPose();
        UpdatePlacementIndicator();

        if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            PlaceObject();
        }
    }

    private void PlaceObject()
    {
        Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
    }

    private void UpdatePlacementIndicator()
    {
        print(placementIndicator);
        if (placementPoseIsValid)
        {
            placementIndicator.SetActive(true);
            placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
        }
        else
        {
            placementIndicator.SetActive(false);
        }
    }

    private void UpdatePlacementPose()
    {
        var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
        var hits = new List<ARRaycastHit>();
        arOrigin.GetComponent<ARRaycastManager>().Raycast(screenCenter, hits, UnityEngine.XR.ARSubsystems.TrackableType.Planes);
        print(hits.Count);
        print(hits);


        placementPoseIsValid = hits.Count > 0;
        if (placementPoseIsValid)
        {
            placementPose = hits[0].pose;

            var cameraForward = Camera.current.transform.forward;
            var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
            placementPose.rotation = Quaternion.LookRotation(cameraBearing);
        }
    }
}
c# unity3d augmented-reality
1个回答
0
投票

取决于UI的设置方式,您可能可以使用EventSystems.EventSystem.IsPointerOverGameObject检查指针是否在UI元素上,在这种情况下可以跳过放置

EventSystems.EventSystem.IsPointerOverGameObject
© www.soinside.com 2019 - 2024. All rights reserved.