4.与多个用户共享对象运动4. Sharing object movements with multiple users

本教程介绍如何共享对象的运动,使共享体验的所有参与者可以展开协作并查看彼此的交互。In this tutorial, you will learn how to share the movements of objects so that all participants of a shared experience can collaborate and view each other's interactions.

目标Objectives

  • 配置项目以共享对象的运动Configure your project to share the movements of objects
  • 了解如何生成基本的多用户协作应用Learn how to build a basic multi-user collaborative app

准备场景Preparing the scene

在本部分,你将通过添加教程预制件来准备场景。In this section, you will prepare the scene by adding the tutorial prefab.

在“项目”窗口中,导航到“资产” > “MRTK.Tutorials.MultiUserCapabilities” > “预制件”文件夹,然后将“TableAnchor”预制件拖动到“层次结构”窗口中“SharedPlayground”对象上,以将其作为 SharedPlayground 对象的子项添加到场景 :In the Project window, navigate to the Assets > MRTK.Tutorials.MultiUserCapabilities > Prefabs folder and drag the TableAnchor prefab onto the SharedPlayground object in the Hierarchy window to add it to your scene as a child of the SharedPlayground object:

选中新增的 TableAnchor 预制件的 Unity

配置 PUN 以将对象实例化Configuring PUN to instantiate the objects

在本部分,你将配置项目以使用在入门教程中创建的“探测车浏览器”体验,并定义在哪里将其实例化。In this section, you will configure the project to use the Rover Explorer experience created during the Getting started tutorials and define where it will be instantiated.

在“项目”窗口中,导航到“资产” > “MRTK.Tutorials.MultiUserCapabilities” > “资源”文件夹。 In the Project window, navigate to the Assets > MRTK.Tutorials.MultiUserCapabilities > Resources folder.

在“层次结构”窗口中,展开“NetworkLobby”对象并选择“NetworkRoom”子对象,然后在“检查器”窗口中,找到“Photon 房间(脚本)”组件,并按如下所示对其进行配置:In the Hierarchy window, expand the NetworkLobby object and select the NetworkRoom child object, then in the Inspector window, locate the Photon Room (Script) component and configure it as follows:

  • 向“探测车浏览器预制件”字段分配来自“资源”文件夹的“RoverExplorer_Complete_Variant”预制件 To the Rover Explorer Prefab field, assign the RoverExplorer_Complete_Variant prefab from the Resources folder

配置了部分 Photon 房间组件的 Unity

在“NetworkRoom”子对象仍处于选中状态的情况下,在“层次结构”窗口中展开“TableAnchor”对象,然后在“检查器”窗口中,找到“Photon 房间(脚本)”组件,并按如下所示对其进行配置: With the NetworkRoom child object still selected, in the Hierarchy window, expand the TableAnchor object, then in the Inspector window, locate the Photon Room (Script) component and configure it as follows:

  • 向“探测车浏览器位置”字段中分配来自“层次结构”窗口的“TableAnchor”>“Table”子对象 To the Rover Explorer Location field, assign the TableAnchor > Table child object from the Hierarchy window

配置了 Photon 房间组件的 Unity

尝试带有共享对象移动的体验Trying the experience with shared object movement

如果现在生成了 Unity 项目并将其部署到 HoloLens,然后返回到 Unity,并且在应用运行于 HoloLens 上时按“玩游戏”按钮以进入“游戏”模式,那么,当你在 HoloLens 中移动对象时,你将看到对象在 Unity 中移动:If you now build and deploy the Unity project to your HoloLens, and then, back in Unity, press the Play button to enter Game mode while the app is running on your HoloLens, you will see the object move in Unity when you move the object in HoloLens:

动画显示了具有联网对象的 Unity

祝贺Congratulations

你已成功将项目配置为同步对象移动,因此用户可以在其他用户移动对象时看到对象移动。You have successfully configured your project to synchronize object movements so users can see the objects move when other users move them. 在下一教程中,你将实现协调物理世界体验的功能。In the next tutorial, you will implement functionality to align the experience in the physical world. 这将确保用户可以看到各自的实际物理位置,让对象在所有用户看来都处于同一物理位置并同步旋转。This will ensure the users see each other in their actual physical location, and so the objects appear in the same physical position and rotation for all users.