【发布时间】:2021-07-20 22:54:05
【问题描述】:
我有一个简单的 Android 统一 AR 应用程序。当相机跟踪图像时会生成一个 3d 模型(狐狸)。 它工作正常。
我想从 android 手机点击 3d 模型并打开第二个场景。我可以用一个按钮来做,但我不能将 3dmodel 用作按钮。 有没有办法使用 3d 模型作为按钮? 谢谢
【问题讨论】:
标签: c# unity3d animation augmented-reality vuforia
我有一个简单的 Android 统一 AR 应用程序。当相机跟踪图像时会生成一个 3d 模型(狐狸)。 它工作正常。
我想从 android 手机点击 3d 模型并打开第二个场景。我可以用一个按钮来做,但我不能将 3dmodel 用作按钮。 有没有办法使用 3d 模型作为按钮? 谢谢
【问题讨论】:
标签: c# unity3d animation augmented-reality vuforia
您可以通过使用Raycast“触摸”3D 模型来实现此目的。
使用Input.GetTouch 函数获取用户的输入。在其中,您将需要调用 Raycast 函数。 Raycast 功能将发射一条原点在相机上且方向垂直于屏幕(即您正在看的方向)的射线。您需要在您的 3D 模型上放置一个Collider 对象。当射线击中对撞机时,Raycast 函数返回 true,您可以使用此结果打开第二个场景。
【讨论】:
我终于使用 Raycast 制作了 6 个 3dmodel。我将网格对撞机放在所有这些上,并使用开关打开相应的场景。 这是其中一个模型的代码。它可以通过鼠标单击以及在 android 屏幕点击中正常工作。非常感谢斯蒂特
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;
public class GoToScene : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 mousePosFar = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane);
Vector3 mousePosNear = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane);
Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mousePosFar);
Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mousePosNear);
RaycastHit hit;
if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit))
{
var tagGit = hit.transform.gameObject.tag;
if (int.TryParse(tagGit, out int caseSwitch))
{ caseSwitch = Int32.Parse(tagGit); }
else { }
switch (caseSwitch)
{
case 1:
SceneManager.LoadScene("FoxScene");
break;
case 2:
SceneManager.LoadScene("TigerScene");
break;
case 3:
SceneManager.LoadScene("RaptorScene");
break;
case 4:
SceneManager.LoadScene("PenguinScene");
break;
case 5:
SceneManager.LoadScene("BeeScene");
break;
case 6:
SceneManager.LoadScene("EagleScene");
break;
default:
break;
}
}
}
}
}
【讨论】: