D I G T E K

Loading...

Nullam dignissim, ante scelerisque the is euismod fermentum odio sem semper the is erat, a feugiat leo urna eget eros. Duis Aenean a imperdiet risus.

Unity 3D is a game engine. You will be writing code that runs inside of this game engine. So, you’re writing software that’s run inside of another piece of software. What this means is that your code operates a layer further from your computer’s hardware resources.

NOTE: What do I mean by further from the computer hardware resources? This is where the term “highlevel programming” comes from. At the lowest level is the actual computer hardware. Programmers at this level are building silicon chips.

At one level above the silicon are things such as bus and motherboard where everything is either plugged into or soldered on. At another level above those are components that tell the motherboard what to do when power is turned on, how to look for data storage to launch operating systems, and just about everything else that happens when the computer is turned on.

Once this level finds an operating system, it hands over the tasks to the lowest level of your computer operating system where drivers and other hardware-to-software interfaces come into play to get your computer running. Once the computer gets booted and your operating system is properly talking to all the hardware components, it is ready for software applications to start running. At this level your software is usually not talking to any computer hardware directly, but it is talking to the software that runs the operating system.

Unity 3D is a software that talks to the operating system. When you write your game software your code usually talks directly to Unity 3D, but not to the operating system or computer hardware. Your code is being run with many other layers of software between you and the hardware. Similar to so many mattresses between you and the floor, there is more padding between you and any of the complexities that run your computer.

When your code is compiled, it turns into a machine language. The details here are simplified as there’s a bit more going on under the hood than what we can explain here. The game code is made up of many different objects called classes. Classes are composed of data (also known as variables), logic (also known as functions or methods), and a name (also known as an identifier).

Functions and data work together inside of Unity 3D to make your game experience come to life. This coordinated effort can be packaged and turned into a new program that you can publish to any number of computer platforms including browsers, game consoles, and mobile devices.

C# has a few simple parts that we’ll be studying later in this chapter. As a part of an exercise, we’ll have to learn how to read a bit of code. Open the LineNumbers.cs class by double clicking on it in the Assets directory of the BookContents/Chapters/Chapter2 project.

An underappreciated part of reading code is the line number. These numbers are found to the left of the code.

Each line of code is numbered starting with 1 at the top and ascending as you scroll down. Here we can see that lines 1–3 of the code are required to access libraries or software that has already been written by many engineers to empower your code with a vast wealth of software.

At line 5, you’ll find what is called a class declaration. This serves as a good overview of what to expect in the remaining chapters, so don’t worry if this seems like a lot of unfamiliar information.

Continuing down you’ll find some public variables at lines 7 and 8. At line 10, you’ll find a comment describing the use of void   Start(), which is the first line of code declaring a function. Inside the function at line 12, you’ll find a statement that will be executed when the Start() function is called.

At lines 16–18, you’ll find another function named Update() that doesn’t do anything right now, and at line 20, you’ll find another function named MyFunction(). At line 24, you’ll find the closing curly brace that closes the LineNumbers class.

When Visual Studio comes across an error, the error list indicates both the type and the line the error appears. Place a random letter in the code and Visual Studio will be quick to indicate something is out of place.

2.6.1 Creating and Assigning a New C# File: Now It’s Your Turn

Let’s start with the BookContents/Chapters/Chapter2 project we’ve been working with.

C# files are basic text files. It is possible to open Notepad or any other simple text editor and create a new file from scratch, but it is better to have Unity 3D create a clean, error-free starting file to work from. Right click on the Project panel and select Create→C# Script from the pop-up menu.

This will create a new C# file in the Assets directory in your project. We’ll go into organizing these files with a bit more logic later, but for now we’ll just leave it in the Assets directory. Alternatively, you can use the menu and select Assets→Create→C# Script to create a script in the Assets directory.

NOTE: You can resize the file display with the slider on the lower right. This will make longer file names easier to read.

This will create a file called NewBehaviourScript, which needs to be renamed as MyScript to follow along. If you unselect the file, it’ll be created as NewBehaviourScript. This means you’ll need to change the file name both in the Assets directory and in the class once it is opened in Visual Studio. We will cover how this is done in a moment. It’s not necessary to add the .cs at the end of the file name when using the Asset Panel in Unity 3D; the Editor will keep track of the file types and add the correct file extensions.

To the right in the Inspector panel, you’ll notice some code shown in the panel. This is a preview of the content in your new file. Selecting different objects both in the Assets panel and in the game scene will prompt different options to appear in the Inspector panel.

Leave a Reply