Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Learning C# by Developing Games with Unity
  • Toc
  • feedback
Learning C# by Developing Games with Unity

Learning C# by Developing Games with Unity

By : Harrison Ferrone
4.4 (47)
close
Learning C# by Developing Games with Unity

Learning C# by Developing Games with Unity

4.4 (47)
By: Harrison Ferrone

Overview of this book

It's the ability to write custom C# scripts for behaviors and game mechanics that really takes Unity the extra mile. That's where this book can help you as a new programmer! Harrison Ferrone, in this seventh edition of the bestselling series will take you through the building blocks of programming and the C# language from scratch while building a fun and playable game prototype in Unity. This book will teach you the fundamentals of OOPs, basic concepts of C#, and Unity engine with lots of code samples, exercises and tips to go beyond the book with your work. You will write C# scripts for simple game mechanics, perform procedural programming, and add complexity to your games by introducing intelligent enemies and damage-dealing projectiles. You will explore the fundamentals of Unity game development, including game design, lighting basics, player movement, camera controls, collisions, and more with every passing chapter. Note: The screenshots in the book display the Unity editor in full-screen mode for a comprehensive view. Users can easily reference color versions of images by downloading them from the GitHub repository or the graphics bundle linked in the book.
Table of Contents (18 chapters)
close
15
Pop Quiz Answers
16
Other Books You May Enjoy
17
Index

Getting started with Unity 2023

If you don’t have Unity installed (or are running an earlier version), follow these steps to set up your environment:

  1. Head over to https://www.unity.com/.
  2. Select Download (shown in Figure 1.1):

    Figure 1.1: Unity homepage

    To provide a complete view of the Unity editor, all our screenshots are taken in full-screen mode. For color versions of all book images, use the link below: https://packt.link/7yy5V.

  3. This will take you to the Unity store page. Don’t feel overwhelmed by this—you can download Unity completely for free!

    If the Unity homepage looks different for you than what you can see in Figure 1.1, you can go directly to https://store.unity.com.

  4. Scroll down to the How to get started section and download the Unity Hub application for either Windows or Mac, as shown in Figure 1.2. I’ll be using a Mac, but everything works the same on a Windows machine:

    Figure 1.2: Start creating with the Unity portal

  5. Once the download is complete, follow these steps:
    1. Open up the installer (by double-clicking it)
    2. Accept the user agreement
    3. Follow the installation instructions
  6. When you get the green light, go ahead and fire up the Unity Hub application!
    1. If Unity asks you to choose a license option, select the Personal license option (which is completely free) and follow the instructions to set up your account.
  7. The newest version of Unity Hub will prompt you to install the latest LTS (Long Term Support) version of Unity, as shown in Figure 1.3. If the default version is Unity 2023 or higher when you’re reading these instructions, select Install Unity Editor:

    Figure 1.3: Install Unity Editor window

  8. If Unity 2023 is not the default version when you’re reading this, select Skip installation in the bottom-right corner of Figure 1.4:

    Figure 1.4: Install wizard

  9. Switch to the Installs tab from the left-hand menu and select Install Editor, as shown in Figure 1.5:

    Figure 1.5: Unity Hub Installs panel

  10. On the Official releases tab, select your desired version of Unity, then click Install (Silicon for Macs, Intel for Windows). At the time of writing, Unity 2023 is listed under the OTHER VERSIONS section of the Official releases tab, but you should be able to select a 2023 version from the Official releases list by the time you’re reading this:

    Figure 1.6: Add Unity version pop-up window

  11. You’ll then be given the option to add various modules to your installation. Make sure the Visual Studio (for Mac or Windows accordingly) module is selected and click Continue:

    Figure 1.7: Adding install modules

  12. If you want to add any modules later, you can click the gear icon to the right of any installed version in the Installs window.

When the installation is complete, you’ll see a new version in your Installs panel, as follows:

Figure 1.8: Installs tab with Unity versions

You can find additional information and resources about the Unity Hub application at https://docs.unity3d.com/hub/manual/index.html.

