A quick introduction to Unity C# programming

Unity does a lot of things: it manages objects, renders them, animates them, calculates physics, and more. Unity itself is a program. The internal Unity code is accessible by you, the game developer, through the Unity point-and-click Editor interface we've already been using. Within the Unity Editor, scripts are manifested as configurable components. But, it's also more directly accessible by you through the Unity scripting API.

API, or application programming interface, simply refers to published software functions that you can access from your own scripts. Unity's API is very rich and nicely designed. That's one reason people have written amazing plugin add-ons for Unity.

There are many programming languages available. Unity has chosen to support the C# language from Microsoft. Computer languages have a specific syntax that must be obeyed or the computer will not understand your script. In Unity, script errors (and warnings) appear in the Console panel of the editor, as well as the bottom footer of the app window.

The default script editor for Unity is an integrated development environment (IDE) called MonoDevelop. You can configure a different editor if you want, such as VisualStudio. They have helpful features, such as text formatting, autocompletion, and pop-up help, that understand the Unity documentation. C# scripts are text files, named with a .cs extension.

In a Unity C# script, some of the words and symbols are part of the C# language itself. Some come from the Microsoft .NET Framework, and others are provided by the Unity API. Then, there's the code that you write.

An empty default Unity C# script looks like this:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class NewBehaviourScript : MonoBehaviour { 
 
   // Use this for initialization 
   void Start () { 
          
   } 
    
   // Update is called once per frame 
   void Update () { 
          
   } 
} 

Let's dissect it. The first three lines indicate that this script needs some other stuff to run. The using keyword is C#. The using UnityEngine line says we'll be using the UnityEngine API. The using System.Collections line says we might also use a library of functions named Collections for accessing lists of objects.

C# requires that each line of code ends with a semicolon. Double-slashes (//) indicate comments in the code, and anything from there to the end of that line will be ignored.

This Unity script defines a class named NewBehaviorScript. Classes are like code templates with their own properties (variables) and behaviors (functions). Classes derived from the base class MonoBehaviour are recognized by Unity and used when your game runs. The public class NewBehaviorScript: MonoBehaviour line basically says we are defining a new public class named NewBehaviorScript that inherits all the abilities of the Unity base class MonoBehaviour, including the capability of Start() and Update() functions. The body of our class is enclosed in a pair of curly braces ({}).

When something is public, it can be seen by other code outside this specific script file; when it's private, it can only be referenced within this file. We want Unity to see our NewBehaviorScript class.

Classes define variables and functions. A variable holds data values of a specific type, such as float, int, boolean, GameObject, and Vector3. Functions implement logical step-by-step instructions. Functions can receive arguments--variables enclosed in parenthesis--used by its code, and they can return new values when done.

Numeric float constants, such as 5.0f, require an f at the end in C# to ensure that the data type is a simple floating-point value and not a double-precision floating-point value.

Once you've written or modified a script in your code editor, save it and then switch to the Unity Editor window. Unity will automatically recognize the script has changed and reimport it. If errors are found, it will report them right away, in the Console panel.

Unity will automatically call specific named functions if you've defined them. Note that Start() and Update() are two examples of callbacks. Empty versions of these are provided in the default C# script. The datatype in front of a function indicates the type of value returned; Start() and Update() do not return values, so they're void.

Each Start() function from all the MonoBehaviour scripts in your game is called before the gameplay begins. It's a good place for data initialization.

At runtime, Unity runs a big loop, repeating over and over as the display needs to be updated in each frame, perhaps 60 times a second. All Update() functions are called at each time slice, or frame, while the game is running. This is where much of the action is.

Unity programs live in the real world and must be able to respond to events, such as user input, network messages, or video feed. Events are processed by event handlers, or callbacks, that you can write in your own code. These functions usually start in this format: On[EventName] (such as OnMouseDown).

This is just a cursory introduction to Unity programming. As we work through our projects in this book, we will explain additional things as they're introduced.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.113