Unity implements multi-scene switching under VR based on PICO4 device (very detailed, this is the only csdn company with all the cases)

1 Create a unity project

1.1 Open Unity Hub

Open Unity Hub, first click on the project column on the left, then click on the new project in the upper right corner. After the creation window pops up, select the URP template, customize the project name and location, uncheck version management, and finally click Create Project (note :The unity version I have here is 2020.3.37f1c1 LTS).
Insert image description here
Insert image description here

1.2 Create project

After clicking Create Project, when you create a project for the first time, you need to wait for a while to load. The initial page of the project is as follows
Insert image description here

2 configuration items

2.1 Configure plug-in and import SDK

1. Click Package Manager under Windows and follow the steps shown in the figure to download and install the two plug-ins, XR Interaction Toolkit and XR Plugin Management (the plug-in will restart the project after installation, don’t panic). When installing XR Interaction Toolkit, there will be A pop-up box appears, click yes->no thanks to introduce the starter assets resources (the demo contains the configured VR controller parameters).
Insert image description here
Insert image description here
Insert image description here

2. Follow the steps as shown below, click +, and then click Import from local disk to import pico's SDK. The download point for picoSDK jumps here .
Insert image description here
Insert image description here
Insert image description here
Insert image description here

2.2 Set up the scene

1. First right-click to delete the Main Camera that comes with the project, because the built-in camera cannot see anything in the VR device (such as pico4). You need to use the camera under XR. Right-click on the blank area of ​​the project and select XR XR Origin(VR).
Insert image description here
2. The most critical step is here. Click on the starter assets resource we just imported (under the XR Interaction Toolkit of Samples), press and hold the left button of the XRI Default Left Controller without letting go, and drag it to the XR of the LeftHand Controller. Just release it on the Controller (Action-base). In the same way, drag the XRI Default Right Controller to the XR Controller (Action-base) of the RightHand Controllerd and release it. This step is to use the controller parameters configured in the official demo directly. There is no need to write a script from scratch, which saves time.
Insert image description here

2.3 Writing scripts

1. In the Project window, right-click the Scripts directory, create->C# Script, customize the script name, here I call it SceneSwitch, double-click to open it for editing, the code is as follows
Insert image description here
,

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class SceneSwitch : MonoBehaviour
{
    
    

    // Use this for initialization
    void Start()
    {
    
    

    }

    // Update is called once per frame
    void Update()
    {
    
    

    }
    public void jump0()
    {
    
    
        SceneManager.LoadScene(0);//跳转到场景SampleScene
    }
    public void jump1()
    {
    
    
        SceneManager.LoadScene(1);//跳转到场景Scene1
    }
    public void jump2()
    {
    
    
        SceneManager.LoadScene(2);//跳转到场景Scene2
    }
}


2. Create a new scene, right-click the Scene directory, create->Scene, create two new empty scenes Scene1 and Scene2, double-click Scene1, and add a cube to Scene1. Specific operations: In the HIerarchy window, right-click the blank area, 3D Object- >Cube is enough to distinguish between two scenes. (Both scenarios require repeating steps 2.2)
Insert image description here

2.4 PICO project configuration

2.4.1 Implementation ideas

In order to switch scenes, we need to use something to implement this function. The following idea is used here.
Create a Canvas under the Main Camera of XR Origin, and create some UI components on it, such as button components that can interact with VR handle rays. and text components that can be used for text display, and then mount the scripts in 2.3 to these buttons. Click the button with the handle ray in the VR environment, and the button will execute the corresponding script function on the mount, and scene switching can be realized. function. (The purpose of creating a Canvas under the Main Camera of XR Origin is to prevent the UI interface from changing as the field of view of the VR device changes, and to keep it fixed in front of the field of view)

2.4.1 Implementation steps

1. Create a Button (Canvas is automatically created when a Button is created). See the steps below.
Insert image description here
2. Rename the button to down, which means switching to the next scene, and adjust the position and size of the Button (you can also adjust it according to your personal preferences). ), the steps are as follows,
2.1. Drag the button to the lower left corner
Insert image description here
2.2 Modify its properties, as follows
Insert image description here
3. Mount the script to Canvas, the steps are as follows
Insert image description here
4. Mount the function jump1 that loads the next scene to the down button, as follows
Insert image description here
Insert image description here
5 , At this point, we have implemented a function to switch to the next scene. In the same way, I can also install the above steps and add an up button (switch to the previous scene). The steps are the same as above. Just modify the function to jump2. Can.
We repeat the same operation in Scene1 and Scene2 to complete the loop switching of the three scenes, namely SampleScene->Scene1->Scene2->SampleScene->Scene1->Scene2->SampleScene->…

3 Project packaging and running deployment

3.1 PICO project operating environment configuration

The configuration in this step is the project configuration officially provided by pico to ensure that your application can successfully access the XR capability, complete the construction and run normally on the PICO device. For specific detailed steps, please see the quick start of pico official documentation, click here to jump. (Steps 4 and 5 do not need to be configured)

3.2 Package and run

You can follow the steps below to package and run, or you can refer to the official running example and click here to jump.
Once the scene is created, let's package it as an application (that is, an .apk file) that can run on the PICO device.
Step 1: Switch the development platform.
The target compilation platform of the PICO XR application is the Android platform.
1. From the upper menu bar, select File > Build Settings.
2. In the Build Settings window, select Android from the Platform list.
Insert image description here

3. Click Switch Platform. If the button changes to Build, it means the platform switch is successful.
Step 2: Package and run the scene.
Use the Build tool that comes with the Unity editor to build the XR scene into an APK file that can be run on the PICO device. The steps are as follows:
1. Use USB data cable to connect the PICO device and PC.
2. In the Build Settings window, click Add Open Scenes to add SampleScene as a scene to be built. Click Scene1/Scene2 again, switch to the scene, File->Building Settings, and click Add Open scenes to add the scene to the next packaged apk file.
Insert image description here

3. Set Run Device to All Compatible Devices or the model of the PICO device connected to the PC.
Insert image description here
4. Click Build And Run. You will see the Build Android window.
5. Select the storage location of the APK file. Unity starts compiling the application. After compilation is completed, the PICO device will automatically run the APK file.

4 Conclusion

This article is mainly about the technology used in a small VR project in the laboratory before. In order to facilitate the follow-up work of the junior students in the laboratory, this article is written for reference and learning. Regarding some follow-up technologies of this project, such as optimization model , adjust the scene ground plane height and UI panel distribution, beautify the VR scene, and achieve cross-language communication between multiple devices (based on udp/tcp communication). Server side (send/receive data): PC side, pico4, computer client (send/receive data and display data in the form of graphical visualization), I will continue to produce corresponding articles.
If you encounter any questions, please leave a positive message in the comment area and we will reply as soon as you see it.

Guess you like

Origin blog.csdn.net/qq_52431815/article/details/131978583