In this walkthrough, we will use Unity to build a stand-alone Windows application from a SketchUp model. It will be a rather simple application, just featuring a keyboard-controlled camera that allows for steering the camera in a 3D scene with a single building.
Close Unity for now and download the Old Main model we are going to use here as a SketchUp .skp file. [1] You should still have the Unity project “MyFirstUnityProject” from the beginning of the lesson with just an empty scene ("SampleScene" in the scenes folder). This is what we will use for this walkthrough. The first task we have to solve is to export the 3D model from SketchUp into Unity.
Note: If the Old main model is too heavy for your personal computer, you can use the model for Walker Laboratory [2].
Unity allows for importing 3D models in different formats. Typically the files with the 3D models can be simply put into the Assets folder (or some subfolder of the Assets folder) of a project and then Unity will discover these and import them so that they show up under Assets in the project. Until very recently, there was no support to directly import SketchUp .skp files into Unity and, therefore, 3D models had to be exported from SketchUp in a different format as shown in the figure below.
However, direct import of SketchUp .skp files is now possible. So we are simply going to put the downloaded .skp file into the Assets folder. Before that, please make sure that neither Unity nor SketchUp are currently running! Then perform the following steps:
If the above method does not work for your case, you can always simply add the skp model into the editor by dragging and dropping it into the folder structure. You may have noticed that the Building object we have imported looks rather dull and is missing lots of textures and materials. To fix this problem, we need to extract all the materials and textures from the prefab of the model so that Unity knows how to map them. Select the prefab of the model you have imported into your folder. You can see that in the inspector menu it is detected that this is a SketchUp model, but all the materials for the individual components of this model are missing (labeled as none).
To fix this, click on the “Extract Textures …” button in the inspector panel. Create a new folder in the pop-up window and rename it to “Textures”, and then click on “Select Folder”. Do the same for Materials by clicking on the “Extract Materials …” and creating a new folder called “materials”. Once done, the model should have all the materials and textures added to it.
The next thing we are going to do is place the Old Main model in the Scene and make a few more changes to the Scene.
5.3 If you select the “MainCamera” and look at the Preview (or try out Play Mode), you will see that this is now quite a bit away from the building. So let us place the camera such that we have a nice view of the building from the sunny side and the building roughly fills the entire picture. Remember that one way to do this is by changing the Scene view to the desired perspective and then use “Align With View”.
To make things at least a little bit interactive, we now want to be able to move the camera freely around in the scene using mouse and keyboard. Such interactivity is typically achieved by adding script components. So in this case, we want to attach a script to the “MainCamera” object that reads input from the mouse and keyboard and adapts the camera position and rotation accordingly.
We won’t teach you scripting with C# for this functionality because we can reuse some existing script. A resource for Unity scripts and extensions outside of the Asset Store is the Unity3D Wiki. As it happens, we find a script published under the Creative Commons Attribution-ShareAlike license there to realize the free-flight camera control we are looking for. The script can be downloaded here [1].
Let us have a very short look at the code. If you have some programming experience, you will recognize that there are a bunch of variables defined early on in the script (public float … = …) that will determine things like movement speed, etc. of the camera. These variables will be accessible from the Unity Inspector Window once we have attached this script to our camera. The actual functionality to control the camera is realized in the function Update() { … } . GetAxis(…) is used to read information from the mouse, while GetKey(…) is being employed to check the status of certain keys on the keyboard. The content of the variable called “transform” roughly corresponds to what you always see in the Transform section of the Inspector Window and is used here to change the camera position and rotation based on the user input. The Update() function of a script that is attached to a GameObject in the scene will be called for every tick (= rendered frame) when the project is executed. So in Play Mode, the keyboard and mouse will be constantly checked to achieve a smooth control of the camera.
Here are the steps to add this code as a script asset to our project and then attach it to the camera object in the Scene: