【发布时间】:2019-11-06 12:51:22
【问题描述】:
我正在构建一个 AR 应用程序,当用户将屏幕点击到地平面时会生成一个 3d 模型。
但我面临的问题是,我在屏幕底部放置了一个按钮来截屏。单击按钮时,它会截取屏幕截图,但它也会在按钮下方生成一个图像。
我怎样才能避免这种情况?我可以使用光线投射来检测按钮,而不是在那里生成对象吗?
下面是我用来放置对象的代码
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);
}
}
}
【问题讨论】:
-
如何检测飞机上的点击?
-
arOrigin.GetComponent
().Raycast(screenCenter, hits, UnityEngine.XR.ARSubsystems.TrackableType.Planes); -
我添加了用于将对象放置到地平面上的代码@derHugo
-
是的,我可以看到你只收集了
UnityEngine.XR.ARSubsystems.TrackableType.Planes,所以这样做你永远不会知道你之前是只打过飞机还是其他东西。我不知道那种类型,但也许您可以将其限制为精确的一次点击并检查它是平面还是按钮,或者使用普通的Physics.RaycastAll并且仅在平面是 时才定位只有东西命中 -
否则
EventSystem.current.IsPointerOverGameObject()也可以在这里提供帮助,这取决于您的项目中 UI 的实现方式
标签: c# unity3d augmented-reality