Build Minecraft in Unity Part 1

Today we start a series of tutorials oriented towards the research and building of a minimal Minecraft-like game while exploring different facets of the Unity game engine. As this is an introductory tutorial, and because of the need to simplify (for didactic purposes), the algorithms presented in Series 1 are not in any way optimal, nor the structures of the objects used in the game are efficient. Presenting a trivial, fast way to build a minimal Minecraft-like game offers an introductory experience to people just stating to learn Unity, and in the same time, it arises the imperative need for optimization and efficiency. Over the course of the following tutorial series we will cover different implementations with higher degree of complexity.

I recommend to you, The Reader, to state any issues in the comments below. I encourage you to debate anything and everything regarding the contents of this tutorial, for the benefit of the community. Your contribution will help make these tutorials better!

52_final

A taste of things to come

Part I. Starting up

Download the latest version of Unity here.

Download textures needed in this tutorial:

First, let us familiarize a bit with Unity. When running Unity for the first time, the Project Wizard window will pop up. You can import one of the built-in Unity packages. The packages are collections of different project files (code, models, audio files, textures etc.) stored in a hierarchical structure, encapsulated in .unitypackage files. Packages can be exported in and from any Unity project. This is an easy way to transfer files between projects, so as to maintain their hierarchy in the project. At the moment, we don’t need to import any package.

1_project_wizard

Unity Project Wizard window

After setting the new project path press Create to finish the project creation. If you opened Unity and created a project beforehand, you can create a new project by clicking File -> New Project to access the Project Wizard window.

2_file_newproj

Creating a new Unity project

The Unity interface is divided in several useful tabs:

  • The Project tab contains all the resources that are used in the Unity game. For tidiness, it is recommended that these files should be placed in folders with appropriate names (Materials, Textures, Models, Prefabs etc.). This window mirrors how files are stored on the hard disk, but it is very important that any changes to the project files should be made through the Project tab and not directly through the file explorer, so that you don’t risk irreversible linking loss between files.
  • The Console tab displays useful debugging messages – if you have some minimal programming experience you know why these are useful.
  • The Inspector tab shows different components contained in objects from the Hierarchy or the Project tab – any editable components can have their values modified here.
  • The Hierarchy tab shows a list of the instance names of all the objects contained in the current scene.
  • The Scene tab shows the game world from an arbitrary perspective (different from the main camera perspective). The position and orientation of the Scene camera can be changed by using the W,A,S,D keys, while pressing the right mouse button.
  • The Game tab shows exactly what happens when you start the game, from the main camera point of view. Also the Scene tab can display a grid whilst the Game tab doesn’t.

You can arrange them in any way you wish by dragging and dropping them in the desired places.

3_fullwindow

The Unity interface

Any objects or functionality added to you current 3D environment can be saved in a scene file (with a .unity extension). You can think of scenes as similar to game levels. The Unity developer has the freedom to store the game levels (when necessary) in separate scene files and to load them by code at runtime. A project can contain multiple scenes. To save the current scene press File -> Save Scene / Save Scene as… and then type a name in the explorer window.

4_savescene

Saving a Scene

Save it in the Assets folder (the root of all files in a Unity project).

5_savescene2

Naming a Scene

If you open the Assets folder in the Project tab you can see the newly created scene file. Here, right click and create three new folders, titled Code, Materials and Textures like in the images below.

6_createfolder

Creating a New Folder

7_folders

Folders that we will need in our project

Now we are ready to start! Drag and drop the cube and skybox textures into the Textures folder.

8_textures

Cube and skybox textures imported in the project

Next, enter the Materials folder and create four materials named:

  • SkyboxMaterial
  • BottomMaterial
  • SideMaterial 
  • TopMaterial

Materials add color to 3D objects by running GPU-processed programs called Shaders. More Information about Unity Materials and Shaders here. The last three materials mentioned will be applied on the sides of the cube we will create in Part 2.

9_createmat

Creating a new Material

10_materials

Materials for the skybox and cube faces

Left click on BottomMaterial. In the Inspector tab, click on the Select button from the Texture component of the Material, then, from the new window, pick the texture titled bottom.

11_material_no_texture

Material viewed in the Inspector tab

12_picktex

Selecting a texture component

Next, pick the appropriate textures for SideMaterial and TopMaterial, like in the edited image below.

13_voxelmaterials

Cube materials with loaded texture components

[OPTIONAL] For aesthetic reasons, if you want to make the background resemble a beautiful landscape, you can render a skybox. For this purpose we created a skybox material onto which we will load the six remaining textures from the Textures folder.

Left click on SkyboxMaterial. In the Inspector tab, near the Shader label, left click on the drop down list and pick RenderFX -> Skybox. These are built-in Unity Shaders, that implement basic shader functionalities (Blinn-Phong reflection model, bump mapping, reflections, transparency etc.) and a few advanced ones like parallax mapping. The complete shader source code can be downloaded from here. Also, you can write your own custom shaders and add them to your project (more details about writing Unity shaders in a future tutorial).

14_skyboxmat

Choosing a shader used for skybox rendering

Next, similar to the diffuse shader from the materials assembled above, we need to load the six skybox textures in their appropriate fields.

15_skyboxtexturesmat2

Choose each skybox texture according to the Material component name

Now we need to render this skybox in our scene. Go to Edit -> Render Settings. In the Inspector tab, at the Skybox Material label, click on the small circle on the right and pick SkyboxMaterial from the existing project materials.

16_renderskybox2

Specifying the skybox material in RenderSettings

If we look close enough, we can observe the edges of the skybox cube. This issue can be avoided by clamping the textures. Basically, when a texture is clamped, the edge pixels are repeated, so as fill with color the edges of the model on which they are mapped on. More information about texture clamping here.

17_unclamp

Typical unclamped texture behaviour

Go to the Textures folder in the Project tab, select all the images and at the Wrap Mode label, select Clamp from the drop down list and press Apply.

18_clamp

Setting the Wrap Mode for all the project textures

 Done! In Part 2 we will create a textured cube using separate quads.


blog comments powered by Disqus