Unity 2020 Virtual Reality Projects
上QQ阅读APP看书,第一时间看更新

Importing content

So far, we have shown you how to use Unity and be productive in creating a scene, but with pretty simple content. Normally, Unity is not a 3D-modeling or asset-creation tool. Rather (as the name Unity suggests), it's a unified platform for pulling together content from a variety of sources to assemble and program a game or experience involving animation, physics, rendering effects, and so on. If you are a 3D artist, then you may know how to create content in other programs, or you can find a plethora of models on the web.

Creating 3D content for VR

Unity offers some basic geometric shapes, but when it comes to more complex models, you'll need to go beyond Unity. The Unity Asset Store and many other sites have tons of amazing models. Where do they come from? Will you run into problems while importing them into Unity?

For example, Blender is a free and open source 3D animation suite (http://www.blender.org/) that you can use to make a model and then import it into Unity. Other popular software, for both hobbyists and professionals, includes ZBrush, 3DS Max, Maya, and Cinema4D. A detailed list of 3D modeling software can be found at https://en.wikipedia.org/wiki/List_of_3D_modeling_software.

Actually, there are content creation tools that run right inside the Unity editor. Polybrush and ProBuilder are two examples, bought by Unity Technologies in 2019 and provided free with Unity itself (we will be using both these packages in later chapters in this book). Many other in-editor tools can be found on the Unity Asset Store, for example, by searching modeling or painting tools (https://assetstore.unity.com/tools?category=tools%2Fmodeling%5Ctools%2Fpainting&orderBy=1).

In addition to traditional 3D-modeling software, there is a new generation of 3D design apps that let you directly create inside VR. Let's be honest: it's pretty awkward trying to use an inherently 2D desktop screen with a 2D mouse to form, sculpt, and manipulate 3D models. If only it could be more like real-life pottery, sculpture, and construction. Well, why not just do it directly in VR? Some of the growing number of VR 3D content creation tools include the following:

Let's see how to import assets from the store in the next section.

Importing from the Unity Asset Store

One terrific source is the Unity Asset Store (https://www.assetstore.unity3d.com/en/). Many asset packs are free, especially starter ones, with possible paid upgrades if you want more. These can be very useful, for instance, if you are looking for a few things to get your learning and experimental projects going.

As of Unity 2020.1, you are expected to browse and purchase assets from the Unity Asset Store website using your web browser. You can then find and import packages that you already own using the Package Manager (by going to Window | Package Manager). The Package Manager window with the My Assets filter selected is shown in the following screenshot:

Unfortunately (or fortunately), Unity keeps evolving, and some asset packages can get left behind. The Asset Store is filled with popular packages that were built for older versions of Unity and may have problems importing into the current version, possibly including the following:

  • Unsupported shaders
  • Obsolete C# script APIs

In this book, we created our Unity project using the Universal Render Pipeline, derived from the new Scriptable Render Pipeline (SRP) system that replaces the built-in render pipeline (https://docs.unity3d.com/Manual/ScriptableRenderPipeline.html). As we'll see, Unity provides tools to help upgrade materials for your Render Pipeline (RP). Sometimes that works, but sometimes it needs manual adjustment, as we'll show you next.

The Unity Scripting API (programmer interface) also evolves. When importing old scripts, Unity will try to automatically update them to work with the current version. Sometimes it cannot. Usually, if an API function has changed, Unity will give a warning that it is deprecated, and then eventually it will become unsupported after a number of Unity updates. You may find assets on the Asset Store that have not been updated and will throw errors when you try to import. Sometimes, this incompatibility makes the package unusable and obsolete. But sometimes a minor script edit can take care of the problem without having to chase down the developer and request a fix.

Let's run through a couple of these scenarios now, while at the same time learning more about Unity.

A skybox is a panoramic texture drawn behind all objects in the scene to represent the sky or another vista at a far distance. You can find many skyboxes to use in your projects; one of my favorites is the free Wispy Skybox from MUNDUS. Let's add it to our project now:

  1. In your browser, go to the Asset Store (https://assetstore.unity.com/) and search for Wispy Skybox, or go directly to https://assetstore.unity.com/packages/2d/textures-materials/sky/wispy-skybox-21737.
  2. Click Open in Unity.
  3. In Unity, the Import Unity Package dialog box will pop up, with all the contents selected. Click Download (if present), and then click Import.
  4. Check your console window for errors (by going to Window | General | Console). I see none, so that's good.
  5. Open the scene's Lighting window (by going to Window | Rendering | Lighting).
  6. Select the Environment tab at the top of thewindow.
  7. At the top is a slot for Skybox Material. Click the doughnut icon, which opens the Select Material dialog.
  8. Search using the string wispy.
  9. Select one of the skyboxes to use in the scene.

Voila! That's good.

Next, let's import a model with a relatively high-quality PBR material. For fun, let's add the FREE Snowman from Angry Mesh:

  1. In your browser, go to the Asset Store (https://assetstore.unity.com/) and search for free snowman, or go directly to https://assetstore.unity.com/packages/3d/props/free-snowman-105123.
  2. Click Open in Unity.
  3. In Unity, Import the package.
  4. In the Project window, go to the Assets/ANGRY MESH/Snowman/Prefabs/ folder.
  5. Drag the Snowman_01 prefab into the scene.

Oh no! It looks all magenta. That's Unity telling you that the object's materials are missing or not compatible with the current render pipeline:

To fix it, we'll try converting the imported materials to our RP:

  1. Select Edit | Render Pipeline | Universal Render Pipeline | Upgrade Project Materials to UniversalRP Materials, then click Proceed:

There we go. Now he looks happier:

If you were to open the Demo_Snowman demo scene that comes with the package, you will see three snowmen in a terrain of pink snow! The upgrade tool didn't work on the terrain. Let's see why:

  1. Save your current work by going to File | Save.
  2. Go to File | Open Scene and select the scene Assets/ANGRY MESH/Snowman/Scene/Demo_Snowman.
  3. Select the Terrain GameObject.
  4. In Inspector, click the gear icon to open its Terrain Settings (you may need to first click Active to enable this object if the gear icon is disabled). Note that its Material property is called Default-Terrain-Standard.

As we've seen, Unity has a number of built-in default materials—for example, when you create a new object, its default material is Lit with the URP/Lit shader, because that's compatible with our current render pipeline. The default material with the imported terrain is not compatible with our render pipeline. We can create new material instead by going through the following steps:

  1. In the Terrain's Inspector, next to the Material slot, click the Create... button.
  2. Name it Snow Terrain Material.
  3. Set its Shader to Lit (Universal Render Pipeline/Terrain/Lit).

There, that works! Although we're not using this terrain in our scene, it was worthwhile seeing how to drill down into the game object properties to fix the unsupported shader that it was using.

We will not be doing much with terrains in the projects in this book. To learn more about the Unity Terrain Engine, see https://docs.unity3d.com/Manual/terrain-UsingTerrains.html.

Next, let's look at some assets that we will definitely be using in the projects in this book.

Using Unity Legacy Standard Assets

Several of the chapters in this book use a variety of assets that Unity has provided as a Standard Assets package for a long time; however, while still useful and fun, the package as a whole has grown stale and unsupported. Since Unity 2018, it was removed from the standard Unity install and became available on the Asset Store instead. We're still going to use this package, but we'll need to make some adjustments for compatibility with Unity 2019.3 or later and the Universal RP.

A copy of a subset of the Standard Assets that we will be using, already converted to be compatible with Unity 2019 or later, and the Universal RP, is also provided as a download with the files for this book. You can import that package in lieu of following the steps in this section.

You can import the entire package or a subset as described in the following steps:

  1. In your browser, go to the Asset Store (https://assetstore.unity.com/) and search for Unity Standard Assets, or go directly to https://assetstore.unity.com/packages/essentials/asset-packs/standard-assets-for-unity-2017-3-32351.
  2. ClickOpen in Unity, click Download, and then click Import.
  3. In Unity, theImport Unity Packagedialog box pops up, with all the contents selected.
  4. Uncheck the SampleScenes folder.
  5. Uncheck the Standard Assets/2D folder.
  6. Uncheck the Standard Assets/Cameras folder.
  7. Uncheck the Standard Assets/Characters/FirstPersonCharacter folder.
  8. Uncheck the Standard Assets/Characters/RollerBall folder.
  9. Uncheck theStandard Assets/Vehiclesfolder.

The import box with items that we don't need for this book is shown in the following image:

Import the assets, then correct any import errors as follows:

  1. ClickImport.
  2. Check your Console window for errors by going to Window | General | Console.
  3. There may be numerous import errors, but most of them will have been corrected by Unity. Click the Clear button (in the top-left of the Console window).
  4. The remaining errors need to be resolved manually. I see two script files with obsolete references. We could update the scripts rather easily, but since we will not need time at all, they can just be deleted.
  1. Clicking an error message will take you to the offending file in the Project window. Locate the file, right-click it, and select Delete. At the time of writing, the broken files will be as follows:
    • Assets\Standard Assets\Utility\ForcedReset.cs
    • Assets\Standard Assets\Utility\SimpleActivatorMenu.cs
  2. The import can now continue. More errors may be found and fixed by Unity. Click Clear again.
Generally, you should be cautious in deleting assets from the Assets folder, as there is no way to undo it. This is another good reason to use a version control system, such as Git, to let you recover from errors by going to a previously committed save point. In the case of imported asset packages, if you inadvertently delete or modify a file, you can reimport the original package.

That should complete the import. Next, let's attempt to convert materials to the current render pipeline:

  1. SelectEdit | Render Pipeline | Universal Render Pipeline | Upgrade Project Materials to UniversalRP Materials and then clickProceed.

You can browse the warning messages, but don't worry about them; we'll find incompatible materials soon enough.

One of the models that we plan to use soon is the Ethan humanoid model. Let's add him to the scene:

  1. In the Project window, drill down to the Assets/Standard Assets/Characters/ThirdPersonCharacter/Prefabs/ folder.
  2. Drag the ThirdPersonController into the Scene.
  3. You may want to turn him around so that he's facing the camera by going to Transform | Rotation | Y: 180.

Say hi to Ethan!

Ethan has a third-person control script that lets you control his animation with user input. We will work on VR input devices in later chapters, so for now, let's try it out with just the keyboard:

  1. Click on the Play icon at the top of the Unity window in the center to start your game.
  2. Use the W, A, S, and D keys to move him around. Run, Ethan! Run!
  3. Be careful not to let him fall over the edge of our playground, or he'll fall down forever.
  4. Click on the Play icon again to stop the game and return to edit mode.

Other than creating geometry inside Unity and getting asset packages from the Asset Store, content is otherwise imported from other graphics software.

Importing models in supported formats

Unity has built-in support for reading and importing models in a few generic file formats, including .fbx and .obj. Most 3D-modelling software can export these files. No extra software or licenses are required to use these files. In general, you should try to obtain 3D models in .fbx or .obj file format.

Unity also supports importing models in a number of proprietary formats, provided that you have the corresponding applications that edit these files on your development machine. These include Autodesk 3ds Max (.max) and Blender (.blend). This can be an advantage if you or your team is actively working on 3D models with one of these applications, and you have the software licenses to install it on your system (if needed). But you must have a copy of the application on your own system for Unity to know how to import the files. Not having to export files to .fbx, for example, before using them in Unity saves a step, especially if the models are still being developed and modified. Unity can import proprietary files from the following 3D-modelling software (from the Unity Manual, https://docs.unity3d.com/Manual/3D-formats.html):

  • Autodesk® 3ds Max®
  • Autodesk® Maya®
  • Blender (a free and open source 3D application)
  • Cinema4D
  • Modo
  • LightWave
  • Cheetah3D

For models that have been imported in Unity, you can modify their Import Settings and reimport as needed. The following is a screenshot of the import settings for the snowman FBX file we used earlier—for example, you can change the Scale Factor and Units if the source file was built at a scale other than Unity's 1-unit equals 1-meter scale. There are settings to select what to import from the file's scene, the mesh, and geometry.

Depending on the capabilities of the model, the Rig, Animation, and Materials tabs provide additional import parameters. If you change settings, click Apply to reimport the object with the new settings:

When the source model has materials, you can instruct Unity to use those Materials automatically, or you have the option to extract the Materials into separate Unity Material files that can be modified and adjusted like any other Material in Unity.

Additional details and tips for import models into Unity can be found at https://docs.unity3d.com/Manual/ImportingModelFiles.html.

While Unity provides a lot of import parameters, if you have access to the original modeling software, you can also gain control of the file that is using that software's export settings—for example, in Blender, its default coordinate system has the z-axis up, while Unity's is y. This can be compensated by rotating it once inside Unity, or you can adjust Blender's FBX export settings as shown in the following screenshot:

Next, we'll consider the workflows related to creating and importing 3D assets.

Round-trip geometry workflows

All this said, Unity is increasingly more capable of creating and modifying geometry within the Unity Editor itself—for example, the ProBuilder package that is integrated into Unity (which we'll use in Chapter 10, Exploring Interactive Spaces) is useful for white-boxing (prototyping) level designs and creating simple buildings with walls, passageways, and windows. There are many other third-party tools to be found on the Asset Store.

Unity is supporting advanced workflows with the FBX Exporter package, available in Package Manager, which provides round-trip workflows between Unity and other 3D software. With this package, GameObject hierarchies, animations, lights, and cameras can now be exported from Unity and shared with other applications.

The tool also allows you to preconfigure an integration with a specific 3D-modeling software. Presently, only Autodesk Maya and 3ds Max are supported, but more may be available by the time you read this. For details and instructions on using the FBX Exporter, see https://docs.unity3d.com/Manual/HOWTO-exportFBX.html. The package is presently still in preview, but is very promising.

As you can see, building a VR project requires you to not just understand how to use the many features of Unity itself, but how to import artwork and other assets from other sources into your project.