Reader small image

You're reading from  Learning C# by Developing Games with Unity - Seventh Edition

Product typeBook
Published inNov 2022
Reading LevelBeginner
PublisherPackt
ISBN-139781837636877
Edition7th Edition
Languages
Tools
Right arrow
Author (1)
Harrison Ferrone
Harrison Ferrone
author image
Harrison Ferrone

Harrison Ferrone is an instructional content creator for LinkedIn Learning and Pluralsight, tech editor for the Ray Wenderlich website, and used to write technical documentation on the Mixed Reality team at Microsoft. He is a graduate of the University of Colorado at Boulder and Columbia College, Chicago. After a few years as an iOS developer at small start-ups, and one Fortune 500 company, he fell into a teaching career and never looked back.
Read more about Harrison Ferrone

Right arrow

Preface

Unity is one of the most popular game engines in the world, catering to hobbyists, professional AAA studios, and cinematic production companies. While known for its use as a 3D tool, Unity has a host of dedicated features supporting everything from 2D games and virtual reality to post-production and cross-platform publishing.

Developers love its drag-and-drop interface and built-in features, but it’s the ability to write custom C# scripts for behaviors and game mechanics that really takes Unity the extra mile. Learning to write C# code might not be a huge obstacle to a seasoned programmer with other languages under their belt, but it can be daunting for those of you who have no programming experience. That’s where this book comes in, as I’ll be taking you through the building blocks of programming and the C# language from scratch while building a fun and playable game prototype in Unity.

Who this book is for

This book was written for those of you who don’t have any experience with the basic tenets of programming or C#. However, if you’re a competent novice or seasoned professional coming from another language, or even C#, but need to get hands-on with game development in Unity, this is still where you want to be.

What this book covers

Chapter 1, Getting to Know Your Environment, starts off with the Unity installation process, the main features of the editor, and finding documentation for C# and Unity-specific topics. We’ll also go through creating C# scripts from inside Unity and look at Visual Studio, the application where all our code editing takes place.

Chapter 2, The Building Blocks of Programming, begins by laying out the atomic-level concepts of programming, giving you the chance to relate variables, methods, and classes to situations in everyday life. From there, we move on to simple debugging techniques, proper formatting and commenting, and how Unity turns C# scripts into components.

Chapter 3, Diving into Variables, Types, and Methods, takes a deeper look at the building blocks from Chapter 2. This includes C# data types, naming conventions, access modifiers, and everything else you’ll need for the foundation of a program. We’ll also go over how to write methods, add parameters, and use return types, ending with an overview of standard Unity methods belonging to the MonoBehaviour class.

Chapter 4, Control Flow and Collection Types, introduces the common approaches to making decisions in code, consisting of the if...else and switch statements. From there, we move on to working with arrays, lists, and dictionaries, and incorporating iteration statements for looping through collection types. We end the chapter with a look at conditional looping statements and a special C# data type called enumerations.

Chapter 5, Working with Classes, Structs, and OOP, details our first contact with constructing and instantiating classes and structs. We’ll go through the basic steps of creating constructors, adding variables and methods, and the fundamentals of subclassing and inheritance. The chapter will end with a comprehensive explanation of object-oriented programming and how it applies to C#.

Chapter 6, Getting Your Hands Dirty with Unity, marks our departure from C# syntax into the world of game design, level building, and Unity’s featured tools. We’ll start by going over the basics of a game design document and then move on to blocking out our level geometry and adding lighting and a simple particle system.

Chapter 7, Movement, Camera Controls, and Collisions, explains different approaches to moving a player object and setting up a third-person camera. We’ll discuss incorporating Unity physics for more realistic locomotion effects, as well as how to work with collider components and capture interactions within a scene.

Chapter 8, Scripting Game Mechanics, introduces the concept of game mechanics and how to effectively implement them. We’ll start by adding a simple jump action, create a shooting mechanic, and build on the previous chapters’ code by adding logic to handle item collection.