There’s always a chance of something going wrong, so be sure to check the following section if you’re using macOS Catalina or later, which has been known to throw up issues.

Using macOS

If you’re working on a Mac with OS Catalina or later, there is a known issue when using some versions of Unity Hub to install Unity. If this is the case for you, take a deep breath, go to the Unity download archive, and grab the 2023 version you need (https://unity3d.com/get-unity/download/archive). Remember to use the Downloads (Mac) or Downloads (Win) option instead of the Unity Hub download:

Figure 1.9: Unity download archive

Once the installer application downloads, open it up and follow the setup instructions! All of the examples and screenshots for this book were created and captured using Unity 2023.1.5f1. If you’re using a newer version, things might look slightly different in the Unity Editor, but this shouldn’t affect your following along.

Now that Unity Hub and Unity 2023 are installed, it’s time to create a new project!

Creating a new project

Launch the Unity Hub application, which is your staging area—you can see a list of all your projects and Unity versions and access learning resources and community features here. Then, take the following steps:

  1. To get started, click on New project in the top-right corner:

    Figure 1.10: Unity Hub Projects panel

  2. Make sure the editor version at the top is set to your 2023 version and set the following fields:
    • Templates: The project will default to 3D Core
    • Project name: I’ll be calling mine Hero Born
    • Location: Wherever you’d like the project to be saved
  3. Once the settings have been configured, hit Create project:

    Figure 1.11: Unity Hub with New project configuration popup

With the project created, you’re all set to explore the Unity interface! You can re-open your project anytime from the Projects panel in Unity Hub, but if your computer is running a little slow with both Unity and Unity Hub open, feel free to close Unity Hub.

Navigating the editor

When the new project finishes initializing, you’ll see the glorious Unity Editor! I’ve marked the important tabs (or windows, if you prefer) in Figure 1.12:

Figure 1.12: Unity interface

This is a lot to take in, so we’ll look at each of these panels in more detail:

  1. The Toolbar panel is the topmost part of the Unity Editor. From here, you can sign in to a Unity account, manage services, access the Asset Store, collaborate with a team (far-left button group), and play and pause the game (the center buttons). The right-most button group contains a search feature, LayerMasks, and layout scheme features, which we won’t be using in this book because they don’t apply to learning C#.
  2. The Hierarchy window shows every item currently in the game scene. In the starter project, this is just the default camera and directional light, but when we create our prototype environment, this window will start to get filled in with the objects we create in the scene.
  3. The Game and Scene windows are the most visual aspects of the editor. Think of the Scene window as your stage, where you can move and arrange 2D and 3D objects. When you hit the Play button, the Game window will take over, rendering the Scene view and any programmed interactions. You can also use the Scene view when you’re in play mode.
  4. The Inspector window is your one-stop shop for viewing and editing the properties of objects in the scene. If you select Main Camera in the Hierarchy (highlighted in blue in the above screenshot), you’ll see several parts displayed, which Unity calls components—all of which are accessible from the Inspector.
  5. The Project window holds every asset that’s currently in your project. Think of this as a representation of your project’s folders and files.
  6. The Console window is where any output we want our scripts to print will show up. From here on out, if we talk about the console or debug output, this panel is where it will be displayed.

If any of these windows get closed by accident, you can re-open them anytime from the Unity menu > Window > General. You can find more in-depth breakdowns of each window’s functionality in the Unity docs at: https://docs.unity3d.com/Manual/UsingTheEditor.html.

Before continuing, it’s important that Visual Studio is set up as the script editor for your project. Go to the Unity menu > Preferences > External Tools and check that External Script Editor is set to Visual Studio for Mac (or Windows):

Figure 1.13: Changing External Script Editor to Visual Studio

As a final tip, if you want to switch between light and dark modes, go to the Unity menu > Preferences > General and change Editor Theme:

Figure 1.14: Unity general preferences panel

I know that was a lot to process if you’re new to Unity, but rest assured that any instructions going forward will always reference the necessary steps. I won’t leave you wondering what button to push. With that out of the way, let’s start creating some actual C# scripts.

bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete