簡體   English   中英

統一使用 3d 模型作為按鈕

[英]Use a 3d model as button in unity

我有一個簡單的 android 統一 AR 應用程序。 當相機跟蹤圖像時會生成 3d 模型(fox)。 它工作正常。

在此處輸入圖片說明

我想從 android 手機點擊 3d 模型並打開第二個場景。我可以用一個按鈕來做,但我不能將 3dmodel 用作按鈕。 有沒有辦法將 3d 模型用作按鈕? 謝謝

您可以通過使用Raycast來“觸摸”3D 模型來實現這一點。

使用Input.GetTouch函數來獲取用戶的輸入。 在這里面,你需要調用 Raycast 函數。 Raycast 函數將發出一條射線,其原點在相機上,方向與屏幕垂直(即您正在看的方向)。 您需要在 3D 模型上放置一個Collider對象。 當光線擊中碰撞器時,Raycast 函數返回 true,您可以使用此結果打開第二個場景。

我終於使用 Raycast 制作了 6 個 3dmodel。 我在所有這些物體上都放置了網格碰撞器,並使用開關打開相應的場景。 這是其中一個模型的代碼。 它適用於鼠標點擊和安卓屏幕點擊。 非常感謝 Stitt

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;
            }
        }
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM