Placeholder Image

字幕列表 影片播放

由 AI 自動生成
  • What is up everyone and welcome to a new tutorial about Spatial Anchor.

    大家好,歡迎閱讀關於空間錨的新教程。

  • So, we previously learned how to create mixed reality applications using the Unity XR Toolkit.

    是以,我們之前學習瞭如何使用 Unity XR 工具包創建混合現實應用。

  • We did some plane detection to set plane on the position of our walls, ceiling, and floor in the real world.

    我們進行了一些平面檢測,以在現實世界中設置牆壁、天花板和地板的平面位置。

  • But how can we actually anchor not just plane but any object anywhere to our real world?

    但是,我們如何才能將平面以及任何地方的物體錨定在我們的現實世界中呢?

  • And this is what we are going to see with the Spatial Anchor.

    這就是我們將要看到的空間錨。

  • As the name suggests, the Spatial Anchor will use the feature of the real world to set the position of an object in your game.

    顧名思義,"空間錨 "將利用現實世界的特徵來設置遊戲中物體的位置。

  • This means that the object will not follow your game, which is pretty cool if you don't want the object to move when the player is centered or leave the headset.

    這意味著對象不會跟隨你的遊戲,如果你不想讓對象在玩家居中或離開耳機時移動,這就很酷了。

  • Now, I hope you guys are ready for the first tutorial of 2024 and if you are, make sure to leave a like down below.

    現在,我希望你們已經為 2024 年的第一個教程做好了準備,如果是的話,請務必在下面留下一個贊。

  • You can as always support my work on my Patreon where you will be able to find the source code of all of my tutorials and some exclusive content.

    您可以一如既往地在我的 Patreon 上支持我的工作,在那裡您可以找到我所有教程的源代碼和一些獨家內容。

  • But without further ado, let's get started!

    話不多說,讓我們開始吧!

  • Okay, so we are back where we were left at the end of the second episode about using the Unity XR Toolkit to make a meta mixed reality application and this is the project that I'm going to use to show you how to use the Spatial Anchor.

    好了,我們又回到了第二集結尾的地方,使用 Unity XR 工具包製作一個元混合現實應用程序,這就是我要向大家展示如何使用空間錨的項目。

  • So, first thing first, as you can see, there is a big change from the last episode.

    所以,首先第一件事,正如你所看到的,與上一集相比有了很大的變化。

  • It is that now the version of Unity that I'm using is 2023.2 and not 2022.

    現在我使用的 Unity 版本是 2023.2,而不是 2022。

  • Now, this is important because if we go to Windows, Package Manager, there if you go to In Project, as you can see, the version of AR Foundation that we are currently using is the version 5.1.1, but this version is actually not the latest one.

    現在,這一點很重要,因為如果我們轉到 Windows、套裝軟體管理器,然後轉到 "項目",可以看到我們當前使用的 AR Foundation 版本是 5.1.1,但這個版本實際上並不是最新的。

  • There is actually a new version, so the 6.0, which has changed some of the stuff from the AR Foundation that I want to use because I want to make this tutorial last as long as possible.

    實際上有一個新版本,也就是 6.0 版,它更改了 AR Foundation 中的一些內容,我想使用這些內容是因為我想讓這個教程儘可能長久。

  • So, to make sure that future people will be able to look at this tutorial right now and do the same stuff as me, well, what we are going to do is update this AR Foundation and, as mentioned, the first step is to use the Unity version 2023 and the second version is to download the Unity AR Foundation 6.0.

    是以,為了確保以後的人能夠現在就看這個教程,並做和我一樣的事情,我們要做的就是更新這個 AR Foundation,如前所述,第一步是使用 Unity 2023 版本,第二個版本是下載 Unity AR Foundation 6.0。

  • Now, to download this version of the AR Foundation, if we go to AR Package Manager, AR Foundation, and that we have a look at the version history, as you can see, we cannot see any other version right there and it cannot be downloaded here from the Package Manager, but what we can do is actually download the new version of the AR Foundation by changing the Package Manifest.

    現在,要下載這個版本的 AR Foundation,如果我們進入 AR 套裝軟體管理器,AR Foundation,然後查看版本歷史,可以看到,我們在這裡看不到任何其他版本,也無法從套裝軟體管理器中下載,但我們可以通過更改套裝軟體清單來下載新版本的 AR Foundation。

  • So, let me close this, then right-click on Asset here and click on Show in Explorer.

    所以,讓我關閉這個窗口,然後右鍵單擊這裡的 "資產",點擊 "在資源管理器中顯示"。

  • Now, it should open here an Explorer page and if we go here to Packages, we can double-click on Manifest right there and here you can see all of the packages that are inside this project and if we go here to the XR AR Foundation, we can see the version that we are using is 5.1.1 and, in my case, I want to use the latest one which is 6.0.0.3.5.

    現在,它應該會在這裡打開一個資源管理器頁面,如果我們轉到 "包"(Packages),可以雙擊 Manifest,在這裡你可以看到這個項目中的所有包,如果我們轉到 XR AR Foundation,可以看到我們正在使用的版本是 5.1.1,在我的情況下,我想使用最新的版本,即 6.0.0.3.5。

  • There you go.

    這就對了。

  • Now, make sure to have the same thing as me written over there and to not make any typo for this work.

    現在,請確保那邊寫的內容和我一樣,不要在這項工作中出現任何錯別字。

  • We can then save the manifest with Ctrl S and then go back to Unity.

    然後,我們可以按 Ctrl S 保存清單,然後返回 Unity。

  • And there you go, as you can see, it is now updating the package.

    就這樣,正如你所看到的,它現在正在更新套裝軟體。

  • And if we go to Windows Package Manager, as you can see, we can see that we have successfully updated the AR Foundation to the new 6.0 version.

    如果我們進入 Windows 套裝軟體管理器,可以看到我們已經成功地將 AR Foundation 更新到了新的 6.0 版本。

  • Beautiful!

    真漂亮

  • Now, let's close this and let's create our first Spatial Anchor.

    現在,關閉這個窗口,創建第一個空間錨點。

  • Okay, so the first step to create a Spatial Anchor is to go to the XR Origin right there and click on Add Component to add a AR Anchor Manager.

    好了,創建空間錨的第一步是進入 XR 起源,點擊添加組件,添加 AR 錨點管理器。

  • Beautiful!

    真漂亮

  • And, believe it or not, but it is the only thing that we need to manage the anchor in our game.

    信不信由你,但這是我們在遊戲中管理錨的唯一需要。

  • So, what we can do now is create the fourth anchor and for this, I think I'm going to right-click, go to 3D Object and create a very simple 3D cube that I'm going to put in front of the player.

    是以,我們現在要做的就是創建第四個錨點,為此,我想我要右鍵單擊,轉到 3D 對象,然後創建一個非常簡單的 3D 立方體,放在播放器前面。

  • So, let's first set it to 0, 0, move it forward a bit upward, scale it, maybe do a little rotation.

    是以,我們先將其設置為 0,0,然後將其向上移動一點,縮放,或許再旋轉一下。

  • And now, my goal is to turn this cube into an anchor which will be fixed inside our real environment and not moving with the player.

    現在,我的目標是把這個立方體變成一個錨,固定在我們的真實環境中,不隨玩家移動。

  • So, let's click on Add Component and create a new Beautiful!

    是以,讓我們點擊添加組件,創建一個新的 "美麗 "組件!

  • Now, as the name suggests, my goal is to set this cube to be an anchor at the start of the game.

    顧名思義,我的目標是在遊戲開始時將這個立方體設置為錨。

  • So, for this, we need to write at the top using UnityEngine.XR.ARFoundation and then, we can simply remove here the Update function because, as you will see, it's very simple to turn this GameObject into an anchor at the start because we only need to do GameObject.AddComponent and add a component of type ARAnchor.

    是以,我們需要在頂部使用UnityEngine.XR.ARFoundation進行編寫,然後,我們可以簡單地刪除這裡的Update函數,因為正如你將看到的那樣,一開始將這個GameObject變成一個錨點非常簡單,因為我們只需要執行GameObject.AddComponent並添加一個ARAnchor類型的組件。

  • And beautiful!

    而且很漂亮!

  • Now, that's basically it.

    現在,基本上就是這樣了。

  • Simply by adding this ARAnchor component, we will anchor this 3D cube inside our real world.

    只需添加這個 ARAnchor 組件,我們就能將這個 3D 立方體固定在現實世界中。

  • Oh, and by the way, if you want this cube to stop being an anchor in your world, you can simply call DestroyGameObject.GetComponentARAnchor like this.

    對了,如果你想讓這個立方體不再是你世界中的錨點,你可以像這樣簡單地調用 DestroyGameObject.GetComponentARAnchor。

  • But, of course, this line is just here to show a bit what you should write if you want to destroy the ARAnchor.

    當然,這一行只是為了說明一下,如果你想摧毀 ARAnchor,應該寫些什麼。

  • We don't actually want to do this in this case.

    在這種情況下,我們實際上並不想這樣做。

  • So, let me simply remove it like this.

    所以,讓我這樣簡單地刪除它吧。

  • Beautiful!

    真漂亮

  • And now, let's save and go back to the Unity.

    現在,讓我們保存並返回 "統一 "系統。

  • Now, what I'm going to do is simply place this cube on the side and duplicate it.

    現在,我要做的就是把這個立方體放在邊上,然後複製它。

  • And on this new cube on the right, what I'm going to do is remove here the SetAnchorAtStart.

    在右側的這個新立方體上,我要做的是刪除這裡的 SetAnchorAtStart。

  • Now, my goal is simply to showcase to you the difference between one cube which is anchor and another which is not.

    現在,我的目標只是向你們展示一個立方體是錨和另一個不是錨之間的區別。

  • So, one thing left to do.

    所以,還有一件事沒做。

  • Now, it is to build our game.

    現在,我們要打造自己的遊戲。

  • So, let's go to File and click on Build and Run.

    是以,讓我們轉到文件,點擊 "構建並運行"。

  • Okay, so here you go.

    好了,給你。

  • As you can see, I'm inside the Mixed Reality application.

    如你所見,我正在混合現實應用程序中。

  • On the right side, I have a cube which is anchor but not on the left side.

    在右側,我有一個立方體,它是錨,但左側沒有。

  • And let me just show you the difference.

    讓我來告訴你其中的區別。

  • If, for example, I turn my head slightly to the left here and that I try to press on the recenter button here on my Oculus right hand controller.

    舉例來說,如果我把頭稍稍向左轉,然後嘗試按下 Oculus 右手控制器上的 "重新定位 "按鈕。

  • As you can see, this cube does not move because it is anchor inside our real environment and not based on the player's position like the rest of the objects are.

    正如你所看到的,這個立方體不會移動,因為它是我們真實環境中的錨點,而不是像其他物體那樣基於玩家的位置。

  • But, of course, here we are able to anchor some objects at the start of the game.

    當然,在這裡我們可以在遊戲開始時錨定一些對象。

  • Now, let me show you how to do this at runtime.

    現在,讓我來告訴你如何在運行時做到這一點。

  • So, let's go back to Unity.

    那麼,讓我們回到 "團結"。

  • Okay, so now let's see how we can create anchor during runtime.

    好了,現在讓我們看看如何在運行時創建錨點。

  • So, to do this, I'm going to create a very simple interaction here which is to add some ray which will come out of my hands.

    是以,為了實現這一點,我將在這裡創建一個非常簡單的交互,即添加一些射線,這些射線將從我的手中射出。

  • And if I press on the grip button, I want to set an anchor where my ray is pointing.

    如果我按下手柄按鈕,我想在射線指向的地方設置一個錨點。

  • So, first thing first, we need to add a ray interactor.

    首先,我們需要添加一個射線交互器。

  • So, for this, let's go under our camera offset here which is located under the XR for ray interactor action based.

    是以,為此,讓我們轉到攝影機偏移量下,該偏移量位於 XR 下,用於基於射線交互器的操作。

  • Beautiful.

    真漂亮

  • I'm going to call it right ray interactor.

    我把它叫做右射線交互器。

  • We can go under the XR controller, select here this little icon and search for the right controller preset.

    我們可以進入 XR 控制器,選擇這裡的小圖標,然後搜索合適的控制器預設。

  • Beautiful.

    真漂亮

  • Now, we can duplicate this, rename it left ray interactor and do the same but this time for the left controller preset.

    現在,我們可以複製這個預設,將其重命名為左射線交互器,然後對左控制器預設執行同樣的操作。

  • Beautiful.

    真漂亮

  • Now, with this, as you can see, this will add a ray interactor that will follow our left or right hand.

    現在,如你所見,這將添加一個射線交互器,它將跟隨我們的左手或右手。

  • And one thing that I want to do is if we select both the right and left ray interactor by pressing on the control key, I want here to set the line bend ratio to one because I don't want the line to bend at all.

    我想做的一件事是,如果我們按下控制鍵同時選擇左右光線交互器,我想在這裡將線條彎曲率設置為 1,因為我根本不想讓線條彎曲。

  • Beautiful.

    真漂亮

  • Now, if you remember in the previous episode, what was actually happening was that there was a near default plane which was for each one of the wall or the ceiling of the floor that we have in our real world.

    現在,如果你還記得上一集,實際發生的情況是,在我們的現實世界中,每一面牆壁或地板天花板都有一個近似默認的平面。

  • And if we click on this AR default plane, as you can see, it is basically a mesh collider.

    如果我們點擊這個 AR 默認平面,正如你所看到的,它基本上就是一個網格碰撞器。

  • So, this allowed us, if you remember, to create some collision within the scene from our real world to our virtual one which is awesome.

    是以,如果你還記得的話,這讓我們可以在場景中創建一些從現實世界到虛擬世界的碰撞,這非常棒。

  • But anyway, I want to use this AR plane as well because I want to make it from the ray interactor.

    但無論如何,我也想使用這個 AR 平面,因為我想通過光線交互器來製作它。

  • So, let's click on add component and add a xr simple interactable.

    所以,讓我們點擊添加組件,添加一個 xr 簡單可交互組件。

  • Beautiful.

    真漂亮

  • And that's basically it.

    基本上就是這樣。

  • With it, we should be able to interact with this plane.

    有了它,我們就能與這架飛機互動。

  • So, what's left for us to do is now go back to the right and left ray interactor.

    是以,我們現在要做的就是回到左右射線交互器。

  • I'm going to select them both and I'm going here to create a new component which will be called phone anchor from ray select.

    我將同時選中這兩個組件,並在此創建一個新組件,該組件將被稱為 "ray select 中的電話錨"。

  • Beautiful.

    真漂亮

  • Okay.

    好的

  • So, again, as the name suggests, the goal of this script is to spawn an anchor from the ray selection.

    是以,顧名思義,這個腳本的目的還是從射線選區中生成一個錨點。

  • So, for this, we are going to need a public reference to the xr ray interactor which we can call if we go at the top and do using unityengine.xr.interaction.toolkit.

    為此,我們需要一個 xr 射線交互器的公共引用,如果我們在頂部使用 unityengine.xr.interaction.toolkit 就可以調用該引用。

  • And while we are at it, let's add also the unityengine.xr.ar package that we need.

    同時,我們還需要添加 unityengine.xr.ar 套裝軟體。

  • And then the two variables that I'm going to need is a xr ray interactor which I can call ray interactor and a public AR anchor manager called anchor manager.

    然後,我需要兩個變量,一個是 xr 射線交互器(我可以稱之為射線交互器),另一個是名為錨點管理器(anchor manager)的公共 AR 錨點管理器。

  • Beautiful.

    真漂亮

  • Now, what I'm going to do is create a public void function which will be called spawn anchor and which will take as a parameter a base interaction event args which I can call args.

    現在,我要做的是創建一個公共 void 函數,該函數將被稱為 spawn anchor,它將接收一個基本交互事件 args 作為參數,我可以將其稱為 args。

  • So, the goal of using this variable here is that now in the start function, we can do ray interactor.selectentered.addlistener and use here our spawn anchor function.

    是以,在這裡使用這個變量的目的是,現在在啟動函數中,我們可以使用 ray interactor.selectentered.addlistener 並在這裡使用我們的 spawn anchor 函數。

  • So, basically, what we've done is to hook this spawn anchor function to be called whenever the ray interactor selects something.

    是以,基本上,我們所做的就是掛接這個生成錨點函數,以便在光線交互器選擇某樣東西時調用它。

  • So, basically, what we can do is then get the hit point and then create an anchor based on this hit point from the ray.

    是以,基本上我們能做的就是獲取命中點,然後根據射線的命中點創建一個錨點。

  • So, first things first, let's get the 3D raycast hit with ray interactor.try get current 3D raycast hit.

    首先,讓我們使用 ray interactor.try 獲取當前的 3D 射線命中率。

  • And what we want is write out raycast hit hit.

    我們想要的是寫出 raycast hit hit。

  • So, here the out raycast hit hit will basically be our output.

    是以,在這裡,我們的輸出基本上就是射線播出的命中率。

  • So, this will be the raycast hit that is currently performed by our ray interactor.

    是以,這將是我們的射線交互器當前執行的射線投射命中。

  • And from this hit position, we can actually create a new pose by doing pose hit pose equals new pose and write here hit dot point for the position.

    根據這個命中位置,我們可以通過 "pose hit pose equals new pose"(命中姿勢等於新姿勢)來創建一個新姿勢,並在這裡寫入位置的命中點。

  • And basically, for the rotation, I want to take into consideration the normal of the phase that we hit with our ray.

    基本上,在旋轉時,我要考慮到我們用射線擊中的相位的法線。

  • So, let's do quaternion dot look rotation minus hit dot normal.

    是以,我們來做四元數點視旋轉減去打點法線。

  • So, hit dot normal is, as I told you, the normal of the surface that we hit.

    是以,正如我告訴過你的,擊中點的法線就是我們擊中的表面的法線。

  • And if we call the quaternion dot look rotation, it will basically create a quaternion, so a rotation, based on this forward axis.

    如果我們調用四元數點看旋轉,它基本上會創建一個四元數,是以是基於前向軸的旋轉。

  • So, it will basically align everything to point to this direction.

    是以,它基本上會把所有東西都對準這個方向。

  • Oh, and here I just write minus because I want the rotation to face to the wall and not out of the wall, but actually as you prefer.

    哦,這裡我之所以寫減號,是因為我想讓旋轉面朝向牆壁,而不是離開牆壁,但實際上,這也是你想要的。

  • This is actually something that I do here because I'm going to need it in a minute.

    這其實是我在這裡做的事情,因為我馬上就需要它。

  • But for now, I'll just copy what I do.

    但現在,我只能照搬我的做法。

  • So, now that we've created a new pose, what we can do is create an anchor.

    現在,我們已經創建了一個新姿勢,可以做的就是創建一個錨點。

  • So, we can do for this var result equals anchor manager dot try add anchor async and give us an input or hit pose.

    是以,我們可以這樣做: var result equals anchor manager dot try add anchor async 並給出輸入或點擊姿勢。

  • And here is the important function, so the try add anchor async, which is basically adding an anchor at a certain position.

    這裡有一個重要的功能,即嘗試添加錨點異步,基本上就是在某個位置添加一個錨點。

  • But as you can see, this try and add anchor is a nothing function.

    但正如你所看到的,這個嘗試和添加錨點的功能什麼都不是。

  • Async means that we can wait for this function to finish.

    Async 意味著我們可以等待該函數完成。

  • And this is very important because adding an anchor is not instant, so it takes some time to process.

    這一點非常重要,因為添加錨點不是一蹴而就的,需要一些時間來處理。

  • And this can create some issues because if we want to spawn some prefab on the anchor, we need to wait for the anchor to be processed.

    這可能會產生一些問題,因為如果我們想在錨點上生成一些預製件,就需要等待錨點被處理。

  • So, a solution to await is simply to write await here at the front.

    是以,await 的解決方案就是在前面寫 await。

  • But as you can see, everything is underlined in red now because we actually need to write at the top async for this to work.

    但正如你所看到的,現在所有內容都用紅色下劃線標出,因為我們實際上需要在頂部寫入 async 才能使其生效。

  • And as you can see now, everything seems to work.

    正如你現在看到的,一切似乎都很順利。

  • And finally, with the result, this is not actually the AR anchor that we are getting with this result.

    最後,對於結果,這實際上並不是我們通過這個結果得到的 AR 錨點。

  • So, to get actually this anchor, we need to write bool success equals result dot try get result out var anchor.

    是以,要得到這個錨點,我們需要寫 bool success equals result dot try get result out var anchor。

  • And here is the anchor that is created if everything went smoothly and that we will be able to use in a minute.

    如果一切順利,我們就可以創建一個錨,我們馬上就可以使用它。

  • But now, with this spawn anchor, if I sum up everything, we first get the hit position.

    但現在,有了這個 "產卵錨",如果我總結一下,我們首先會得到命中位置。

  • We turn this hit position into a pose, which is basically a point in space.

    我們將擊中的位置轉化為姿勢,基本上就是空間中的一個點。

  • And from this point in space, we create an anchor, which takes some frame to process.

    從空間中的這一點出發,我們創建了一個錨,這需要一些框架來處理。

  • So, that's why we write await here to await this to work.

    所以,這就是為什麼我們要在這裡寫下等待,等待它發揮作用。

  • And then, once it has done the job to adding an anchor, we have a look at if it succeeds or not and get the output anchor at end.

    然後,一旦它完成了添加錨點的工作,我們就可以查看它是否成功,最後得到輸出的錨點。

  • And there we go.

    我們走吧。

  • Now, that's basically it.

    現在,基本上就是這樣了。

  • Let me save and go back to Unity.

    讓我保存並返回到 "統一"。

  • Okay.

    好的

  • And now that we are back inside Unity, we, of course, need to set up here the spawn anchor from re-select script.

    現在我們回到了 Unity,當然需要在這裡設置從重新選擇腳本生成錨點。

  • So, for the re-interactor, let's simply drag the xr-re-interactor from the same script.

    是以,對於重現器,我們只需從同一個腳本中拖動 xr-re-interactor。

  • And for the anchor manager, we can drag here the xr-origin.

    對於錨點管理器,我們可以在這裡拖動 xr-origin。

  • Now, let's do the same, but for the left re-interactor.

    現在,讓我們做同樣的事情,但針對的是左側的重新互動者。

  • So, drag the re-interactor over there and the xr-origin.

    所以,把 re-interactor 和 xr-origin 拖到那裡。

  • Now, everything should run smoothly.

    現在,一切都應該順利進行。

  • So, okay, by adding an anchor from the try-add-anchor async, what this will do is add an empty GameObject with the ar-anchor component to it.

    好了,通過在 try-add-anchor async 中添加錨點,這樣做的目的是添加一個帶有 ar-anchor 組件的空 GameObject。

  • But actually, you can override this prefab here with this anchor-prefab parameters.

    但實際上,你可以使用錨點預製參數覆蓋這個預製。

  • So, if we click here and search for anchor, then click on assets.

    是以,如果我們點擊這裡搜索錨點,然後點擊資產。

  • And if we click on this little toggle there, we should see the anchor transform, which is basically just a GameObject with three axes.

    如果我們點擊這裡的小開關,就會看到錨點變換,它基本上就是一個有三個軸的 GameObject。

  • I'm going to set this as an anchor.

    我要把它設為錨。

  • So, this should display when we interact with something.

    是以,當我們與某些東西交互時,就應該顯示這些內容。

  • Now, let's save and see if this works by building our game.

    現在,讓我們保存並製作遊戲,看看是否能正常運行。

  • Okay.

    好的

  • So, as you can see, everything still works.

    所以,正如你所看到的,一切都還能正常工作。

  • I have still this one cube, which is anchor, and this cube, which is not.

    我還有這個是錨的立方體和這個不是錨的立方體。

  • I have some array, which are coming out of my hands.

    我有一些陣列,正從我手中流出。

  • And if I press on the grip button, as you can see, it works.

    如果我按下手柄按鈕,如你所見,它就會工作。

  • We can spawn some anchor, which will be placed on our array.

    我們可以生成一些錨點,將其放置在我們的數組中。

  • And as you can see, this works on all surface.

    正如您所看到的,這對所有表面都有效。

  • And as you can see, the anchor have the correct orientation.

    正如你所看到的,錨的方向是正確的。

  • So, everything is perfect.

    所以,一切都很完美。

  • But of course, right now, we are just spawning just little gizmos.

    當然,現在我們只是在生產一些小玩意兒。

  • What we want is to anchor any content, not just this prefab.

    我們想要的是錨定任何內容,而不僅僅是這個預製件。

  • So, let me show you how we can do this.

    那麼,讓我來告訴你如何做到這一點。

  • Okay.

    好的

  • So, if we want to not just anchor the gizmo, but any prefab, what we want is to go back to the script.

    是以,如果我們不僅要錨定小工具,還要錨定任何預製件,我們就需要回到腳本。

  • And at the top, I'm simply going to write a public GameObject called prefab.

    在頂部,我只需編寫一個名為 prefab 的公共 GameObject。

  • There you go.

    這就對了。

  • And at this point, it is very simple.

    這一點,非常簡單。

  • If we have successfully created an anchor, what we can do is create a spawn prefab by instantiating our prefab.

    如果我們已經成功創建了一個錨點,那麼就可以通過實例化我們的預製件來創建一個生成預製件。

  • We can actually spawn it at the anchor.pose.position and at the anchor.pose.rotation.

    實際上,我們可以在錨點位置和錨點旋轉位置生成它。

  • Beautiful.

    真漂亮

  • And last but not least, we can also set the spawn prefab to have its parent to be the anchor transform.

    最後但並非最不重要的一點是,我們還可以將 spawn 預製件的父級設置為錨點變換。

  • So, this means that if the anchor change position, because the prefab that we are spawning is a child of the anchor, it will move with it.

    是以,這意味著如果錨的位置發生變化,由於我們生成的預製件是錨的子預製件,它也會隨之移動。

  • So, that's something really great.

    所以,這是一件非常了不起的事情。

  • Now, let me save, go back to Unity, go to the XR origin, and I'm going to here select this little button and click on None.

    現在,讓我保存,回到 Unity,轉到 XR 原點,然後選擇這個小按鈕,點擊 "無"。

  • Because instead of having a gizmo spawn, what I want to do is to spawn a custom prefab.

    因為我想做的不是生成一個小工具,而是生成一個自定義預製件。

  • And what prefab should we be able to place inside our world?

    我們應該在自己的世界裡放置什麼樣的預製件呢?

  • So, lucky for me, I have this magnificent image that I want to use inside my project.

    所以,我很幸運,我有了這張精美的圖片,我想在我的項目中使用它。

  • So, what I'm going to do is right-click, go to 3D object, Quad.

    所以,我要做的就是右鍵單擊,轉到 3D 對象,Quad。

  • This will create a 2D quad, as you can see.

    如圖所示,這將創建一個二維四邊形。

  • We can maybe scale it down a little bit and drag here the marvelous image that we have inside this quad.

    我們也許可以把它縮小一點,然後把這個四邊形中的奇妙影像拖到這裡。

  • As you can see, this will create also material and apply this image as a texture to the quad.

    如您所見,這也將創建一個材質,並將該影像作為紋理應用到四邊形上。

  • Now, I'm going to call this one Beautiful Painting.

    現在,我要把這幅畫稱為 "美麗的畫"。

  • I'm going to turn it into a prefab by dragging it inside our project folder and remove the Beautiful Painting by selecting it and pressing the Delete key.

    我將把它拖到我們的項目文件夾中,把它變成一個預製件,然後選擇 "美麗的繪畫 "並按下 Delete 鍵將其刪除。

  • Beautiful.

    真漂亮

  • Now, what's left is to go to the right and left ray interactor and drag our Beautiful Painting inside the prefab to spawn.

    現在,我們要做的就是轉到左右射線交互器,然後將我們的 "美麗繪畫 "拖入預製構件中。

  • This means that now, by doing this, we should have our Beautiful Painting spawning where our ray is interacting with the world.

    這就意味著,通過這樣做,我們應該在射線與世界互動的地方生成美麗的繪畫。

  • And this means that it will also be anchored in our world and be placed with our real environment and not based on the player.

    這意味著它也將立足於我們的世界,與我們的真實環境融為一體,而不是基於玩家。

  • So, one last thing to do and is, of course, to try and find out if this is working.

    所以,最後要做的一件事,當然是要試著找出這是否有效。

  • Okay, and here you go.

    好了,給你。

  • I'm in Mixed Reality right now and let's try to press on the grip button on the wall to see what happens.

    我現在就在混合現實中,讓我們試著按下牆上的手柄按鈕,看看會發生什麼。

  • And as you can see, it works!

    正如你所看到的,它很有效!

  • The marvelous painting is showing with the correct position and at the correct rotation.

    這幅奇妙的畫作以正確的位置和正確的旋轉方式展示出來。

  • But the big thing is that all of these are created and anchored to our real world.

    但最重要的是,所有這些都是根據我們的現實世界創造和確定的。

  • So, if, for example, I try to recenter the position, as you can see, you can see this cube which is moving because it is following the player's space.

    是以,舉例來說,如果我嘗試重新調整位置,正如你所看到的,你可以看到這個立方體在移動,因為它在跟隨玩家的空間移動。

  • But all of the stuff that we spawn and that we anchor stay the same.

    但我們催生的所有東西和我們錨定的東西都不會改變。

  • This is awesome!

    這太棒了!

  • And basically, this sums up this tutorial on how to anchor GameObject in Mixed Reality to our real world.

    基本上,這就是本教程關於如何將混合現實中的 GameObject 固定到現實世界的總結。

  • So, I hope that you enjoyed this first tutorial of 2024.

    希望大家喜歡 2024 年的第一篇教程。

  • Now, thank you for watching till the end and, as always, if you want to support my work and get access to the source code of all of my tutorials, you can join us on Patreon.

    現在,感謝您一直觀看到最後,如果您想一如既往地支持我的工作,並獲得我所有教程的源代碼,可以加入我們的 Patreon。

  • The link is in the description.

    鏈接在說明中。

What is up everyone and welcome to a new tutorial about Spatial Anchor.

大家好,歡迎閱讀關於空間錨的新教程。

字幕與單字
由 AI 自動生成

單字即點即查 點擊單字可以查詢單字解釋