Chapter 9, Basic AI and Enemy Behavior, starts with a brief overview of artificial intelligence in games and the concepts we will be applying to Hero Born. Topics covered in this chapter will include navigation in Unity, using the level geometry and a navigation mesh, smart agents, and automated enemy movement.

Chapter 10, Revisiting Types, Methods, and Classes, takes a more in-depth look at data types, intermediate method features, and additional behaviors that can be used for more complex classes. This chapter will give you a deeper understanding of the versatility and breadth of the C# language.

Chapter 11, Specialized Collection Types and LINQ, dives into Stacks, Queues, HashSets, and the different development scenarios that each is uniquely suited for. This chapter also explores filtering, ordering, and transforming data collections using LINQ.

Chapter 12, Saving, Loading, and Serializing Data, gets you ready to handle your game’s information. Topics covered in this chapter include working with the filesystem and creating deleting and updating files. We’ll also cover different data types including XML, JSON, binary data, and end with a practical discussion on serializing C# objects directly into data formats.

Chapter 13, Exploring Generics, Delegates, and Beyond, details intermediate features of the C# language and how to apply them in practical, real-world scenarios. We’ll start with an overview of generic programming and progress to concepts such as delegation, events, and exception handling.

Chapter 14, The Journey Continues, reviews the main topics you’ve learned throughout the book and leaves you with resources for further study in both C# and Unity. Included in these resources will be online reading material, certifications, and a host of my favorite video tutorial channels.

To get the most out of this book

The only thing you need to get the most from your upcoming C# and Unity adventure is a curious mind and a willingness to learn. Having said that, doing all the code exercises, Hero’s trials, and Quiz sections is a must if you hope to cement the knowledge you’re learning. Lastly, revisiting topics and entire chapters to refresh or solidify your understanding before moving on is always a good idea. There is no sense in building a house on an unstable foundation.

You’ll also need a current version of Unity installed on your computer—2022 or later is recommended. All code examples have been tested with Unity 2022.1 and should work with future versions without issues.

Software/hardware covered in the book

Unity 2022.1 or later

Visual Studio 2019 or later

C# 8.0 or later

Before starting, check that your computer setup meets the Unity system requirements at https://docs.unity3d.com/2022.1/Documentation/Manual/system-requirements.html.

Download the example code files

The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Learning-C-by-Developing-Games-with-Unity-Seventh-Edition. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/7yy5V.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: “Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system.”

A block of code is set as follows:

public class Shop<T>
{
    public List<T> inventory = new List<T>();
    // 1
    public void AddItem(T newItem)
    {
      
        inventory.Add(newItem);
    }
}

Any command-line input or output is written as follows:

# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample
     /etc/asterisk/cdr_mysql.conf

Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “Select System info from the Administration panel.”

Warnings or important notes appear like this.

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email feedback@packtpub.com and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com.

Share your thoughts

Once you’ve read Learning C# by Developing Games with Unity, Seventh Edition, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.

Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.

Download a free PDF copy of this book

Thanks for purchasing this book!

Do you like to read on the go but are unable to carry your print books everywhere?

Is your eBook purchase not compatible with the device of your choice?

Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.

Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.

The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily

Follow these simple steps to get the benefits:

  1. Scan the QR code or visit the link below
https://packt.link/free-ebook/9781837636877
  1. Submit your proof of purchase
  2. That’s it! We’ll send your free PDF and other benefits to your email directly
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning C# by Developing Games with Unity - Seventh Edition
Published in: Nov 2022Publisher: PacktISBN-13: 9781837636877
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Harrison Ferrone

Harrison Ferrone is an instructional content creator for LinkedIn Learning and Pluralsight, tech editor for the Ray Wenderlich website, and used to write technical documentation on the Mixed Reality team at Microsoft. He is a graduate of the University of Colorado at Boulder and Columbia College, Chicago. After a few years as an iOS developer at small start-ups, and one Fortune 500 company, he fell into a teaching career and never looked back.
Read more about Harrison Ferrone